X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=check-subvol-stale;h=5ae7cb6b52996305e2eed53aa731777eff455d66;hb=51c8b40fd2aac71d29dc9298ca65425725ad1edd;hp=a3d0149e24fa574ccec2d62a5e427db81935851e;hpb=7b47d6a266340223e78317cfe0570868f45a4cad;p=distro-setup diff --git a/check-subvol-stale b/check-subvol-stale index a3d0149..5ae7cb6 100644 --- a/check-subvol-stale +++ b/check-subvol-stale @@ -16,7 +16,7 @@ [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@" -source /usr/local/lib/err +source /usr/local/lib/bash-bear shopt -s nullglob @@ -27,8 +27,9 @@ usage: $0 SUBVOL_MOUNTPOINT... | -p SUBVOL_PATH... In git this is not not executable because it's meant to be installed using ./install-my-scripts -If latest subvols \$@ are not mounted, print a message, and print -the unstale subvol name into /nocow/btrfs-stale/\$subvol +Print the unstale subvol name into /nocow/btrfs-stale/\$subvol + +If latest subvols \$@ are not mounted, print a message to terminal. Fresh is opposite of stale. To be fresh, either SUBVOL_MOUNTPOINT is a snapshot of the latest, or the latest snapshot is snapshot of @@ -76,13 +77,16 @@ stale-file() { fi } +pre="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}" + d() { if $verbose; then - printf "%s\n" "$*" + printf "$pre %s\n" "$*" fi } # duplicated in mount-latest-sub +# Reassign $1 var from /dev/dm- to corresponding /dev/mapper/ mapper-dev() { local mapdev local -n devref=$1 @@ -95,8 +99,10 @@ mapper-dev() { done fi } - +tmpf=$(mktemp) +d tmpf=$tmpf for d; do + if $subvol_path; then svp=$d root_dir=${d%/*} @@ -140,14 +146,55 @@ for d; do d "svp=$svp # subvolume path" fi - snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century. - if [[ ! ${snaps[*]} ]]; then + # note: relying on null glob + ls_args=($root_dir/btrbk/$subvol_dir.20*) + if (( ${#ls_args[@]} )); then + # Assumes we are in the 21st century. + ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf + mapfile -t snaps <$tmpf + else # no snapshots yet # TODO: make this an error and override with a cli flag echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume" continue fi + # last_snap by date. + last_snap="${snaps[0]}" + + case $last_snap in + $root_dir/btrbk/$subvol_dir.20*) : ;; + *) + echo "$0: error: unexpected last_snap:$last_snap" + exit 1 + ;; + esac + + d last_snap=$last_snap + ## alternate slower alternative which would not rely on ls sorting: + # last_snap=$( + # for s in ${snaps[@]}; do + # f=${s##*/} + # unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) + # printf "%s %s\n" $unix_time $s # part of the pipeline + # # sort will fail + # done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]] + # ) + # if [[ ! $last_snap ]]; then + # # should not happen. + # echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2 + # exit 1 + # fi + + if [[ ! -e $svp ]]; then + echo "$0: warning: subvol does not exist: $svp" + echo "$0 assuming this host was just for receiving and latest snap is freshest" + freshest_snap=$last_snap + stale=true + stale-file + continue + fi + # get info on last received sub last_received= last_received_cgen=0 @@ -159,39 +206,17 @@ for d; do if [[ $cgen -gt $last_received_cgen ]]; then last_received_cgen=$cgen last_received=$f + elif [[ $last_received ]]; then + # optimization: we are looking in reverse order by date, so if + # we find one that has a lesser cgen, assume the rest will all + # be lesser. + break fi fi done d last_received_cgen=$last_received_cgen d last_received=$last_received - # Get last_snap by date. - # when a btrbk bugfix makes it into the distro, - # we might replace this with btrbk list latest /mnt/root/$vol | ... - last_snap=$( - for s in ${snaps[@]}; do - f=${s##*/} - unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) - printf "%s %s\n" $unix_time $s # part of the pipeline - # sort will fail - done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]] - ) - if [[ ! $last_snap ]]; then - # should not happen. - echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2 - exit 1 - fi - d last_snap=$last_snap - - if [[ ! -e $svp ]]; then - echo "$0: warning: subvol does not exist: $svp" - echo "$0 assuming this host was just for receiving and latest snap is freshest" - freshest_snap=$last_snap - stale=true - stale-file - continue - fi - # if there is a last_received, we can assume stale or fresh if we are newer/older if [[ $last_received ]]; then @@ -213,7 +238,7 @@ for d; do stale=true # fresh if $svp has $last_snap as a snapshot, if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \ - grep -xF ${last_snap#$root_dir/} ; then + grep -xF ${last_snap#"$root_dir"/} >/dev/null; then stale=false else # or else $svp is a snapshot of $last_snap. we use a uuid # comparison, which if I remember from the docs, is a bit more @@ -226,3 +251,4 @@ for d; do stale-file done +rm $tmpf