#!/bin/bash

# Overview of backup process
#  1) Create a FS snapshot using LVM
#  2) Mount the snapshot FS
#  3) Mount our backup file in a loop filesystem (we can guarantee the space
#     will be present this way, and it allows selective recovery of files if
#     need be)
#  4) rsync the differences between the snapshot FS and the backup FS
#  5) Unmount the backup FS
#  6) Unmount the snapshot FS
#  7) Delete the snapshot LVM (leaving it will degrade performance)

# Create the LVM snapshot
lvcreate -s -L40G -n FC6Backup /dev/System/fc6

# Mount our filesystems
mount /dev/System/FC6Backup /mnt/FC6-Snapshot
mount -o loop /mnt/smb/userdata/root/FC6-Backup-File /mnt/FC6-Backup

# Update the backup file system with files from the snapshot
rsync -aHEAXxv --stats --delete --ignore-errors /mnt/FC6-Snapshot/* /mnt/FC6-Backup

# Unmount the backups
umount /mnt/FC6-Backup
umount /mnt/FC6-Snapshot

# Delete the snapshot
lvremove -f /dev/System/FC6Backup

# Create a link to the backup file which (in the name) indicates the date of
# the backup
cd /mnt/smb/userdata/root
rm -f FC6-Backup_*
ln -s FC6-Backup-File FC6-Backup_$(date +%F)

