Update btrbk.lib separated making latest links to function, add -n to link command

master
David Kebler 2023-03-07 10:26:18 -08:00
parent c374b03061
commit 15572b297c
1 changed files with 53 additions and 25 deletions

View File

@ -25,17 +25,62 @@ btrbk_conf () {
return 1 return 1
} }
btrbk_run () { btrbk_src () {
local file=$(btrbk_conf $1)
[[ ! $file ]] && return 1
sudo btrbk -c $file list config --format col:h:source_subvolume
}
local dry_run="-n" btrbk_dests () {
local dry_run_echo="echo" local file=$(btrbk_conf $1)
[[ $1 == "-e" ]] && dry_run="" && dry_run_echo="" && shift [[ ! $file ]] && return 1
local dests=$(sudo btrbk -c $file list config --format col:h:snapshot_path | tail -1)
dests+=" $(sudo btrbk -c $file list target --format col:h:target_path)"
echo $dests
}
btrbk_latest_links () {
local dry_run="echo"
[[ $1 == "-e" ]] && dry_run="" && shift
local file=$(btrbk_conf $1) local file=$(btrbk_conf $1)
[[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1 [[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1
shift shift
local src=$(sudo btrbk -c $file list config --format col:h:source_subvolume) local latest=$(sudo btrbk -c $file list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
[[ ( ! $latest ) || $latest == "-" ]] && echo "no latest snapshots so can't make .latest links" && return 3
local dests=$(btrbk_dests $file)
if [[ $dry_run ]]; then
echo dry run for making latest links for following destinations, NOTE: use -e to actually make them
echo $dests
echo "-------------------"
fi
echo making .latest symlinks in each destination
for dest in $dests; do
for snap in $latest; do
# [[ -e $dest/${snap%%.*}.latest ]] && $dry_run_echo sudo rm $dest/${snap%%.*}.latest
local cmd="sudo ln -srfn $dest/$snap $dest/${snap%%.*}.latest"
echo $cmd
[[ ! $dry_run ]] && $cmd
done
done
}
btrbk_run () {
local dry_run="-n"
local dry_run_echo="echo"
[[ $1 == "-e" ]] && dry_run="" && shift
local file=$(btrbk_conf $1)
[[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1
shift
local src=$(btrbk_src $file)
local snaps=$(sudo btrbk -c $file list source --format col:h:snapshot_name) local snaps=$(sudo btrbk -c $file list source --format col:h:snapshot_name)
@ -60,35 +105,18 @@ echo from $src
echo at these destinations $dests echo at these destinations $dests
echo additional passed arguments: $@ echo additional passed arguments: $@
# so snaps # do snaps and backups
[[ $dry_run ]] && echo backup dry run || echo Taking snapshost and making backups now... [[ $dry_run ]] && echo backup dry run || echo Taking snapshost and making backups now...
sudo btrbk -c $file run $dry_run --progress $@ sudo btrbk -c $file run $dry_run --progress $@
# post snap/backup
local latest=$(sudo btrbk -c $file list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D') btrbk_latest_links $([[ $dry_run ]] || printf "%s" -e) $file
if [[ $dry_run ]]; then
echo Latest Snaphosts: $latest
fi
[[ ( ! $latest ) || $latest == "-" ]] && echo "no latest snapshots so can't make .latest links" && return 3
# post snap tasks
echo making .latest symlinks in each destination
for dest in $dests; do
for snap in $latest; do
[[ -e $dest/${snap%%.*}.latest ]] && $dry_run_echo sudo rm $dest/${snap%%.*}.latest
$dry_run_echo sudo ln -sr $dest/$snap $dest/${snap%%.*}.latest
done
done
} }
(return 0 2>/dev/null) || btrbk_run $@ (return 0 2>/dev/null) || btrbk_run $@
latest_links () {
btrbk_run -e $@ -n
}
latest_clone () { latest_clone () {