refactor snapshot_send and mvlnbk

add clean_locale
master
David Kebler 2024-11-04 07:01:55 -08:00
parent df297025d4
commit c6c8f9876b
3 changed files with 153 additions and 60 deletions

View File

@ -3,9 +3,17 @@ export BTRFS_BIN=$(which btrfs)
module_load confirm module_load confirm
module_load helpers module_load helpers
# set this for sudo # NOTE: set this for sudo
# ALL ALL = (root) NOPASSWD:/bin/btrfs # ALL ALL = (root) NOPASSWD:/bin/btrfs
is_subvol () {
if [[ $1 == "-r" ]]; then
if ! btrfs subvolume show "$2" 2> /dev/null | grep readonly &> /dev/null; then return 1; fi
else
btrfs subvolume show "$1" &> /dev/null
fi
}
make_subvol() { make_subvol() {
local usesudo local usesudo
local uid local uid
@ -124,30 +132,63 @@ folder_snapshot() {
snapshot_send() { snapshot_send() {
if [[ $1 == "-e" ]];then
dr="" # Show usage and exit with status
shift 1 help () {
echo 'usage: snapshot_send -h,-r <source_path, full or relative> <destination_parent_dir> <optional, alternate directory name>'
}
if [[ $1 == "-h" ]]; then help; return; fi
local dr="echo "
if [[ $1 == "-r" ]]; then
unset dr; shift;
else else
echo "snapshot_send <-e> <souce snapshot path> <destination folder on different partition> <destination extension>" echo this will be a dry run, use -r to actually run the command
echo preface with -e to execute
dr="echo"
fi fi
temp="$(dirname $1)/tmp_send"
name=$(basename $1) if [[ $# -lt 2 ]]; then help; return 1; fi
name=$(echo $name | cut -f 1 -d '.')
ext="$(echo $name | cut -s -f 2 -d '.')" # Check for directories
if ! is_subvol "$1"; then
echo source $1 not a subvolume
help
return 2
fi
if [[ ! -d "$2" ]]; then
echo destination $2 not a valid directory
help; return 3
fi
# Get paths
local src=$(abs_path "${1%/}")
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 '.')
[[ $ext ]] && ext=".$ext" [[ $ext ]] && ext=".$ext"
[[ $3 ]] && ext=".$3" [[ $3 ]] && ext=".$3"
sudo mkdir $temp
$dr sudo $BTRFS_BIN sub snap -r $1 $temp/$name$ext local tsnap
if [[ $dr ]]; then if ! is_subvol -r $src; then
$dr "sudo $BTRFS_BIN send $temp/$name$ext | btrfs receive $2" tsnap=$psrc/$name.tmp
else $dr sudo $BTRFS_BIN sub snap -r $src $tsnap
sudo $BTRFS_BIN send $temp/$name$ext | btrfs receive $2
fi fi
$dr sudo $BTRFS_BIN sub del $temp/$name$ext
ls -la $2 if [[ $dr ]]; then
sudo rm -r $temp echo "sudo $BTRFS_BIN send $([[ $tsnap ]] && echo $tsnap || echo $src) | btrfs receive $dest/$name$ext"
echo "[[ "$tsnap" ]] && sudo $BTRFS_BIN sub del $tsnap"
else
sudo $BTRFS_BIN send $([[ $tsnap ]] && echo $tsnap || echo $src) | btrfs receive $dest
mv $dest/$snap $dest/$name$ext
[[ "$tsnap" ]] && sudo $BTRFS_BIN sub del $tsnap
echo transfer is complete
ls -la $dest
ls -la $dest/$name$ext
fi
} }
mount_subvolume () { mount_subvolume () {

View File

@ -195,3 +195,6 @@ return 0
# alias chext=change-ext # alias chext=change-ext
clean_locale () {
/bin/find /usr/share/locale/* -maxdepth 0 -type d -a ! '(' -name 'en_US' ')' -exec sudo rm -r {} +
}

View File

@ -2,44 +2,93 @@
# moves a directory and then links it back # moves a directory and then links it back
# good for moving directories from one partition to another but without changing any settings # good for moving directories from one partition to another but without changing any settings
SCRIPT_NAME='mvln' module_load copy
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>' module_load path
module_load confirm
mvlnbk () {
# Show usage and exit with status # Show usage and exit with status
show_usage_and_exit () { help () {
echo $USAGE_STRING echo 'usage: mvlnbk -h,-r <source_path, full or relative> <destination_parent_dir> <optional, alternate directory name> -- <copy options>'
exit 1
} }
# ERROR file does not exist if [[ $1 == "-h" ]]; then help; return; fi
no_file () {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax local co;
if [ $# -ne 2 ]; then local dr="echo "
show_usage_and_exit
if [[ $1 == "-r" ]]; then
co="-r"; unset dr; shift;
else
echo this will be a dry run, use -r to actually run the command
fi fi
# Check file existence if [[ $# -lt 2 ]]; then help; return 1; fi
if [ ! -e "$1" ]; then
no_file $1 # Check for directories
if [[ ! -d "$1" ]]; then
echo source $1 not a valid directory
help
return 2
fi
if [[ ! -d "$2" ]]; then
echo destination $2 not a valid directory
help; return 3
fi fi
# Get paths # Get paths
source_path=$1 local src=$(abs_path "${1%/}")
destination_path=$2 local psrc="$(dirname $src)"
local dest=$(abs_path "${2%/}")
local dir=$(basename $src)
local ddir=${3:-$dir}
# Check that destination ends with a slash local use_sudo
[[ $destination_path != */ ]] && destination_path="$destination_path"/ if [[ $(stat -c %u "$psrc") != "$UID" ]]; then
echo $psrc not owned by current user $USER
echo elevating to sudo
use_sudo="sudo "
co+=" -s"
fi
# Move source if confirm do you want to move $src to $dest/$ddir and link back; then
[[ -d "$destination_path" ]] || mkdir -p "$destination_path" if $dr copy $co -m -i $src $dest/$ddir; then
mv "$source_path" "$destination_path" echo directory copied successfully
echo moving source to temporary directory $src-tmp
if $dr $use_sudo mv $src $src-tmp; then
echo success, creating link back now
if $dr $use_sudo ln -s "$dest/$ddir" "$src"; then
if [[ ! $dr ]]; then
echo success
echo destination $dest/$ddir
ls -la $dest/$ddir
echo from link at $src
ls -la $src
ls -Lla $src
if confirm -s Was the move and link back succesfull; then
echo deleting the temporary directory $src-tmp
$dr $use_sudo rm -rf $src-tmp
else
echo restoring $src from $src-tmp
$dr $use_sudo rm $src
$dr $use_sudo mv $src-tmp $src
fi
fi
fi
else
echo failed to move source to temporary directory so not creating link back
fi
else
echo rsync copy failed, aborting link back
fi
# Get original path else
original_path=$destination_path$(basename $source_path) echo aborting move and link back
fi
# Create symlink in source dir }
ln -s "$original_path" "${source_path%/}"
# # if script was executed then call the function
(return 0 2>/dev/null) || mvlnbk $@