btrfish/backup.fish

88 lines
1.9 KiB
Fish
Raw Normal View History

2016-05-05 03:48:13 +02:00
#!/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
if test -d $safepath
2016-06-08 18:05:56 +02:00
if not test -f $mount/.btrfish-lock
sudo btrfs subvolume snapshot -r $mount $safepath-new
2016-05-05 03:48:13 +02:00
2016-06-08 18:05:56 +02:00
sudo touch $mount/.btrfish-lock
set snapshots $snapshots $safepath
set pairs $pairs $pair
else
echo "$mount/.btrfish-lock exists, skipping..."
end
else
echo $mount "is not bootstrapped and will not be included in the backups!"
end
2016-05-05 03:48:13 +02:00
end
echo "Syncing..."
sync
echo "Sending to target..."
for snapshot in $snapshots
sudo btrfs send -p $snapshot $snapshot-new | receive-on-target
2017-03-03 23:16:06 +01:00
while [ $status != 0 ]
echo "Sending failed, retrying..."
sleep 5
sudo btrfs send -p $snapshot $snapshot-new | receive-on-target
end
2016-05-05 03:48:13 +02:00
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..."
2016-06-08 18:05:56 +02:00
for pair in $pairs
2016-05-05 03:48:13 +02:00
set subvol (clean-name (get-subvol $pair))
set mount (get-mount $pair)
if not test -f $mount/.btrfish-keepone
rename-target-subvolume $subvol $subvol.(date +%Y-%m-%d)
2017-03-03 23:16:06 +01:00
while [ $status != 0 ]
echo "Renaming failed, retrying..."
sleep 5
rename-target-subvolume $subvol $subvol.(date +%Y-%m-%d)
end
else
delete-target-subvolume $subvol
2017-03-03 23:16:06 +01:00
while [ $status != 0 ]
echo "Deleting failed, retrying..."
sleep 5
delete-target-subvolume $subvol
end
end
2016-05-05 03:48:13 +02:00
2016-06-08 18:05:56 +02:00
rename-target-subvolume $subvol-new $subvol
2017-03-03 23:16:06 +01:00
while [ $status != 0 ]
echo "Renaming failed, retrying..."
sleep 5
rename-target-subvolume $subvol-new $subvol
end
2016-06-08 18:05:56 +02:00
sudo rm $mount/.btrfish-lock
2016-05-05 03:48:13 +02:00
end
echo "Done!"