#!/bin/bash # Copyright (C) 2016 Ian Kelling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # usage: $0 SUBVOL_MOUNTPOINT... # if latest subvols $@ are not mounted, exit 1, print message, and touch /nocow/btrfs-stale/$subvol [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR shopt -s nullglob if [[ ! $@ ]]; then echo "$0: error: expected mountpoint argument" fi 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 2>/dev/null | 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