various fixes
[distro-setup] / check-subvol-stale
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # usage: $0 SUBVOL_MOUNTPOINT...
17 #
18 # In git, this is not not executable because it's meant to be installed
19 # using ./install-my-scripts
20 #
21 # If latest subvols $@ are not mounted, exit 1, print message, and touch
22 # /nocow/btrfs-stale/$subvol
23 #
24 # Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
25 # the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
26
27 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
28
29 set -eE -o pipefail
30 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
31
32 shopt -s nullglob
33
34 if [[ ! $@ ]]; then
35 echo "$0: error: expected mountpoint argument"
36 fi
37
38 ret=0
39 for d; do
40 vol=${d##*/}
41 cd /mnt/root/btrbk
42 snaps=($vol.20*) # Assumes we are in the 21st century.
43 if [[ ! $snaps ]]; then
44 # no snapshots yet
45 continue
46 fi
47 # when a btrbk bugfix makes it into the distro,
48 # we might replace this with btrbk list latest /mnt/root/$vol | ...
49 # note: this is duplicated in mount-latest-subvol
50 last_snap=$(
51 for f in ${snaps[@]}; do
52 printf "%s %s\n" $(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) $f
53 done | sort -r | head -n 1 | awk '{print $2}'
54 )
55 if [[ ! $last_snap ]]; then
56 # should not happen.
57 echo "$0: error: could not find latest snapshot for $d among ${snaps[@]}"
58 ret=1
59 continue
60 fi
61
62 # check that $d has $last_snap as a snapshot,
63 # or else $d is a snapshot of $last_snap. In the second
64 # case, we use a uuid comparison, which if I remember from the
65 # docs, is a bit more robust, perhaps to renames.
66 if btrfs sub show $d 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
67 grep -xF btrbk/$last_snap &>/dev/null; then
68 stale=false
69 else
70 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
71 if btrfs sub show $d| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
72 stale=false
73 fi
74 fi
75
76 # check if $d is a snapshot of any of the btrbk backups
77 if [[ ! $stale ]]; then
78 for f in ${snaps[@]}; do
79 if [[ $f == $last_snap ]]; then continue; fi
80 uuid=$(btrfs sub show $f| awk '$1 == "UUID:" {print $2}')
81 if btrfs sub show $d| grep "^\s*Parent UUID:\s*$uuid$" &>/dev/null; then
82 stale=true
83 echo "$d stale: it's a snapshot of $f"
84 break
85 fi
86 done
87 fi
88 # check if $d generation is later than last_snap
89 if [[ ! $stale ]]; then
90 last_snap_gen=$(btrfs sub show $last_snap| awk '$1 == "Generation:" {print $2}')
91 d_gen=$(btrfs sub show $d| awk '$1 == "Generation:" {print $2}')
92 if (( last_snap_gen < d_gen )); then
93 stale=false
94 else
95 echo "$d stale: it's generation, $d_gen, is earlier than the last snapshot's, $last_snap_gen"
96 stale=true
97 fi
98 fi
99
100
101 stale_dir=/nocow/btrfs-stale
102 stale_file=$stale_dir/$vol
103 if $stale; then
104 mkdir -p $stale_dir
105 printf "%s\n" $last_snap > $stale_file
106 ret=1
107 continue
108 else
109 rm -f $stale_file
110 fi
111 done
112 exit $ret