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 fi
} }
is_subvv () { is_subvv () {
if [[ $1 == "-r" ]]; then if is_subv "$@"; then
if ! btrfs subvolume show "$2" 2> /dev/null | grep readonly &> /dev/null; then echo is a $([[ $1 == "-r" ]] && echo "read only") subvolume
echo not a readonly subvolume
else else
echo readonly subvoluem echo is not a $([[ $1 == "-r" ]] && echo "read only") subvolume
return 1
fi
}
# TODO: convert these into subcommands
subv_snap () {
local usesudo; local ro
[[ $1 == "-r" ]] && ro=true && shift
[[ $# -eq 0 ]] && echo no subvolume name passed && return 1
[[ $# -eq 1 ]] && echo no snapshot path/name passed && return 2
[[ $EUID -ne 0 ]] && usesudo=sudo
if is_subv "$1"; then
if [[ -v PS1 ]]; then
confirm create snapshot of subvolume $(realpath "$1") to $(realpath "$2") || return 1
$usesudo "$BTRFS_BIN" subvolume snapshot $ro "$1" "$2"
else
$usesudo "$BTRFS_BIN" subvolume snapshot $ro -q "$1" "$2"
fi fi
else else
if btrfs subvolume show "$1" &> /dev/null; then echo $1 not a subvolume, can not make a snapshot
echo subvolume return 3
else
echo not a subvolume
fi
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 {} +
} }
@ -198,24 +214,34 @@ subv_xfer() {
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
echo destination temporary subvolume "$dest"/"$name".tmp exists. Deleting so xfer can proceed
subv_del "$dest"/"$name".tmp
fi
sudo "$BTRFS_BIN" send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | sudo "$BTRFS_BIN" receive "$dest" sudo "$BTRFS_BIN" send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | sudo "$BTRFS_BIN" receive "$dest"
# FIXME: take a snapshot of the destination .tmp then remove .tmp, otherwise it will be readonly
# sudo mv "$dest"/"$name".tmp "$dest"/"$name"$ext
if is_subv -r "$dest"/"$name".tmp; then if is_subv -r "$dest"/"$name".tmp; then
echo transfer of "$name" to "$name""$ext" is complete, deleting source temporary snap "$tsnap"
[[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap"
if is_subv "$dest"/"$name""$ext"; then
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
return 1
fi
fi
sudo "$BTRFS_BIN" sub snap "$dest"/"$name".tmp "$dest"/"$name""$ext" sudo "$BTRFS_BIN" sub snap "$dest"/"$name".tmp "$dest"/"$name""$ext"
sudo "$BTRFS_BIN" sub del "$dest"/"$name".tmp sudo "$BTRFS_BIN" sub del "$dest"/"$name".tmp
echo transfer is complete
fi
[[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap"
ls -la "$dest" ls -la "$dest"
ls -la "$dest"/"$name""$ext" 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 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
echo restore saved permissions to "$perms"
sudo setfacl --restore="$perms"
sudo rm -f "$perms" sudo rm -f "$perms"
else
echo FATAL: permissions were not resorted
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 "$@")
}