diff --git a/bash/bingus-bak/bak.sh b/bash/bingus-bak/bak.sh new file mode 100755 index 0000000..c475bc5 --- /dev/null +++ b/bash/bingus-bak/bak.sh @@ -0,0 +1,42 @@ +#!/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 + diff --git a/bash/bingus-bak/restore.sh b/bash/bingus-bak/restore.sh new file mode 100755 index 0000000..d2da4ff --- /dev/null +++ b/bash/bingus-bak/restore.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -eo pipefail + +if [[ -e restoration ]]; then + echo 'Error: an operation is already ongoing' >&2 + exit 1 +fi + +mkdir restoration + +echo "restoring baks/epoch" >&2 +sudo btrfs receive restoration -f baks/epoch + +find baks -maxdepth 1 -type f | sort | head -n-1 | while read -r file; do + echo "restoring $file" >&2 + mv restoration/{current,last} + sudo btrfs receive restoration -f "$file" + sudo btrfs subvol delete restoration/last +done + +echo "restoration complete" >&2