How do I copy the database from one node over to another node?

Introduction

When we deploy new nodes or have to completely redo one from the ground up we could make a copy of an existing database and have a copy of that be transferred to our new node to mitigate all the time and server resources it would take to sync the database from epoch 0.

Step 1 - Stop our source node

Let's start by stopping our node from where we will make source the database

sudo systemctl stop cnode.service

Step 2 - Move over to Database and do some preparation

We need to change over to our db folder under our cardano node

cd db			#Or wherever you have chosen to store your files and folders
ls

You should see something like this:

OUTPUT:

immutable  ledger  lock  protocolMagicId  shelley_trans_epoch  volatile

Step 3 - Remove the contents of the ledger directory

rm -rf ledger/*		#Remove everything in the ledger dir using the options of -rf (recursively, force)

Step 4 - Restart the node

sudo systemctl start cnode.service

Let the database sync again before we stop the node again.

How do we know when the database is synced on the blockchain? To answer that you can follow the steps by Damien Ostrelich of EDEN pools in his YouTube tutorial:

Or you can copy paste his instructions here:

cd ../logs
tail -f node0.json

The output shows the db syncing in real time.

But we want to filter the output to show us only those lines that cnoptain the "contents" parameter

tail -f node0.json | grep "contents"

The lines on output will now only show those lines that contain the "contents" parameter (usually highlighted in red). The contents parameter is an indicator of the amount of db synced and we need to wait till the subsequent indicator after the "contents" word in the line reaches 100 (100%). See video above to see this in real time.

Step 5 - Stop the node (again) and take a snapshot

Once the node sync contents are 100% we stop the node

sudo systemctl stop cnode.service

Compress a copy of your database (this will take some time)

cd ..
tar -czvf db_snapshot.tar.gz db/

Once the snapshot (the compressed file completed) we restart the node

sudo systemctl start cnode.service

Step 6 - Copy the compressed database over to your new node

This step can be done to your liking. Either by transferring using an app like FileZilla to download the file and reupload to your new server. Or, you can do the transfer from the command line and login to the new server using ssh and do a direct server to server transfer. We leave this step up to you.

Step 7 - Stop your destination node

sudo systemctl disable cnode.service
sudo systemctl stop cnode.service

Step 8 - Rebuild node software as if you are updating the node upgrade

This is the common procedure of upgrading your node software and binaries. You do this as you would any other normal upgrade and instructions on this are skipped since this is covered multiple times in answers on CardanoFAQ.

Step 9 - Replace database after Cardano node is updated and upgraded

Decompress (extract) the db_database.tar.gz file you uploaded into the directory holding the db directory. First we backup the existing database in case we need to roll back for whatever reason.

cp -r db/ db-backup/

Remove the database

rm -r db

Extract the database tar file

tar -xzvf db_snapshot.tar.gz

This will recreate the db folder with all of its contents

Step 10 - Restart the cnode service

systemctl enable cnode.service
systemctl start cnode.service
systemctl status cnode.service

Step 11 - Check to see the database is syncing to the network

Here we can open our gLiveView utility and just check that it is syncing and working again.

That should be it!

 


Links used in this answer:

Damien Ostrelich's YouTube tutorial on this issue: https://youtu.be/4CxYezEvFnY


This answer was published 02 Jan 2021


Revision #3
Created Sat, Feb 6, 2021 12:35 PM by Clive Hyman
Updated Tue, Feb 9, 2021 11:52 AM by Clive Hyman