snippets/bash/bingus-bak/bak.sh

42 lines
1.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eo pipefail
if ssh takyon sudo -u bingus test -e /opt/bingus-files/files_snapshots/current; then
echo 'Error: an operation is already ongoing' >&2
exit 1
fi
mkdir -p baks
ssh takyon sudo -u bingus btrfs subvol snapshot -r \
/opt/bingus-files/files \
/opt/bingus-files/files_snapshots/current
if ssh takyon sudo -u bingus test -e /opt/bingus-files/files_snapshots/last; then
echo "taking incremental backup" >&2
out=baks/"$(date --rfc-3339=seconds)"
ssh takyon sudo btrfs send \
-p /opt/bingus-files/files_snapshots/last \
/opt/bingus-files/files_snapshots/current \
--compressed-data > "$out"
du -h "$out"
echo "removing parent" >&2
ssh takyon sudo -u bingus btrfs property set -ts /opt/bingus-files/files_snapshots/last ro false
ssh takyon sudo -u bingus btrfs subvol delete /opt/bingus-files/files_snapshots/last
else
echo "taking first copy" >&2
ssh takyon sudo btrfs send \
/opt/bingus-files/files_snapshots/current \
--compressed-data > baks/epoch
du -h baks/epoch
fi
echo "renaming current to last" >&2
ssh takyon sudo -u bingus mv \
/opt/bingus-files/files_snapshots/current \
/opt/bingus-files/files_snapshots/last
echo "backup complete" >&2