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
}
# duplicated in mount-latest-sub
+# Reassign $1 var from /dev/dm- to corresponding /dev/mapper/
mapper-dev() {
local mapdev
local -n devref=$1
done
fi
}
-
+tmpf=$(mktemp)
+d tmpf=$tmpf
for d; do
+
if $subvol_path; then
svp=$d
root_dir=${d%/*}
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
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
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
stale-file
done
+rm $tmpf