Technology

Hyperledger Fabric: Backup and Restore

JavaScript frameworks make development easy with extensive features and functionalities. Here are our top 10 to use in 2022.
Written by
spydraadmin
Published on
September 14, 2022

There are four main items that are needed for the backup to work. Only with these four items available, would you be able to recreate the network.

  1. Crypto materials (Public/Private Keys and Certificates)
  2. Channel artifacts (Genesis block and channel blocks)
  3. Peer backup files
  4. Orderer backup files

These four items are the key to recreating a network even if the network is completely down. If the network has CouchDB as the configured DB, it does not matter if you wish to back up the CouchDB files or not because the peers will keep the couchdb in sync once they have their blocks.

These steps assume that you are in the folder where the network is brought up. For example, if you bring up the network using the Build Your First Network (BYFN) tutorial, you will be in the fabric-samples/first-network/ directory using the ./byfn.sh command.

For these steps, the folder name blockchain-network/ will be used. The following steps also assume the network is brought up using the BYFN method with 2 peers, 1 organization, and 1 Orderer using Docker.

These four items are the key to recreating a network even if the network is completely down. If the network has CouchDB as the configured DB, it does not matter if you wish to back up the CouchDB files or not because the peers will keep the CouchDB in sync once they have their blocks.

These steps assume that you are in the folder where the network is brought up. For example, if you bring up the network using the Build Your First Network (BYFN) tutorial, you will be in the fabric-samples/first-network/ directory using the ./byfn.sh command.

For these steps, the folder name blockchain-network/ will be used. The following steps also assume the network is brought up using the BYFN method with 2 peers, 1 organization, and 1 Orderer using Docker.

Assuming the directory as below:

blockchain-network
- byfn.sh
- configtx.yaml
- crypto-config.yaml
- docker-compose.yaml

Steps to Backup

Step 1: Create a backup folder.

blockchain-network$ mkdir backup

Step 2: Create a copy of the crypto materials.

Before bringing up the network, you will have to generate the crypto materials using the crypto-config.yaml file.

This will generate a folder, crypto-config/ , containing all the public and private keys as well as the certificates.

Copy the crypto-config/ folder into the backup/ folder.

blockchain-network$ cp –r crypto-config/ backup/

Step 3: Create a copy of the channel artifacts.

Before bringing up the network, you will have to generate the channel artifacts using the configtx.yaml file. This will generate the Orderer genesis block (genesis.block), channel configuration transaction (channel.tx) and anchor peer update transaction(Org1MSPanchors.tx) files in the channel-artifacts/ folder.

Copy the channel-artifacts/ folder onto the backup/ folder

blockchain-network$ cp –r channel-artifacts/ backup/

Step 4: Extract peers files for backup

Extract data from peer0. The ledger data, block data, and chaincode used are stored in the /var/hyperledger/production/ folder in the peer. In this folder, there are three main folders:

  1. chaincodes/
  2. ledgerData/
  3. transientStore/

These three folders are required to recreate the peer. Thus, copy everything inside the /var/hyperledger/production/ folder into backup/peer0.org1/ folder.

blockchain-network$ docker cp peer0.org1.example.com:/var/hyperledger/production/ backup/peer0.org1/

Extract data from peer1. The ledger data, block data and chaincode used are stored in the /var/hyperledger/production/ folder in the peer. In this folder, there are three main folders:

  1. chaincodes/
  2. ledgerData/
  3. transientStore/

These three folders are required to recreate the peer. Thus, copy everything inside the /var/hyperledger/production/ folder into backup/peer1.org1/ folder.

blockchain-network$ docker cp peer1.org1.example.com:/var/hyperledger/production/ backup/peer1.org1/

Step 5: Extract Orderer files for backup

Extract data from Orderer. The block files and channel configuration details are stored in the /var/hyperledger/production/orderer/ folder in the orderer. In this folder, there are two main folders:

  1. chains/
  2. index/

These two folders are required to recreate the Orderer. Thus, copy everything inside the /var/hyperledger/production/orderer/ folder into the backup/orderer/ folder.

blockchain-network$ docker cp orderer.example.com:/var/hyperledger/production/orderer/ backup/orderer/

Now we have backed up all the important files necessary to recreate the network.
The restoration assumes that the network is down and all Docker images are removed. The crypto materials and channel artifacts are also deleted. The only thing that remains is the configured network in the docker-compose.yaml file.

Steps to Restore

Step 1: Copy the backup folders out.

Copy the folders crypto-config/, channel-artifacts/, orderer/, peer0.org1/, peer1.org1/ from backup/ to blockchain-network/

blockchain-network$ cd backup/ && cp -r * ../ && cd ../

Step 2: Configure docker-compose.yaml files for peers to mount from backup

Configure Peer0 to mount files from localhost. In the peer0.org1.example.com service in the yaml file, look for the volumes section as shown below

restore1.png

Notice the part peer0.org1.example.com:/var/hyperledger/production That is where to mount.

Change from peer0.org1.example.com:/var/hyperledger/production
To => ./peer0.org1:/var/hyperledger/production

Assuming the yaml file is in the same location as blockchain-network/. The backup for peer0.org1/ has already been copied out.

So the volume configuration will be:
./peer0.org1:/var/hyperledger/production

Configure peer1 to mount files from localhost. In the peer1.org1.example.com service in the yaml file, look for the volumes section as shown below

restore2.png

Notice the part peer1.org1.example.com:/var/hyperledger/production That is where to mount.

Change from peer1.org1.example.com:/var/hyperledger/production
To => ./peer1.org1:/var/hyperledger/production

Assuming the yaml file is in the same location as blockchain-network/. The backup for peer1.org1/ has already been copied out.

So the volume configuration will be:
./peer1.org1:/var/hyperledger/production

Step 3: Configure docker-compose.yaml files for Orderer to mount from backup

Configure Orderer to mount files from localhost. In the orderer.example.com service in the yaml file, look for the volumes section as shown below:

restore3.png

Notice the part orderer.example.com:/var/hyperledger/production/orderer. That is where to mount.

Change from orderer.example.com:/var/hyperledger/production/orderer
To => ./orderer:/var/hyperledger/production/orderer

Assuming the yaml file is in the same location as blockchain-network/.

The backup for Orderer/ has already been copied out. So the volume configuration will be:
./orderer:/var/hyperledger/production/orderer

Start the network
Previously the network was started using ./byfn.sh up command. Use the same command

blockchain-network$ ./byfn.sh up

or if the network was started using a different script, use it.

You’ll notice the peers will automatically join the channel created previously before the network is brought down. The ledger data will be restored and the number of blocks will resume from where the previous network left off.

Latest posts

Subscribe to Our Newsletter

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.