summaryrefslogtreecommitdiff
path: root/src/samizdat-daily-snapshot-root
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-06-19 18:26:54 -0400
committerAndrew Cady <d@jerkface.net>2019-06-19 20:04:53 -0400
commitddd28e45d507665ffd89b35e8c759b6add7686a6 (patch)
tree466e4b6f014f58dfb063e23f7cb59db9b6046a14 /src/samizdat-daily-snapshot-root
parent0b63dd60a8187fd3367049aa1ad6a250cbac19ec (diff)
add some commands to the live system
Diffstat (limited to 'src/samizdat-daily-snapshot-root')
-rwxr-xr-xsrc/samizdat-daily-snapshot-root55
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
4is_subvol()
5{
6 btrfs subvolume show "$1" >/dev/null 2>&1
7}
8
9btrfs_subvolumes()
10{
11 btrfs subvolume list -a -t "${1:-/}" | awk '{print $4}' | sed -Ene 's?^(<FS_TREE>/)\?root/?/?p'
12}
13
14snapit()
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
35do_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
47snapit /root
48
49btrfs_subvolumes | while read f
50do
51 if is_subvol "$f"
52 then
53 do_not_snap "$f" || snapit /root/"${f#/}"
54 fi
55done