User Tools

Site Tools


guides:farming:backupdb

How to backup the Chia blockchain database

You can use SQLite's ''VACUUM INTO'' to create a consistent backup of the blockchain database without having to stop the node.

Requirements

  • You need to have the SQLite CLI installed (the sqlite3 command). On Debian/Ubuntu, install the sqlite package for that.
  • You need to have enough free space for another full copy of the blockchain database.
  • The online backup takes some time (typically on the order of minutes), and is heavy on disk I/O. Test that your node can handle the additional disk I/O so that you don't fall out of sync during backups.

One time hot backup (Linux)

If your database is in the default location, run the following command:

sqlite3 $HOME/.chia/mainnet/db/blockchain_v2_mainnet.sqlite \
  "vacuum into '$HOME/.chia/mainnet/db/blockchain_v2_mainnet.sqlite.bak'"

If you moved your database, adapt the paths accordingly.

Daily backup using systemd user units (Linux)

If you use a Linux distribution using systemd, you can use systemd's user units to automate the db backup. (Of course, any other automation method, such as plain cronjobs, will do as well.)

With systemd user units, first ensure that the path for user units exists:

mkdir -p ~/.config/systemd/user

Create a ~/.config/systemd/user/chia-blockchain-backup.service file, the unit that runs the backup, with the following content:

[Unit]
Description=Hot-backup Chia blockchain database

[Service]
Type=oneshot
Environment=DATABASE=%h/.chia/mainnet/db/blockchain_v2_mainnet.sqlite
ExecStartPre=rm -f ${DATABASE}.bak
ExecStart=/usr/bin/sqlite3 ${DATABASE} "vacuum into '${DATABASE}.bak'"

Create a ~/.config/systemd/user/chia-blockchain-backup.timer, the unit that schedules automatic runs of the backup, with the following content:

[Unit]
Description=Run Chia blockchain database backup

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=900

[Install]
WantedBy=timers.target

This timer unit will run a backup once a day, at some random time in the first 15 minutes (900 seconds) past midnight.

Finally, enable the automatic backup by running:

systemctl --user enable --now chia-blockchain-backup.timer

Tips & tricks for daily use

To see when the backup will run next, execute:

$ systemctl --user status chia-blockchain-backup.timer
● chia-blockchain-backup.timer - Run Chia blockchain database backup
     Loaded: loaded (/home/chia/.config/systemd/user/chia-blockchain-backup.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Sun 2021-11-28 18:56:45 CET; 3min 14s ago
    Trigger: Mon 2021-11-29 00:06:29 CET; 5h 6min left
...

This output tells you, that the next backup is scheduled for Mon, 2021-11-29 at 00:06:29 CET.

If you ever want to run a backup of the database right now (to, for example, create an up-to-date copy of the database), you can run the following:

systemctl --user start chia-blockchain-backup.service

To see when the last backup(s) ran and how long it took, you can call:

$ systemctl --user status chia-blockchain-backup.service
● chia-blockchain-backup.service - Hot-backup Chia blockchain database
     Loaded: loaded (/home/chia/.config/systemd/user/chia-blockchain-backup.service; static; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● chia-blockchain-backup.timer

Nov 28 17:14:30 my.linux.host systemd[3979]: Starting Hot-backup Chia blockchain database...
Nov 28 17:24:38 my.linux.host systemd[3979]: chia-blockchain-backup.service: Succeeded.
Nov 28 17:24:38 my.linux.host systemd[3979]: Finished Hot-backup Chia blockchain database.

Here you see that the last backup ran on Nov 28 at 17:14 and took roughly 10 minutes to complete.

guides/farming/backupdb.txt · Last modified: 2022/08/23 04:21 by earl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki