From 5c57e266520cbb25877bcaea1fd126f3e930217c Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Tue, 24 Nov 2020 21:20:28 +0100 Subject: [PATCH 01/10] Add btv gc NUMBER command --- btv | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/btv b/btv index cff65e4..85646c9 100755 --- a/btv +++ b/btv @@ -327,7 +327,6 @@ def do_create(args): ## do optional processing ## if snapshot.rank == 2: - ## snapshot serialization # if there's a previous Rank 2 snapshot, compute a diff against it # if not or if the process fails, demote this snap to Rank 1 @@ -493,23 +492,42 @@ def do_gc(args=None): Drops old snapshots. If the only arg is "greedy", drops ALL snapshots except the youngest - Rank 2. + Rank 2. If it's a number, drops that many oldest snapshots. """ - if args and "greedy" in args: - newest = list_snapshots(2)[-1] - - if newest: - print(">>> Dropping all snapshots except the newest Rank 2 in 5 s...") - time.sleep(5) + if args: + if args[0] == "greedy": + newest = list_snapshots(2)[-1] - for snap in list_snapshots(): - if snap.name != newest.name: - snap.drop() + if newest: + print(">>> Dropping all snapshots except the newest Rank 2 in 5 s...") + time.sleep(5) + + for snap in list_snapshots(): + if snap.name != newest.name: + snap.drop() + + else: + print("!!! no Rank 2 snapshot exists") + sys.exit(1) else: - print("!!! no Rank 2 snapshot exists") - sys.exit(1) + try: + count = int(args[0]) + except ValueError: + print("!!! %s is not a count of snapshots to delete" %(args[0])) + sys.exit(1) + + snaps = list_snapshots()[:count] + + for snap in snaps: + print(" %d %s %s" %(snap.rank, snap.name, ", ".join(snap.notes))) + + print(">>> These snapshots will be dropped in 5 s...") + time.sleep(5) + + for snap in snaps: + snap.drop() else: counts = [0, 0, 0] # Rank 0, 1, 2 @@ -565,6 +583,9 @@ Verbs: gc greedy Drop ALL snapshots except the newest Rank 2. + + gc COUNT + Drop COUNT oldest snapshots. """.format(cmd=sys.argv[0])) if __name__ == "__main__": From 00e3baf117a5f12fefc311fd6f273e6a4e4dc369 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Thu, 18 Feb 2021 01:41:21 +0100 Subject: [PATCH 02/10] add telemetry --- btv | 16 ++++++++++++++++ cfg/config.ini | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/btv b/btv index 85646c9..1bf9113 100755 --- a/btv +++ b/btv @@ -10,6 +10,8 @@ import os import shlex import sys import time +import socket +import urllib.request CONFIG = "/etc/btv/config.ini" LOCKFILE = "/run/lock/btv/serialization.lock" @@ -259,6 +261,12 @@ def serialize(snap, outdir, key, snap_from=None): return 0 +def ping(url): + try: + urllib.request.urlopen(url, timeout=10) + except socket.error as e: + print("Ping failed: %s" %(e)) + # ------------------------------------------------------------------------------ # Verbs @@ -306,6 +314,10 @@ def do_create(args): elif snaps_since_rank_1 >= cfg.getint("snap", "rank_1_interval"): snapshot.rank = 1 + ping_url = cfg.get("monitoring", "rank2_start_url") + if snapshot.rank == 2 and ping_url: + ping(ping_url) + ## create the snapshot itself ## os.makedirs(snapshot.path) @@ -358,6 +370,10 @@ def do_create(args): ## garbage collection do_gc() + + ping_url = cfg.get("monitoring", "rank2_end_url") + if snapshot.rank == 2 and ping_url: + ping(ping_url) def do_list(args): """ diff --git a/cfg/config.ini b/cfg/config.ini index cb00390..f3dc5a9 100644 --- a/cfg/config.ini +++ b/cfg/config.ini @@ -35,3 +35,10 @@ dir = /mnt/pool/subvol/_backup rank_0_count=25 rank_1_count=36 rank_2_count=42 + +[monitoring] +# GETs this URL before starting a rank2 snapdhot +rank2_start_url = + +# GETs this URL after successfully completing a rank2 snapshot +rank2_end_url = From b96c163c8f06ec338185c9698e9fd66490fa8319 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 30 May 2021 23:40:51 +0200 Subject: [PATCH 03/10] chmod rank2 files read-only for owner and no-access for everyone else --- btv | 3 +++ 1 file changed, 3 insertions(+) diff --git a/btv b/btv index 1bf9113..c84665e 100755 --- a/btv +++ b/btv @@ -258,6 +258,9 @@ def serialize(snap, outdir, key, snap_from=None): for file in os.listdir(directory): path = os.path.join(directory, file) os.chown(path, outdir_stat.st_uid, outdir_stat.st_gid) + + if path.endswith(".aes") or path.endswith(".sha512"): + os.chmod(path, 0o100) return 0 From 7a51d286f29b5a6ec69e07f58afb9e2fc4add0b0 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 30 May 2021 23:46:13 +0200 Subject: [PATCH 04/10] fix a stupid mistake from the previous commit --- btv | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/btv b/btv index c84665e..1de3c82 100755 --- a/btv +++ b/btv @@ -249,7 +249,7 @@ def serialize(snap, outdir, key, snap_from=None): ## add a self-check executable with open(os.path.join(directory, "check-integrity.sh"), "w") as f: f.write("#! /bin/sh\n\nsha512sum --check manifest.sha512\n") - os.chmod(f.name, 0o555) + os.chmod(f.name, 0o500) ## fix permissions and ownership of created objects outdir_stat = os.stat(outdir) @@ -260,7 +260,9 @@ def serialize(snap, outdir, key, snap_from=None): os.chown(path, outdir_stat.st_uid, outdir_stat.st_gid) if path.endswith(".aes") or path.endswith(".sha512"): - os.chmod(path, 0o100) + os.chmod(path, 0o400) + + return 0 From 71886bee68b11ff0433713087ecb4bc343146b9d Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 30 May 2021 23:53:47 +0200 Subject: [PATCH 05/10] chmod rank2 dir as well --- btv | 1 + 1 file changed, 1 insertion(+) diff --git a/btv b/btv index 1de3c82..1f9a5c1 100755 --- a/btv +++ b/btv @@ -254,6 +254,7 @@ def serialize(snap, outdir, key, snap_from=None): ## fix permissions and ownership of created objects outdir_stat = os.stat(outdir) os.chown(directory, outdir_stat.st_uid, outdir_stat.st_gid) + os.chmod(directory, 0o500) for file in os.listdir(directory): path = os.path.join(directory, file) From 8345eb3217f00633f8d80c9869cae4a1e117c545 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Mon, 31 May 2021 12:08:53 +0200 Subject: [PATCH 06/10] chmod dirs rwx instead of r-x so they can be moved --- btv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btv b/btv index 1f9a5c1..d2348b1 100755 --- a/btv +++ b/btv @@ -254,7 +254,7 @@ def serialize(snap, outdir, key, snap_from=None): ## fix permissions and ownership of created objects outdir_stat = os.stat(outdir) os.chown(directory, outdir_stat.st_uid, outdir_stat.st_gid) - os.chmod(directory, 0o500) + os.chmod(directory, 0o700) for file in os.listdir(directory): path = os.path.join(directory, file) From 831d9ae9a292df58cc240562675600e368eec1db Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 1 Aug 2021 09:36:19 +0200 Subject: [PATCH 07/10] add unpack script to every snapshot --- btv | 6 +++++- unpack.sh | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 unpack.sh diff --git a/btv b/btv index d2348b1..5646eff 100755 --- a/btv +++ b/btv @@ -8,6 +8,7 @@ import datetime import json import os import shlex +import shutil import sys import time import socket @@ -246,11 +247,14 @@ def serialize(snap, outdir, key, snap_from=None): ## final touches ## - ## add a self-check executable + ## add self-check and unpack executables with open(os.path.join(directory, "check-integrity.sh"), "w") as f: f.write("#! /bin/sh\n\nsha512sum --check manifest.sha512\n") os.chmod(f.name, 0o500) + shutil.copy("/usr/share/btv/unpack.sh", "unpack.sh") + os.chmod("unpack.sh", 0o500) + ## fix permissions and ownership of created objects outdir_stat = os.stat(outdir) os.chown(directory, outdir_stat.st_uid, outdir_stat.st_gid) diff --git a/unpack.sh b/unpack.sh new file mode 100644 index 0000000..f78a8ba --- /dev/null +++ b/unpack.sh @@ -0,0 +1,24 @@ +#! /bin/zsh + +TIMESTAMP=($(basename "$(pwd)")) +OUTDIR="$1" +KEYFILE="$2" + +function die { + >&2 echo "$2" + exit $1 +} + +[[ "$0" != "./unpack.sh" ]] && die 1 "This can only be executed from the snapshot directory itself." +[[ ! -d "$OUTDIR" ]] && die 1 "The first argument must be a directory to unpack subvolumes into." +[[ ! -f "$KEYFILE" ]] && die 1 "The second argument must be a readable keyfile." +./check-integrity.sh || die 2 "This snapshot failed integrity checks." + +### end of checks + +for ARCHIVE in *btrfs.zst.aes +do + openssl enc -d -aes-256-cbc -pbkdf2 -salt -pass "file:$KEYFILE" < "$ARCHIVE" | zstd -d | btrfs receive "$OUTDIR" || die 3 "Failed to unpack subvolume." + SUBVOL_NAME=${ARCHIVE%%.btrfs.zst.aes} + mv "${OUTDIR}/${SUBVOL_NAME}" "${OUTDIR}/${SUBVOL_NAME}.${TIMESTAMP[1]}" || die 4 "Failed to rename subvolume." +done From 63906d328f7f2c2f99d98b7d56ceee4d7a127a4f Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 1 Aug 2021 09:56:32 +0200 Subject: [PATCH 08/10] fix unpack.sh directory --- btv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btv b/btv index 5646eff..85701d2 100755 --- a/btv +++ b/btv @@ -252,7 +252,7 @@ def serialize(snap, outdir, key, snap_from=None): f.write("#! /bin/sh\n\nsha512sum --check manifest.sha512\n") os.chmod(f.name, 0o500) - shutil.copy("/usr/share/btv/unpack.sh", "unpack.sh") + shutil.copy("/usr/share/btv/unpack.sh", os.path.join(directory, "unpack.sh")) os.chmod("unpack.sh", 0o500) ## fix permissions and ownership of created objects From bbd568260aa6e5fd5ba91dea0032f55f1b523856 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 1 Aug 2021 10:14:40 +0200 Subject: [PATCH 09/10] fix unpack.sh chmod --- btv | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/btv b/btv index 85701d2..fd3ea57 100755 --- a/btv +++ b/btv @@ -252,8 +252,9 @@ def serialize(snap, outdir, key, snap_from=None): f.write("#! /bin/sh\n\nsha512sum --check manifest.sha512\n") os.chmod(f.name, 0o500) - shutil.copy("/usr/share/btv/unpack.sh", os.path.join(directory, "unpack.sh")) - os.chmod("unpack.sh", 0o500) + unpack_path = os.path.join(directory, "unpack.sh") + shutil.copy("/usr/share/btv/unpack.sh", unpack_path) + os.chmod(unpack_path, 0o500) ## fix permissions and ownership of created objects outdir_stat = os.stat(outdir) From 83bac5876dc98dc46f8b4c02c06f718d8521e45a Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Sun, 15 Jan 2023 18:53:52 +0100 Subject: [PATCH 10/10] simplify shapshot naming --- btv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/btv b/btv index fd3ea57..206c75c 100755 --- a/btv +++ b/btv @@ -199,9 +199,9 @@ def serialize(snap, outdir, key, snap_from=None): ## prepare directories ## if snap_from: - name = "%s diff from %s" %(snap.name, snap_from.name) + name = "%s to %s" %(snap_from.name, snap.name) else: - name = "%s full" %(snap.name) + name = snap.name directory = os.path.join(outdir, name) os.makedirs(directory)