Rsync backup with BTRFS

Posted under tech On By xpk

There are plenty of backup tools in the community but I still prefer rsync for desktop backup. It only copies the changes and it does not require any catalog. I’ve been using that for years, rsync-ing my files to a USB drive. While it works fine and it saved my day a couple times, my script only keeps 1 version of backup. It’d be nice if I can have at least 2 versions, one from today and one from yesterday.

To set that up, I formatted the USB drive with BTRFS, mounted it under /backup, and created a subvolume /backup/current. Then create a read-only snapshot and put it under /backup/previous. This task is needed for tomorrow’s run.

mkfs.btrfs /dev/sdd -f
mount /dev/sdd /backup btrfs subvolume create /backup/current btrfs subvolume snapshot -r /backup/current /backup/previous rsync -a /source /backup/current/

The rsync command is a simplified version. Basically it puts a copy of my files under /backup/current. In all subsequent days, my script will perform the followings

btrfs subvolume delete /backup/previous
btrfs subvolume snapshot -r /backup/current /backup/previous
rsync -a /source /backup/current/

It first remove the snapshot for the previous day, make a new snapshot of today, and start rsync again.

To access the most recent backup, simply go into /backup/current. To access the backup for the previous day, go into /backup/previous.

With a bigger backup disk and additional script logic, this can easily be made to store more versions. Every version is immediately available for examination and restore.

To prevent accidental change of data on the backup drive, mount it before backup script runs, and unmount it after. For this particular setup, only /backup needs to be unmounted. The subvolumes will follow.

 783 total views,  2 views today