Wednesday, 27 July 2016

Unix/Linux Disaster Recovery - Full system backups using ZFS


Assuming you have created your ZFS system,  here is how to do full system backups to an extra attached disk.
You can adjust these instructions if you need to store the backup remotely – but they are out of scope of this post.
How to Prepare ZFS and take snapshot
First, in case you haven’t already… here is how to format/dev/da1 as a dedicated ZFS backup drive.  You can configure the backup drive however you want (it doesn’t even need to be ZFS-based) but you will also have to adjust these instructions accordingly to restore too.
gpart destroy -F da1
dd if=/dev/zero of=/dev/da1 bs=1m count=128
zpool create zbackup /dev/da1
zfs set mountpoint=/backup zbackup
The above will destroy any existing data on /dev/da1, and create a ZFS filesystem which is mounted at /backup.
Next we snapshot all filesystems under zroot and send them to a gzip’d file on the backup medium.  We then release the snapshot:
zfs snapshot -r zroot@backup
zfs send -Rv zroot@backup | gzip > /backup/full-system-backup.zfs.gz
zfs destroy -r zroot@backup
This is all you need to do on the live system.
If the worst happens, and you need to restore to a new system (or a freshly formatted one)…
How to Restore 
Firstly, follow the original instructions up to and including the line “zfs set checksum=fletcher4 zroot”.
Next, we import the backup ZFS drive and mount it – then we use ZFS receive to restore the filesystem and all its dependants:
zpool import -f zbackup
zfs set mountpoint=/boot/zfs/backup zbackup
zfs mount zbackup
gunzip -c /boot/zfs/backup/full-system-backup.zfs.gz | zfs receive -vdF zroot
Now we need to unmount the backup drive, and mount the original root ZFS so we can re-create the cache file (the system will not boot without the correct cache file):
zpool export zbackup
zfs set mountpoint=/boot/zfs/zroot zroot
cd /boot/zfs
zpool export zroot && zpool import zroot
cp /boot/zfs/zpool.cache /boot/zfs/zroot/boot/zfs/zpool.cache
zfs unmount -a
zfs set mountpoint=legacy zroot
reboot
This will reboot the system in its original state.  If you want to re-mount your backup medium, it will need to be re-imported and mounted:
zpool import -f zbackup
zfs set mountpoint=/backup zbackup
That’s all there is to it.  A fully working disaster recovery solution

3 comments: