Rsync backup with BTRFS

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.

Backup to USB drive and power it off when not in use

On Linux, it is possible to power off a USB device using command line. It’s perfect for my USB backup drive. I can power it off after backup every night, and power it on before the job starts. This saves power, reduce heat, and more importantly, prevent accidental or unexpected changes as the device is completely offline.

On Linux, it is possible to power off a USB device using command line. It’s perfect for my USB backup drive. I can power it off after backup every night, and power it on before the job starts. This saves power, reduce heat, and more importantly, prevent accidental or unexpected changes as the device is completely offline.
Continue reading “Backup to USB drive and power it off when not in use”