2 # Copyright (C) 2016 Ian Kelling
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # usage: $0 SUBVOL_MOUNTPOINT...
18 # In git, this is not not executable because it's meant to be installed
19 # using ./install-my-scripts
21 # If latest subvols $@ are not mounted, exit 1, print message, and touch
22 # /nocow/btrfs-stale/$subvol
24 # Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
25 # the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
27 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
30 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
35 echo "$0: error: expected mountpoint argument"
42 snaps
=($vol.20*) # Assumes we are in the 21st century.
43 if [[ ! $snaps ]]; then
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
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}'
55 if [[ ! $last_snap ]]; then
57 echo "$0: error: could not find latest snapshot for $d among ${snaps[@]}"
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
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
76 # check if $d is a snapshot of any of the btrbk backups other than the latest
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
83 echo "$d stale: it's a snapshot of $f"
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 (( d_gen
< last_snap_gen
)); then
95 echo "$d stale: it's generation, $d_gen, is earlier than the last snapshot's, $last_snap_gen"
101 stale_dir
=/nocow
/btrfs-stale
102 stale_file
=$stale_dir/$vol
105 printf "%s\n" $last_snap > $stale_file