From d5a105f3f222ab7be3f46b526c254634d5fbde32 Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Thu, 5 May 2016 01:48:13 +0000 Subject: [PATCH] Implement backup script --- backup.fish | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ utils.fish | 4 ++++ 2 files changed, 52 insertions(+) create mode 100755 backup.fish diff --git a/backup.fish b/backup.fish new file mode 100755 index 0000000..2341e9b --- /dev/null +++ b/backup.fish @@ -0,0 +1,48 @@ +#!/usr/bin/fish + +set scriptdir (dirname (status --current-filename)) + +source $scriptdir/config.fish +source $scriptdir/utils.fish + +if not test -d $subvoldir + echo "Backup directory doesn't exist, run bootstrap.fish first!" + exit +end + +echo "Creating new snapshots..." +for pair in (get-subvol-mounts) + set subvol (get-subvol $pair) + set mount (get-mount $pair) + + set safename (clean-name $subvol) + set safepath $subvoldir/$safename + + sudo btrfs subvolume snapshot -r $mount $safepath-new + + set snapshots $snapshots $safepath +end + +echo "Syncing..." +sync + +echo "Sending to target..." +for snapshot in $snapshots + sudo btrfs send -p $snapshot $snapshot-new | receive-on-target +end + +echo "Replacing old snapshots..." +for snapshot in $snapshots + sudo btrfs subvolume delete $snapshot + sudo mv $snapshot-new $snapshot +end + +echo "Renaming subvolumes on target..." +for pair in (get-subvol-mounts) + set subvol (clean-name (get-subvol $pair)) + + rename-target-subvolume $subvol $subvol.(date +%Y-%m-%d) + rename-target-subvolume $subvol-new $subvol +end + +echo "Done!" diff --git a/utils.fish b/utils.fish index 2987baa..b62bfd7 100755 --- a/utils.fish +++ b/utils.fish @@ -9,6 +9,10 @@ function receive-on-target ssh -i $sshkey $targetuser@$targethost sudo btrfs receive $targetpath end +function rename-target-subvolume + ssh -i $sshkey $targetuser@$targethost mv $targetpath/$argv[1] $targetpath/$argv[2] +end + function get-subvol echo $argv | cut -d' ' -f1 end