#!/bin/bash [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR shopt -s nullglob ret=0 for d; do vol=${d##*/} cd /mnt/root/btrbk snaps=($vol.20*) if [[ ! $snaps ]]; then # no snapshots yet continue fi # when a btrbk bugfix makes it into the distro, # we might replace this with btrbk list latest /mnt/root/$vol | ... # note: this is duplicated in mount-latest-subvol last_snap=$( for f in ${snaps[@]}; do printf "%s %s\n" $(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) $f done | sort -r | head -n 1 | awk '{print $2}' ) if [[ ! $last_snap ]]; then echo "$d stale" ret=1 continue fi stale=true if btrfs sub show $d|sed '0,/^\t*Snapshot(s):/d;s/^\s*//' | \ grep -xF btrbk/$last_snap &>/dev/null; then stale=false else last_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}') if btrfs sub show $d| grep "^\s*Parent UUID:\s*$last_uuid$" &>/dev/null; then stale=false fi fi stale_dir=/nocow/btrfs-stale stale_file=$stale_dir/$vol if $stale; then mkdir -p $stale_dir printf "%s\n" $last_snap > $stale_file echo "$d stale" ret=1 continue else rm -f $stale_file fi done exit $ret # todo: figure out what to do when there are no # snapshots yet. I guess that should be # yes to being stale? see the implications # in the other script.