Restore a Bitcoin blockchain backup copy to a new installation.

Salvador Guerrero
2 min readAug 27, 2023

--

These are the steps I took to use a previously downloaded Bitcoin blockchain on a new Raspberry Pi installation

If you haven’t already, stop bitcoind

$ sudo systemctl stop bitcoind

If the backup exists in an external hard drive, connect it to the Raspberry Pi and mount it, assuming that /dev/sdb1 is the external drive run the following commands:

$ sudo mkdir /mnt/sdb
$ sudo mount -t auto /dev/sdb1 /mnt/sdb

My backup exists in a folder called Bitcoin within /mnt/sdb/.

Before you continue, backup the existing copies of blocks, indexes, and chainstate, who knows if you need it for anything.
/mnt/sda/bitcoin is where I want to restore.

$ pushd /mnt/sda/bitcoin
$ sudo mv blocks _blocks_bk_
$ sudo mv indexes _indexes_bk_
$ sudo mv chainstate _chainstate_bk_

Because the copy operation is going to take hours, I don’t want it attached to my terminal because I’m sure it will disconnect from ssh and eventually terminate the command; this is why I’m using nohup as a workaround.
Copy the backup to the new drive in the background:

$ sudo nohup cp -r /mnt/sdb/Bitcoin/chainstate . &
$ sudo nohup cp -r /mnt/sdb/Bitcoin/indexes . &
$ sudo nohup cp -r /mnt/sdb/Bitcoin/blocks . && echo "done!" | mail -a "From: SENDER <SENDER@MAIL.com>" -s "Copy Finished!" RECEIVER@MAIL.com &

As a bonus, I appended a mail command to the blocks copy command because it’s the one that takes longer to send me an email when finished.

You can check the status of the jobs by calling the jobs command

jobs
[1] Running sudo nohup cp -r /mnt/sdb/Bitcoin/chainstate . &
[2]- Running sudo nohup cp -r /mnt/sdb/Bitcoin/indexes . &
[3]+ Running sudo nohup cp -r /mnt/sdb/Bitcoin/blocks . &

Change ownership of the new copy:

$ sudo chown -R sandbox:sandbox chainstate
$ sudo chmod -R 600 chainstate

$ sudo chown -R sandbox:sandbox indexes
$ sudo chmod -R 600 indexes

$ sudo chown -R sandbox:sandbox blocks
$ sudo chmod -R 600 blocks

All folders should have the execute permission

$ sudo find blocks -type d -exec sudo chmod 700 {} \;
$ sudo find chainstate -type d -exec sudo chmod 700 {} \;
$ sudo find indexes -type d -exec sudo chmod 700 {} \;

Rescan

Before triggering rescan, switch to the sandbox user

$ sudo su - sandbox

Initiate rescan

bitcoind -reindex

That’s it!
No need to wait for this command to end because it takes a very long time.

--

--

Salvador Guerrero

Computer Science Engineer, Cross-Platform App Developer, Open Source contributor. 🇲🇽🇺🇸