add btrbk.lib for btrfs snapshosta and backup

master
David Kebler 2023-03-06 12:01:02 -08:00
parent 8a6929b523
commit c374b03061
1 changed files with 149 additions and 0 deletions

149
modules/btrbk.lib Normal file
View File

@ -0,0 +1,149 @@
#!/bin/bash
# dry run by default
# >btrbk_run <conf name> <btrbk run options>
# with -e exectue
# >btrbk_run -e <conf name> <btrbk run options>
# to only create the links
# >btrbk_run -e <conf name> -n
btrbk_scripts_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
# will try to find a conf file with out without .conf extension in a few places
btrbk_conf () {
local file=${1:-btrbk.conf}
[[ -f $file ]] && echo $file && return
[[ -f $file.conf ]] && echo $file.conf && return
BTRBK_CONF_DIR=${BTRBK_CONF_DIR:-/snapshots/conf}
# echo $btrbk_scripts_dir
# echo $BTRBK_CONF_DIR
[[ -f $BTRBK_CONF_DIR/$file ]] && echo $BTRBK_CONF_DIR/$file && return
[[ -f $BTRBK_CONF_DIR/$file.conf ]] && echo $BTRBK_CONF_DIR/$file.conf && return
[[ -f /etc/btrbk/$file ]] && echo /etc/btrbk/$file && return
[[ -f /etc/btrbk/$file.conf ]] && echo /etc/btrbk/$file.conf && return
[[ -f /etc/btrbk.conf ]] && echo /etc/btrbk.conf && return
return 1
}
btrbk_run () {
local dry_run="-n"
local dry_run_echo="echo"
[[ $1 == "-e" ]] && dry_run="" && dry_run_echo="" && shift
local file=$(btrbk_conf $1)
[[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1
shift
local src=$(sudo btrbk -c $file list config --format col:h:source_subvolume)
local snaps=$(sudo btrbk -c $file list source --format col:h:snapshot_name)
local dests=$(sudo btrbk -c $file list config --format col:h:snapshot_path | tail -1)
local dests+=" $(btrbk -c $file list target --format col:h:target_path)"
# any pre snap tasks
for dest in $dests; do
if [[ ! -d $dest ]]; then
if confirm directory $dest does not exist, create; then
sudo mkdir -p $dest
else
echo destination $dest directory MUST exist aborting brtbk run
return 2
fi
fi
done
echo using configuration file: $file
echo creating snapshots: $snaps
echo from $src
echo at these destinations $dests
echo additional passed arguments: $@
# so snaps
[[ $dry_run ]] && echo backup dry run || echo Taking snapshost and making backups now...
sudo btrbk -c $file run $dry_run --progress $@
local latest=$(sudo btrbk -c $file list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
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 $@
latest_links () {
btrbk_run -e $@ -n
}
latest_clone () {
local src=$(realpath $1)
local dest=$(realpath $2)
shift 2
module_load confirm
if [[ ! -d $dest ]]; then
if confirm "destination directory $dest does not exist, create it"; then
sudo mkdir -p $dest
else
return 1
fi
fi
local snaps=$(ls $src | grep latest | sed 's/.\{1\}$//' | xargs -I % realpath $src/%)
[[ ! $snaps ]] && echo no latest snapshots in $src && ls -la $src && return 1
if confirm create snapshots for $snaps in $dest; then
local destsnap
for snap in $snaps; do
destsnap="$dest/$(basename $snap | cut -f 1 -d '.' )"
if [[ -d $destsnap ]]; then
if confirm -s snapshot $destsnap already exists do you want to over write it; then
sudo btrfs subvolume delete $destsnap
else
return 2
fi
fi
sudo btrfs subvolume snapshot $@ $snap $destsnap
done
echo $dest
ls -la $dest
fi
}
# use snapshot to then snap latest to another location, have it edit the fstab file
# #!/bin/bash
# [[ $# -lt 2 ]] && echo "need to supply a <root instance dir> and a snapshot name" && exit
# # echo sed 's/$name/'$name'/g' named.conf.tmpl > $subvoldir-$name.conf
# # echo sed -i 's/$subvoldir/'$subvoldir'/g' $subvoldir-$name.conf
# named="$2"
# src=/mnt/linuxpart
# dest=/mnt/linuxpart
# echo sudo mkdir -p $dest/$named
# echo sudo btrfs subvolume snapshot $src/$1/@root $dest/$named/@root
# echo sudo btrfs subvolume snapshot $src/$1/@opt $dest/$named/@opt
# echo sudo btrfs subvolume snapshot $src/$1/@home $dest/$named/@home
# echo sudo btrfs subvolume snapshot $src/@shell $dest/$named/@shell
# sudo mkdir -p $dest/$named
# sudo btrfs subvolume snapshot $src/$1/@root $dest/$named/@root
# sudo btrfs subvolume snapshot $src/$1/@opt $dest/$named/@opt
# sudo btrfs subvolume snapshot $src/$1/@home $dest/$named/@home
# sudo btrfs subvolume snapshot $src/@shell $dest/$named/@shell
# echo to edit: bfs_vscode $dest/$named