Thursday, March 25, 2010

Quick local backup with rsync & btrfs

I needed to put a spare eSATA drive to use as a backup for my main Corporate box, but I also wanted to try out Btrfs & use its snapshot capabilities to aid in that effort. Using Ubuntu Karmic / 9.10 ...

1. Make sure a newer kernel is loaded from mainline: I'm using 2.6.33 @ the moment.
2. sudo apt-get install btrfs-tools
3. Attach the backup drive & partition as needed using fdisk.
4. mkfs.btrfs (new partition)
5. Create a directory to mount to, such as /media/backup
6. Edit /etc/modules to add btrfs
7. Edit /etc/fstab with the following modified for your needs...

(new partition) /media/backup btrfs compress 0 0

8. Update your boot files with update-initramfs -u
9. Make a backup script to run daily: example /etc/backup.sh

#!/bin/sh
DATE=`date +%F`
rsync -avlh (folder to backup) /media/backup/(folder to backup)
btrfsctl -s /media/backup/$DATE /media/backup

9. Add the script to your /etc/crontab file (and maybe run /etc/init.d/cron restart afterwards)

30 20 * * * root sh /etc/backup.sh

References

* Btrfs Wiki
* "Back up like an expert with rsync"
* Blogger's entry on getting btrfs to work with Debian/Ubuntu

2 comments:

  1. I have something similar setup.
    A couple of suggestions:
    (1) Make a "live" subvolume and use that as the mount point:
    btrfsctl -S live /media/backup
    ... then in fstab:
    (new partition) /media/backup btrfs compress,subvol=live 0 0
    and
    (new partition) /media/backuproot btrfs compress 0 0

    This (to my way of thinking) makes it a bit neater and avoids some potential (?) recursiveness issues.

    (2) I have written a script to take automatic hourly/daily/weekly/monthly snapshots and delete them automatically once they reach a specified age - no room to post here but will find somewhere if anyone is interested.

    ReplyDelete
  2. I'm interested. Let me know where/when you manage to post...

    ReplyDelete