added functions for ini file parsing and conversion to environment vars

fixed dir2subv functions
add subv_snap
fixed is_subv
This commit is contained in:
David Kebler 2025-01-02 17:23:00 -08:00
parent 56ab717402
commit be2edd19d8
2 changed files with 107 additions and 64 deletions

View file

@ -9,26 +9,40 @@ module_load path
is_subv () { is_subv () {
if [[ $1 == "-r" ]]; then if [[ $1 == "-r" ]]; then
if ! btrfs subvolume show "$2" 2> /dev/null | grep readonly &> /dev/null; then return 1; fi return $(btrfs subvolume show "$2" 2> /dev/null | grep readonly &> /dev/null)
else else
return $(btrfs subvolume show "$1" &> /dev/null) return $(btrfs subvolume show "$1" &> /dev/null)
fi
}
is_subvv () {
if is_subv "$@"; then
echo is a $([[ $1 == "-r" ]] && echo "read only") subvolume
else
echo is not a $([[ $1 == "-r" ]] && echo "read only") subvolume
return 1
fi fi
} }
# TODO: convert these into subcommands
is_subvv () { subv_snap () {
if [[ $1 == "-r" ]]; then local usesudo; local ro
if ! btrfs subvolume show "$2" 2> /dev/null | grep readonly &> /dev/null; then [[ $1 == "-r" ]] && ro=true && shift
echo not a readonly subvolume [[ $# -eq 0 ]] && echo no subvolume name passed && return 1
else [[ $# -eq 1 ]] && echo no snapshot path/name passed && return 2
echo readonly subvoluem [[ $EUID -ne 0 ]] && usesudo=sudo
fi if is_subv "$1"; then
else if [[ -v PS1 ]]; then
if btrfs subvolume show "$1" &> /dev/null; then confirm create snapshot of subvolume $(realpath "$1") to $(realpath "$2") || return 1
echo subvolume $usesudo "$BTRFS_BIN" subvolume snapshot $ro "$1" "$2"
else else
echo not a subvolume $usesudo "$BTRFS_BIN" subvolume snapshot $ro -q "$1" "$2"
fi fi
else
echo $1 not a subvolume, can not make a snapshot
return 3
fi fi
} }
@ -89,6 +103,7 @@ subv_del() {
subv_size () { subv_size () {
local all; local all;
[[ $1 == "-a" ]] && all=true [[ $1 == "-a" ]] && all=true
@ -118,13 +133,14 @@ echo "~ $((subvolumeFolderSize/1024/1024)) GB"
} }
# https://unix.stackexchange.com/questions/93324/how-does-this-find-command-using-find-exec-sh-c-sh-work # https://unix.stackexchange.com/questions/93324/how-does-this-find-command-using-find-exec-sh-c-sh-work
find_subvolumes() { subv_find() {
dir=${1:-.} local dir=${1:-.}
dep=${2:-1} local max="-maxdepth ${2:-1}"
sudo /bin/find "$dir" -mindepth 1 -maxdepth "$dep" -type d -exec bash -c ' [[ "$2" == "max" ]] && max=""
sudo /bin/find "$dir" -mindepth 1 $max -type d -exec sudo -E bash -c '
for d do for d do
subv=$(basename $d) lvl=$(/bin/btrfs subvolume show "$d" 2>/dev/null | grep "Top level ID:")
sudo '"$BTRFS_BIN"' subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv" [[ ${lvl##* } -gt 0 ]] && printf "%s\n" "$d"
done' find-sh {} + done' find-sh {} +
} }
@ -196,27 +212,37 @@ subv_xfer() {
$dr sudo "$BTRFS_BIN" sub snap -r "$src" "$tsnap" $dr sudo "$BTRFS_BIN" sub snap -r "$src" "$tsnap"
fi fi
if [[ $dr ]]; then if [[ $dr ]]; then
echo "sudo $BTRFS_BIN send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | btrfs receive $dest" echo "sudo $BTRFS_BIN send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | btrfs receive $dest"
echo "sudo mv $dest/$name.tmp $dest/$name$ext" echo "[[ ""$tsnap"" ]] && sudo $BTRFS_BIN sub del $tsnap"
echo "[[ ""$tsnap"" ]] && sudo $BTRFS_BIN sub del $tsnap" echo "sudo ""$BTRFS_BIN"" sub snap $dest/$name.tmp $dest/$name$ext"
echo "sudo ""$BTRFS_BIN"" sub snap $dest/$name.tmp $dest/$name$ext" echo "sudo ""$BTRFS_BIN"" sub del $dest/$name.tmp"
echo "sudo ""$BTRFS_BIN"" sub del $dest/$name.tmp" else
else # TODO: add mbuffer, show progress
# TODO: add mbuffer, show progress if is_subv "$dest"/"$name".tmp; then
sudo "$BTRFS_BIN" send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | sudo "$BTRFS_BIN" receive "$dest" echo destination temporary subvolume "$dest"/"$name".tmp exists. Deleting so xfer can proceed
# FIXME: take a snapshot of the destination .tmp then remove .tmp, otherwise it will be readonly subv_del "$dest"/"$name".tmp
# sudo mv "$dest"/"$name".tmp "$dest"/"$name"$ext fi
if is_subv -r "$dest"/"$name".tmp; then sudo "$BTRFS_BIN" send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | sudo "$BTRFS_BIN" receive "$dest"
sudo "$BTRFS_BIN" sub snap "$dest"/"$name".tmp "$dest"/"$name""$ext" if is_subv -r "$dest"/"$name".tmp; then
sudo "$BTRFS_BIN" sub del "$dest"/"$name".tmp echo transfer of "$name" to "$name""$ext" is complete, deleting source temporary snap "$tsnap"
echo transfer is complete [[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap"
fi if is_subv "$dest"/"$name""$ext"; then
[[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap" if ! confirm -s destination subvolume "$dest"/"$name""$ext" already exists do you want to overwrite it; then
sudo "$BTRFS_BIN" sub del "$dest"/"$name".tmp
ls -la "$dest" return 1
ls -la "$dest"/"$name""$ext" fi
fi fi
sudo "$BTRFS_BIN" sub snap "$dest"/"$name".tmp "$dest"/"$name""$ext"
sudo "$BTRFS_BIN" sub del "$dest"/"$name".tmp
ls -la "$dest"
ls -la "$dest"/"$name""$ext"
else
echo error receiving "$dest"/"$name".tmp, deleting source temporary snap "$tsnap"
[[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap"
return 1
fi
fi
} }
@ -322,22 +348,20 @@ alias btsub="sudo $BTRFS_BIN subvolume"
dir2subv () { dir2subv () {
# Directory to convert into BTRFS subvolume # Directory to convert into BTRFS subvolume
local dirPath; local dirSub; local perms
dirPath="$(realpath "$1")"
dirPath="$1"
if [[ ! -d $dirPath ]]; then echo no directory at "$dirPath"; return 1; fi if [[ ! -d $dirPath ]]; then echo no directory at "$dirPath"; return 1; fi
dirSub="$2" [[ $2 ]] && dirSub="$(realpath "$2")"
perms=/tmp/perms_$(basename "$dirPath") perms=/tmp/perms_$(basename "$dirPath")
if is_subv "$dirPath"; then echo "$dirPath" is already a subvolume, exiting; return 0; fi if is_subv "$dirPath"; then echo "$dirPath" is already a subvolume, exiting; return 0; fi
if ! confirm transform "$dirPath" into a subvolume at "${dirSub:-$dirPath}"; then return 1; fi if ! confirm transform "$dirPath" into a subvolume at "${dirSub:-$dirPath}"; then return 1; fi
pushd "$dirPath" &> /dev/null || return pushd "$dirPath" &> /dev/null || return
echo saving permissions at "$PWD" to "$perms" echo saving permissions at "$PWD" to "$perms"
getfacl -R . | sudo tee "$perms" sudo bash -c 'getfacl -R . > '"$perms"''
cat "$perms" popd &> /dev/null || return 0
if [[ ! $dirSub ]]; then if [[ ! $dirSub ]]; then
echo moving "$dirPath" to "${dirPath}_temp" echo moving "$dirPath" to "${dirPath}_temp"
@ -347,34 +371,37 @@ fi
echo creating subvolume at "${dirSub:-$dirPath}" echo creating subvolume at "${dirSub:-$dirPath}"
sudo "$BTRFS_BIN" subvolume create "${dirSub:-$dirPath}" sudo "$BTRFS_BIN" subvolume create "${dirSub:-$dirPath}"
# Copy as reflink for speed and save space echo copying contents to new subvolume
if [[ $dirSub ]]; then if [[ $dirSub ]]; then
echo copying "$dirPath" to "$dirSub" # if confirm copying "$dirPath" to "$dirSub"; then
sudo cp --archive --one-file-system --reflink=always "${dirPath}/." "${dirSub}" sudo /bin/cp --archive --one-file-system --reflink=always "${dirPath}/." "${dirSub}"
pushd "$dirSub" &> /dev/null || return 1 # fi
else else
echo copying "${dirPath}"_temp back to subvolume at "$dirPath" # if confirm copying "${dirPath}"_temp back to subvolume at "$dirPath"; then
sudo cp --archive --one-file-system --reflink=always "${dirPath}_temp/." "${dirPath}" sudo /bin/cp --archive --one-file-system --reflink=always "${dirPath}_temp/." "${dirPath}"
# fi
fi fi
# echo for debugging changing permissions at "$PWD" pushd "${dirSub:-$dirPath}" &> /dev/null || return
# sudo chmod -R 777 . # cat $perms
# sudo chown -R nobody:nobody . echo restoring saved permissions using "$perms" on $PWD
# ls -la . if sudo setfacl --restore="$perms"; then
# echo "---------------------" echo permissions restored, deleting $perms
sudo rm -f "$perms"
echo restore saved permissions to "$perms" else
sudo setfacl --restore="$perms" echo FATAL: permissions were not resorted
sudo rm -f "$perms" fi
popd &> /dev/null || return 0
echo "$dirPath" has been converted into a subvolume at "${dirSub:-$dirPath}" echo "$dirPath" has been converted into a subvolume at "${dirSub:-$dirPath}"
if [[ -d ${dirPath}_temp ]];then if [[ -d ${dirPath}_temp ]];then
echo $dirPath was converted but now check it is ok before deleting temporary copy
if confirm -s do you want to now delete the copy at "${dirPath}"_temp; then if confirm -s do you want to now delete the copy at "${dirPath}"_temp; then
sudo rm -rf --one-file-system "${dirPath}_temp" sudo rm -rf --one-file-system "${dirPath}_temp"
fi fi
fi fi
popd &> /dev/null || return 0
} }

16
modules/scripting/ini.mod Normal file
View file

@ -0,0 +1,16 @@
#!/bin/bash
# https://stackoverflow.com/a/61329094
parse_ini () {
local ini=$1
if [[ ! -f $ini ]]; then ini=$1.ini; if [[ ! -f $ini ]]; then echo no file "$ini"; return 1; fi fi
sed -n -E "/^\[.*\]/{s/\[(.*)\]/\1/;h;n;};/^[a-zA-Z]/{s/#.*//;G;s/([^ ]*) *= *(.*)\n(.*)/\3_\1='\2'/;p;}" "$ini" | \
sed -E 's/^_//'| \
sed -E 's/^([^=]*)[.]/\1_/' | \
sed -E 's/^([^=]*)[-]/\1_/' | \
( [[ $2 ]] && grep "$2" || cat )
}
ini2env () {
eval $(parse_ini "$@")
}