use btrfs-send for data syncing, testing still in progress
[distro-setup] / check-subvol-stale
1 #!/bin/bash
2
3 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
4
5 set -eE -o pipefail
6 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
7
8 shopt -s nullglob
9
10 ret=0
11 for d; do
12 vol=${d##*/}
13 cd /mnt/root/btrbk
14 snaps=($vol.20*)
15 if [[ ! $snaps ]]; then
16 # no snapshots yet
17 continue
18 fi
19 # when a btrbk bugfix makes it into the distro,
20 # we might replace this with btrbk list latest /mnt/root/$vol | ...
21 # note: this is duplicated in mount-latest-subvol
22 last_snap=$(
23 for f in ${snaps[@]}; do
24 printf "%s %s\n" $(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) $f
25 done | sort -r | head -n 1 | awk '{print $2}'
26 )
27 if [[ ! $last_snap ]]; then
28 echo "$d stale"
29 ret=1
30 continue
31 fi
32 stale=true
33 if btrfs sub show $d|sed '0,/^\t*Snapshot(s):/d;s/^\s*//' | \
34 grep -xF btrbk/$last_snap &>/dev/null; then
35 stale=false
36 else
37 last_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
38 if btrfs sub show $d| grep "^\s*Parent UUID:\s*$last_uuid$" &>/dev/null; then
39 stale=false
40 fi
41 fi
42 stale_dir=/nocow/btrfs-stale
43 stale_file=$stale_dir/$vol
44 if $stale; then
45 mkdir -p $stale_dir
46 printf "%s\n" $last_snap > $stale_file
47 echo "$d stale"
48 ret=1
49 continue
50 else
51 rm -f $stale_file
52 fi
53 done
54 exit $ret
55
56 # todo: figure out what to do when there are no
57 # snapshots yet. I guess that should be
58 # yes to being stale? see the implications
59 # in the other script.