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 helpers
# set this for sudo
# NOTE: set this for sudo
# 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() {
local usesudo
local uid
@ -124,30 +132,63 @@ folder_snapshot() {
snapshot_send() {
if [[ $1 == "-e" ]];then
dr=""
shift 1
else
echo "snapshot_send <-e> <souce snapshot path> <destination folder on different partition> <destination extension>"
echo preface with -e to execute
dr="echo"
fi
temp="$(dirname $1)/tmp_send"
name=$(basename $1)
name=$(echo $name | cut -f 1 -d '.')
ext="$(echo $name | cut -s -f 2 -d '.')"
# Show usage and exit with status
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
echo this will be a dry run, use -r to actually run the command
fi
if [[ $# -lt 2 ]]; then help; return 1; fi
# 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"
[[ $3 ]] && ext=".$3"
sudo mkdir $temp
$dr sudo $BTRFS_BIN sub snap -r $1 $temp/$name$ext
if [[ $dr ]]; then
$dr "sudo $BTRFS_BIN send $temp/$name$ext | btrfs receive $2"
else
sudo $BTRFS_BIN send $temp/$name$ext | btrfs receive $2
local tsnap
if ! is_subvol -r $src; then
tsnap=$psrc/$name.tmp
$dr sudo $BTRFS_BIN sub snap -r $src $tsnap
fi
$dr sudo $BTRFS_BIN sub del $temp/$name$ext
ls -la $2
sudo rm -r $temp
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"
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 () {

View File

@ -195,3 +195,6 @@ return 0
# 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
# good for moving directories from one partition to another but without changing any settings
SCRIPT_NAME='mvln'
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>'
module_load copy
module_load path
module_load confirm
mvlnbk () {
# Show usage and exit with status
help () {
echo 'usage: mvlnbk -h,-r <source_path, full or relative> <destination_parent_dir> <optional, alternate directory name> -- <copy options>'
}
if [[ $1 == "-h" ]]; then help; return; fi
local co;
local dr="echo "
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
if [[ $# -lt 2 ]]; then help; return 1; fi
# 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
# Get paths
local src=$(abs_path "${1%/}")
local psrc="$(dirname $src)"
local dest=$(abs_path "${2%/}")
local dir=$(basename $src)
local ddir=${3:-$dir}
local use_sudo
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
if confirm do you want to move $src to $dest/$ddir and link back; then
if $dr copy $co -m -i $src $dest/$ddir; then
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
else
echo aborting move and link back
fi
# Show usage and exit with status
show_usage_and_exit () {
echo $USAGE_STRING
exit 1
}
# ERROR file does not exist
no_file () {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax
if [ $# -ne 2 ]; then
show_usage_and_exit
fi
# Check file existence
if [ ! -e "$1" ]; then
no_file $1
fi
# Get paths
source_path=$1
destination_path=$2
# Check that destination ends with a slash
[[ $destination_path != */ ]] && destination_path="$destination_path"/
# Move source
[[ -d "$destination_path" ]] || mkdir -p "$destination_path"
mv "$source_path" "$destination_path"
# Get original path
original_path=$destination_path$(basename $source_path)
# 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 $@