btrfish/backup.fish

88 lines
1.9 KiB
Fish
Executable File

#!/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
if not test -f $mount/.btrfish-lock
sudo btrfs subvolume snapshot -r $mount $safepath-new
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
end
echo "Syncing..."
sync
echo "Sending to target..."
for snapshot in $snapshots
sudo btrfs send -p $snapshot $snapshot-new | receive-on-target
while [ $status != 0 ]
echo "Sending failed, retrying..."
sleep 5
sudo btrfs send -p $snapshot $snapshot-new | receive-on-target
end
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 $pairs
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)
while [ $status != 0 ]
echo "Renaming failed, retrying..."
sleep 5
rename-target-subvolume $subvol $subvol.(date +%Y-%m-%d)
end
else
delete-target-subvolume $subvol
while [ $status != 0 ]
echo "Deleting failed, retrying..."
sleep 5
delete-target-subvolume $subvol
end
end
rename-target-subvolume $subvol-new $subvol
while [ $status != 0 ]
echo "Renaming failed, retrying..."
sleep 5
rename-target-subvolume $subvol-new $subvol
end
sudo rm $mount/.btrfish-lock
end
echo "Done!"