feat: add snapshot locking via chattr +i

the snapshot might get deleted from underneath us so lock it by setting
the immutable bit on the snapshot; note we don't need a recursive lock
because the snapshot already is read-only, we just need to prevent it
from getting pruned by 'btrfs subvolume delete'
This commit is contained in:
surtur 2021-05-03 15:23:51 +02:00
parent e4eed494f1
commit 6ca8343202
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 16 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# btrfs-sync
> **Note**: This fork simply adds in-transfer `zstd` compression support
> **Note**: This fork simply adds in-transfer `zstd` compression support and snapshot locking capability (see [Features](#features))
Smart and easy sync of BTRFS snapshots, locally or through SSH.
@ -35,6 +35,7 @@ Delete subvolume (no-commit): '/media/USBdrive/home-snapshots/hourly_2018-03-08_
- Simple syntax
- Optional progress indication (using `pv`)
- Support for `zstd`, `xz` or `pbzip2` compression in order to save bandwidth.
- Snapshot locking via `chattr +i` while sync (`btrfs-send`) is in progress to prevent snapshot deletion, namely by `btrfs subvolume delete ...` invoked e.g as part of `btrfs-snp`
- Retention policy
- Automatic incremental synchronization
- Cron friendly
@ -114,4 +115,4 @@ chmod +x /etc/cron.daily/btrfs-sync
Original work by *nachoparker* of [ownyourbits.com](https://ownyourbits.com/2018/03/09/easy-sync-of-btrfs-snapshots-with-btrfs-sync/)
zstd compression support added by wanderer of [dotya.ml](https://git.dotya.ml/wanderer)
zstd compression support and snapshot locking capability added by wanderer of [dotya.ml](https://git.dotya.ml/wanderer)

View File

@ -298,11 +298,24 @@ sync_snapshot() {
echov "* Check destination"
get_dst_snapshots "$DST" # sets DSTS DST_UUIDS
for src in "${SRCS[@]}"; do
# the snapshot might get deleted from underneath us so lock it by setting
# the immutable bit on the snapshot; note we don't need a recursive lock
# because the snapshot already is read-only, we just need to prevent it from
# getting pruned by 'btrfs subvolume delete'
echov "* Locking '$src'"
chattr +i "$src"
sync_snapshot "$src" && RET=0 || RET=1
# unlock after we're done
echov "* Freeing lock on '$src'"
chattr -i "$src"
for i in $(seq 1 2); do
[[ "$RET" != "1" ]] && break
echo "* Retrying '$src'..."
echov "* Locking '$src'"
chattr +i "$src"
sync_snapshot "$src" && RET=0 || RET=1
echov "* Freeing lock on '$src'"
chattr -i "$src"
done
[[ "$RET" == "1" ]] && { echo "Abort"; exit 1; }
done