From 46c8f5a9b40fb1b425768fc093dcd4a2f6d9929d Mon Sep 17 00:00:00 2001 From: David Kebler Date: Tue, 17 Dec 2024 17:00:51 -0800 Subject: [PATCH] fix snapshot_send bug in mv enclose vars in "" --- modules/filesystem/btrfs/btrfs.mod | 107 +++++++++++++++-------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/modules/filesystem/btrfs/btrfs.mod b/modules/filesystem/btrfs/btrfs.mod index e947ab1..af2ab32 100644 --- a/modules/filesystem/btrfs/btrfs.mod +++ b/modules/filesystem/btrfs/btrfs.mod @@ -22,22 +22,22 @@ make_subvol() { [[ $EUID -ne 0 ]] && usesudo=sudo uid=${2:-$USER} gid=${3:-$uid} - mkdir -p $(dirname $1) &> /dev/null - echo $usesudo $BTRFS_BIN subvolume create $1 - $usesudo $BTRFS_BIN subvolume create $1 - echo $usesudo chown $uid:$gid $1 - $usesudo chown $uid:$gid $1 + mkdir -p $(dirname "$1") &> /dev/null + echo $usesudo "$BTRFS_BIN" subvolume create "$1" + $usesudo "$BTRFS_BIN" subvolume create "$1" + echo $usesudo chown "$uid":"$gid" "$1" + $usesudo chown "$uid":"$gid" "$1" } del_subvol() { local usesudo; local delete [[ $1 == "-d" ]] && delete=true && shift [[ $EUID -ne 0 ]] && usesudo=sudo - if $usesudo $BTRFS_BIN subvolume show "$1" &> /dev/null; then + if $usesudo "$BTRFS_BIN" subvolume show "$1" &> /dev/null; then if [[ -v PS1 && ! $delete ]]; then confirm "DELETE the subvolume $(realpath "$1") (pass -d to avoid confirm)" || return 1 fi - $usesudo $BTRFS_BIN subvolume delete $1 + $usesudo "$BTRFS_BIN" subvolume delete "$1" else echo "$1 not a btrfs subvolume, nothing to delete" fi @@ -58,14 +58,14 @@ del_subvol() { snapshot_restore () { local src=$1 local dest=$2 - local name=${3:-$(echo $src| rev | cut -f 2- -d '.' | rev)} + local name=${3:-$(echo "$src"| rev | cut -f 2- -d '.' | rev)} local usesudo - echo copying $src at $PWD to $2 then renaming to $name + echo copying "$src" at "$PWD" to "$2" then renaming to "$name" # TODO check for snapshot, strip volume from source path, remove extra snapshot when done [[ $EUID -ne 0 ]] && usesudo=sudo - $usesudo $BTRFS_BIN send $src | $usesudo $BTRFS_BIN -q receive $dest + $usesudo "$BTRFS_BIN" send "$src" | $usesudo "$BTRFS_BIN" -q receive "$dest" # cd $2 || return - $usesudo $BTRFS_BIN subvolume snapshot $dest/$src $dest/$name + $usesudo "$BTRFS_BIN" subvolume snapshot "$dest"/"$src" "$dest"/"$name" # todo check for snapshot then delete transfered one } @@ -75,11 +75,11 @@ subvolume_size () { local all; [[ $1 == "-a" ]] && all=true if [[ $all ]]; then - sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3 - str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3); + sudo "$BTRFS_BIN" qgroup show "${2:-$PWD}" --kbytes | tail -n +3 + str=$(sudo "$BTRFS_BIN" qgroup show "${2:-$PWD}" --kbytes | tail -n +3); else - sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1 - str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1) + sudo "$BTRFS_BIN" qgroup show "${2:-$PWD}" --kbytes | grep "$1" + str=$(sudo "$BTRFS_BIN" qgroup show "${2:-$PWD}" --kbytes | grep "$1") fi # echo $str @@ -92,7 +92,7 @@ while read line; do subvolumeFolderSize=$((subvolumeFolderSize+thisLineKb)); done <<< "$str" -[[ $all ]] && echo "size of entire filesystem at ${2:-$PWD}" || echo size of subvolumes/snapshots $1 at "${2:-$PWD}" +[[ $all ]] && echo "size of entire filesystem at ${2:-$PWD}" || echo size of subvolumes/snapshots "$1" at "${2:-$PWD}" echo $subvolumeFolderSize KB echo "~ $((subvolumeFolderSize/1024)) MB" echo "~ $((subvolumeFolderSize/1024/1024)) GB" @@ -103,10 +103,10 @@ echo "~ $((subvolumeFolderSize/1024/1024)) GB" find_subvolumes() { dir=${1:-.} dep=${2:-1} - sudo /bin/find "$dir" -mindepth 1 -maxdepth $dep -type d -exec bash -c ' + sudo /bin/find "$dir" -mindepth 1 -maxdepth "$dep" -type d -exec bash -c ' for d do subv=$(basename $d) - sudo '$BTRFS_BIN' subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv" + sudo '"$BTRFS_BIN"' subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv" done' find-sh {} + } @@ -117,14 +117,14 @@ folder_snapshot() { else readonly="" fi - for sv in $(find_subvolumes $1 1); do + for sv in $(find_subvolumes "$1" 1); do if [[ -d $2/$sv ]]; then - echo snapshot $sv already exists in $2. You must manually delete target snapshots! + echo snapshot "$sv" already exists in "$2". You must manually delete target snapshots! else if sudo mkdir -p "$2" > /dev/null; then - sudo $BTRFS_BIN sub snap $readonly "$1/$sv" "$2/$sv" + sudo "$BTRFS_BIN" sub snap $readonly "$1/$sv" "$2/$sv" else - echo unable to make directory $2 so can not make subvolumes therein + echo unable to make directory "$2" so can not make subvolumes therein fi fi done @@ -152,53 +152,54 @@ snapshot_send() { # Check for directories if ! is_subvol "$1"; then - echo source $1 not a subvolume + echo source "$1" not a subvolume help return 2 fi if [[ ! -d "$2" ]]; then - echo destination $2 not a valid directory + echo destination "$2" not a valid directory help; return 3 fi # Get paths local src=$(abs_path "${1%/}") - local psrc="$(dirname $src)" + local psrc="$(dirname "$src")" local dest=$(abs_path "${2%/}") - local snap=$(basename $src) - local ext="$(echo $snap | cut -s -f 2 -d '.')" - local name=$(echo $snap | cut -f 1 -d '.') + local snap=$(basename "$src") + local ext="$(echo "$snap" | cut -s -f 2 -d '.')" + local name=$(echo "$snap" | cut -f 1 -d '.') [[ $ext ]] && ext=".$ext" [[ $3 ]] && ext=".$3" local tsnap - if ! is_subvol -r $src; then + if ! is_subvol -r "$src"; then tsnap=$psrc/$name.tmp - $dr sudo $BTRFS_BIN sub snap -r $src $tsnap + $dr sudo "$BTRFS_BIN" sub snap -r "$src" "$tsnap" fi - if [[ $dr ]]; then - echo "sudo $BTRFS_BIN send $([[ $tsnap ]] && echo $tsnap || echo $src) | btrfs receive $dest/$name$ext" - echo "[[ "$tsnap" ]] && sudo $BTRFS_BIN sub del $tsnap" + if [[ $dr ]]; then + 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" else - sudo $BTRFS_BIN send $([[ $tsnap ]] && echo $tsnap || echo $src) | btrfs receive $dest - sudo mv $dest/$snap $dest/$name$ext - [[ "$tsnap" ]] && sudo $BTRFS_BIN sub del $tsnap + sudo "$BTRFS_BIN" send $([[ $tsnap ]] && echo "$tsnap" || echo "$src") | btrfs receive "$dest" + sudo mv "$dest"/"$name".tmp "$dest"/"$name"$ext + [[ "$tsnap" ]] && sudo "$BTRFS_BIN" sub del "$tsnap" echo transfer is complete - ls -la $dest - ls -la $dest/$name$ext + ls -la "$dest" + ls -la "$dest"/"$name"$ext fi } mount_subvolume () { # echo sudo mount $1 -o subvol=$2 $3 - if mountpoint $3 &> /dev/null; then - echo $3 already a mountpoint, aborting subvolume mount + if mountpoint "$3" &> /dev/null; then + echo "$3" already a mountpoint, aborting subvolume mount else - mkdir -p $3 &> /dev/null - if ! sudo mount $1 -o subvol=$2 $3 > /dev/null ; then echo "failed to mount $2"; return 1; fi + mkdir -p "$3" &> /dev/null + if ! sudo mount "$1" -o subvol="$2" "$3" > /dev/null ; then echo "failed to mount $2"; return 1; fi fi } @@ -208,11 +209,11 @@ local usage; local subvol; local target; local run; local usesudo [[ $EUID -ne 0 ]] && usesudo=sudo subvol=$(realpath "$1") usage="usage: named_snapshot <-r> " -if sudo $BTRFS_BIN subvolume show "$1" &> /dev/null; then +if sudo "$BTRFS_BIN" subvolume show "$1" &> /dev/null; then # if [[ -v PS1 && ! $delete ]]; then -[[ ! $2 ]] && echo no extension name for named snapshot && echo $usage && return 1 +[[ ! $2 ]] && echo no extension name for named snapshot && echo "$usage" && return 1 if [[ $3 ]]; then - [[ ! -d $3 ]] && echo "target directory $3 does not exist, can not make snapshot" && echo $usage && return 2 + [[ ! -d $3 ]] && echo "target directory $3 does not exist, can not make snapshot" && echo "$usage" && return 2 target=$(realpath "$3")/$(basename "$(rm_ext "$subvol")").$2 else target="$(rm_ext "$subvol").$2" @@ -221,7 +222,7 @@ fi $usesudo "$BTRFS_BIN" sub snap "$subvol" "$target" else echo "$subvol not a btrfs subvolume, can't make named snapshot" -echo $usage +echo "$usage" return 3 fi @@ -232,13 +233,13 @@ fi mp_subvol () { module_load filesystem local mp; local subvol - mp=$(find_mountpoint $1) + mp=$(find_mountpoint "$1") path=${1//"$mp"/} dev=$(mount | grep "$mp " | cut -f 1 -d ' ') IFS=, read -r -a input <<< $(findmnt -nt btrfs | grep "$mp ") for x in "${input[@]}"; do - eval $(echo $x | grep subvol=) - [[ $subvol ]] && echo $dev $subvol$path + eval $(echo "$x" | grep subvol=) + [[ $subvol ]] && echo "$dev" "$subvol""$path" done } @@ -254,10 +255,10 @@ btrfs_clone () { src=$2 dest=$3 $usesudo umount /tmp/source &> /dev/null - sudo mount $src /tmp/source + sudo mount "$src" /tmp/source # $usesudo umount /tmp/dest &> /dev/null # if mount_subvolume $src $subvol /tmp/source; then - if ! make_subvol $dest/$(basename $subvol) &> /dev/null; then echo unable to make subvolume $1 at $dest; return 2; fi + if ! make_subvol "$dest"/$(basename "$subvol") &> /dev/null; then echo unable to make subvolume "$1" at "$dest"; return 2; fi # $usesudo btrfs subvolume show $dest/$(basename $subvol) # if mount_subvolume $(mp_subvol $dest/$(basename $subvol)) /tmp/dest; then # echo cloning now @@ -270,7 +271,7 @@ btrfs_clone () { echo "#################################" else echo clone failed - echo $cmd + echo "$cmd" fi # else # echo unable to mount a destination subvolume $1 at $3 @@ -303,7 +304,7 @@ getfacl -R "$dirPath" > /tmp/_perms sudo mv "${dirPath}" "${dirPath}_original" # Create btrfs subvolume -sudo $BTRFS_BIN subvolume create "${dirPath}" +sudo "$BTRFS_BIN" subvolume create "${dirPath}" # Copy as "reflink" for speed and save space sudo cp --archive --one-file-system --reflink=always \