Skip to content

Instantly share code, notes, and snippets.

@calebreister
Last active August 29, 2015 14:10
Show Gist options
  • Save calebreister/1eaf1ac48ecc58a6276e to your computer and use it in GitHub Desktop.
Save calebreister/1eaf1ac48ecc58a6276e to your computer and use it in GitHub Desktop.
This is a shell script that runs incremental backups on the btrfs filesystem. I used the instructions on the wiki for the initial setup. It is designed to work with the Ubuntu btrfs configuration. THIS SCRIPT COMES WITH ABSOLUTELY NO WARRANTY. Any loss of data is not my responsibility. However, it does work for me, and I regularly use it.
#!/bin/sh
#Find a date in file name:
#ls | grep -E $datematch
if [ "$(whoami)" != "root" ]; then
echo Please run this script as root.
exit
fi
today=$(date +%Y-%m-%d)
datematch='[0-9]{4}-[0-9]{2}-[0-9]{2}'
cd /mnt
#Mount drives
mount /dev/sda3 ./root
mount /dev/sdb ./bu
increment() {
sync
# $1 directory to look in
# $2 subvolume to backup
# $3 y/n delete old snapshots from internal drive
local old="$(ls $1 | grep -E $2-$datematch)"
local new=$2-$today
echo Backing up $old...
btrfs sub snap -r $1/$2 $1/$new
echo Sending $new to external drive...
if ([ $3 = "y" ] || [ $3 = "Y" ]); then
#The following two lines make sure that the old subvolume is not deleted
#if there was an error in the copying.
btrfs send -p $1/$old $1/$new | btrfs receive -e bu/ && btrfs sub del $1/$old
else
btrfs send -p $1/$old $1/$new | btrfs receive bu/
fi
echo -----------------------------------------------------------------------
}
#User Prompts
read -p 'Do you want to backup home [y/n]? ' bu_home
read -p 'Do you want to backup root [y/n]? ' bu_root
read -p 'Do you want to delete the old snapshots on the internal drive [y/n]? ' del_old
#Backup function calls
if ([ $bu_home = "y" ] || [ $bu_home = "Y" ]); then
increment root @home $del_old
fi
if ([ $bu_root = "y" ] || [ $bu_root = "Y" ]); then
increment root @ $del_old
fi
#Unmount drives
umount /mnt/bu
umount /mnt/root
echo Drives unmounted.
@joegyoung
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment