X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=check-subvol-stale;h=2d00cbbda084f90ccb517970df310a9bd4def376;hb=08b99aa91f2bebb589df97db84e777de9fd28c99;hp=a3d0149e24fa574ccec2d62a5e427db81935851e;hpb=7b47d6a266340223e78317cfe0570868f45a4cad;p=distro-setup diff --git a/check-subvol-stale b/check-subvol-stale index a3d0149..2d00cbb 100644 --- a/check-subvol-stale +++ b/check-subvol-stale @@ -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 @@ -83,6 +84,7 @@ d() { } # 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 +97,10 @@ mapper-dev() { done fi } - +tmpf=$(mktemp) +d tmpf=$tmpf for d; do + if $subvol_path; then svp=$d root_dir=${d%/*} @@ -140,7 +144,9 @@ for d; do d "svp=$svp # subvolume path" fi - snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century. + # Assumes we are in the 21st century. + ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf + mapfile -t snaps <$tmpf if [[ ! ${snaps[*]} ]]; then # no snapshots yet # TODO: make this an error and override with a cli flag @@ -148,6 +154,33 @@ for d; do continue fi + # last_snap by date. + last_snap="${snaps[0]}" + ## 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 + 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 + # get info on last received sub last_received= last_received_cgen=0 @@ -159,39 +192,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 +224,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 +237,4 @@ for d; do stale-file done +rm $tmpf