diff options
author | Andrew Cady <d@jerkface.net> | 2019-06-19 18:26:54 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2019-06-19 20:04:53 -0400 |
commit | ddd28e45d507665ffd89b35e8c759b6add7686a6 (patch) | |
tree | 466e4b6f014f58dfb063e23f7cb59db9b6046a14 /src/samizdat-daily-snapshot-root | |
parent | 0b63dd60a8187fd3367049aa1ad6a250cbac19ec (diff) |
add some commands to the live system
Diffstat (limited to 'src/samizdat-daily-snapshot-root')
-rwxr-xr-x | src/samizdat-daily-snapshot-root | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/samizdat-daily-snapshot-root b/src/samizdat-daily-snapshot-root new file mode 100755 index 0000000..c57524c --- /dev/null +++ b/src/samizdat-daily-snapshot-root | |||
@@ -0,0 +1,55 @@ | |||
1 | #/bin/sh | ||
2 | [ "$(id -u)" = 0 ] || exec sudo "$0" "$@" || exit | ||
3 | |||
4 | is_subvol() | ||
5 | { | ||
6 | btrfs subvolume show "$1" >/dev/null 2>&1 | ||
7 | } | ||
8 | |||
9 | btrfs_subvolumes() | ||
10 | { | ||
11 | btrfs subvolume list -a -t "${1:-/}" | awk '{print $4}' | sed -Ene 's?^(<FS_TREE>/)\?root/?/?p' | ||
12 | } | ||
13 | |||
14 | snapit() | ||
15 | { | ||
16 | local dev mnt target | ||
17 | |||
18 | mnt=$(mktemp -d /tmp/mnt.XXXXXX) || return | ||
19 | src=$mnt/$1/ | ||
20 | target=$mnt/${2:-daily/}${1%/}~$(date -I) | ||
21 | dev=$(btrfs fi sh / | sed -ne 's/.* path //p' | tail -n1) || return | ||
22 | |||
23 | mount "$dev" -o subvol=/ "$mnt" | ||
24 | if [ -e "$target" ] | ||
25 | then | ||
26 | : | ||
27 | else | ||
28 | mkdir -p "$(dirname "$target")" | ||
29 | btrfs subvolume snapshot -r "$src" "$target" | ||
30 | fi | ||
31 | umount "$mnt" | ||
32 | rmdir "$mnt" | ||
33 | } | ||
34 | |||
35 | do_not_snap() | ||
36 | { | ||
37 | case "${1#/root/}" in | ||
38 | /var/cache/apt/archives|/home/*/.cache|/home/*/src/fsmgr/_build|/home/*/.stack|/home/*/.rustup) | ||
39 | true | ||
40 | ;; | ||
41 | *) | ||
42 | false | ||
43 | ;; | ||
44 | esac | ||
45 | } | ||
46 | |||
47 | snapit /root | ||
48 | |||
49 | btrfs_subvolumes | while read f | ||
50 | do | ||
51 | if is_subvol "$f" | ||
52 | then | ||
53 | do_not_snap "$f" || snapit /root/"${f#/}" | ||
54 | fi | ||
55 | done | ||