use which instead of command -v

added unlocku to alias
added new way better copy module/function based on rsync
  copies local and remote
master
David Kebler 2024-03-01 15:01:47 -08:00
parent 140404ff49
commit 3a246c71cd
8 changed files with 221 additions and 139 deletions

View File

@ -52,4 +52,6 @@ edit() { ${EDITOR:-nano} $@; }
gedit() { ${GEDITOR:-xed} $@; }
unlocku () ( faillock --user $1 --reset )
rsynchg() { rsync --help | grep "\-$1"; }

View File

@ -1,2 +1,5 @@
#!/bin/bash
module_load minimize
minimize module.base.src module.base
pushd "$(dirname $(realpath "${BASH_SOURCE:-$0}"))" &> /dev/null || exit
minimize module.base.src module.base
popd &> /dev/null

View File

@ -4,7 +4,7 @@ local DIR
local NAME
DIR=$1
NAME=$2
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
FILE="$(which find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
COUNT=0
if [ "$FILE" ]; then
COUNT=$(echo $FILE | xargs -n 1 | wc -l)

View File

@ -13,7 +13,7 @@ NAME=$2
# TODO use file instead for valid extensions
# will look for valid module extensions and also look for duplicate modules with a directory which is not allowed
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
FILE="$(which find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
# echo files found $FILE
COUNT=0
if [ "$FILE" ]; then

View File

@ -0,0 +1,7 @@
*[Tt]rash/
tmp/*
node_modules/*
*[Cc]ache/*
*[lL]ogs/*
[Ll]og/*
lost+found

206
modules/utility/copy.func Normal file
View File

@ -0,0 +1,206 @@
#!/bin/bash
export COPY_MODULE_PATH="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
module_load debug
module_load confirm
copy () {
if ! which rsync &> /dev/null; then echo rsync must be installed to use this function; return 6; fi
local SHOST; local DHOST; local sshargs; local args
local SRC; local DEST; local mirror; local quiet
local SPATH; local DPATH; local exec; local list; local verbose
local usesudo;local cmd;local noconfirm;local merge
args=()
help() {
cat <<EOF
usage:
rsync <script options> source destination <rsync options> --- <ssh options>
Available Script Options
-r, run the command by this script uses --dry-run. When ready pass this flag.
-s, run command as sudo
-l, list the rsync command that will be run instead of running it. removes --dry-run.
great for generating a rsync command to be used elsewhere
-p, make path at destination if need be. careful will creating missing subdirectories
-e, rsync --exclude , as many of these as desired
-m, merge source inside destination instead of making folder at destination, good for changing destination folder name
-M, copy as "mirror" uses options. -a --numeric-ids --delete --delete-excluded --force
-f, rsync --exclude-from <path>, can have multiple
-F, copy a filesystem, uses -M mirror plus -xHAXS, and uses sudo (i.e. -s script option)
-n, no confirm, will always confirm command unless this is set
-q, quiet, suppress progress
-v, verbose, same as -v for rsync option, unset if -q is set
-a, archive, same as -a for rsync option, basic identical copy with recursion
-c, clean tranfer meaning will not transfer folder with trash, cache, log, nodemodules
-C, show excludes for clean transfer
-h, this help text
source or destination may include a <host>: prefix but not both
use 'fcopy <src> <dest>' for fast copy uses -r -q -m -n -a
EOF
}
# parse sshcp options
local OPTION
local OPTARG
local OPTIND
while getopts 'avMqpf:le:rcCmd:s:hnF' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
s)
usesudo=sudo
;;
c)
# clean out unneeded folders like cache, trash, log
args+=("--exclude-from=$COPY_MODULE_PATH/cache-trash-log.exc")
;;
C)
echo "## clean transfer exclusions found in $COPY_MODULE_PATH/cache-trash-log.exc ##"
cat "$COPY_MODULE_PATH/cache-trash-log.exc"
echo -e "\n##############################################"
return 0
;;
q)
quiet=true
;;
n)
noconfirm=true
;;
e)
args+=("--exclude=$OPTARG")
;;
f)
args+=("--exclude-from=$OPTARG")
;;
F)
args+=("-xHAXS")
mirror=true
usesudo=sudo
;;
p)
args+=("--mkpath")
;;
m)
merge=true
;;
M)
mirror=true
;;
l)
list=true
;;
v)
verbose="-v"
;;
r)
exec=true
;;
a)
args+=("-a")
;;
h)
help
return 0
;;
*)
>&2 echo fatal: unknown remote script option $OPTION, aborting
help
return 1
;;
esac
done
shift $((OPTIND - 1))
[ $# -lt 2 ] && echo both a source and destination need to be passed && return 2
# source parse
SRC=$1; shift
if [[ $SRC =~ ":" ]]; then
echo source is remote
SHOST=${SHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$SRC")}
SPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$SRC")
else
# echo source is local
SPATH=$SRC
fi
# destination parse
DEST=$1;shift
if [[ $DEST =~ ":" ]]; then
# echo destination is remote
DHOST=${DHOST:-$(sed 's/\(.*\):.*/\1/' <<< "$DEST")}
DPATH=$(sed 's/.*:\(.*\)/\1/' <<< "$DEST")
else
# echo destination is local
DPATH=$DEST
fi
[[ $DHOST && $SHOST ]] && echo this script can not process remote host to remote host transfer && return 5
# is source or destination is remote, create -e ssh sync option
if [[ $DHOST || $SHOST ]]; then
if ! module_load ssh &>/dev/null; then
echo unable to load ssh module
echo likely the uci network repo has not been installed
echo to do a remote copy this repo which contains the ssh module
echo "must be installed. Try \"module_load uci-shell-install; install_shell_network\""
return 6
fi
if [[ ! $(get_user_host ${DHOST:-$SHOST}) ]]; then
>&2 echo fatal: the host ${DHOST:-$SHOST} is not valid, aborting remote copy
return 1
fi
# parse ssh arguments from the rest
debug $( ( IFS=$','; echo all arguments: "$*" ) )
for ((d=1; d<$#; ++d)); do
[[ ${!d} == "---" ]] && break
done
if [[ $d -lt $# ]]; then # if there are extra ssh arguments
debug found --- at $d
sshargs=("${@:$d+1:$#}")
debug $( ( IFS=$','; echo "ssh arguments: ${sshargs[*]}" ) )
args+=("${@:1:$d-1}")
debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) )
else
args+=("${@:1}")
fi
args+=( "$(remove_end_spaces "-e '$(ssh -l "${sshargs[*]}")'")")
fi
# assemble options
[[ ! $exec ]] && args+=(--dry-run -v)
[[ ! $quiet ]] && args+=(--info=progress2 $verbose)
[[ $mirror ]] && args+=(-a --numeric-ids --delete --delete-excluded --force)
[[ ! -v PS1 ]] && noconfirm=true
if [[ ! $noconfirm && ! $list ]]; then
if [[ $merge ]]; then
# todo mirror option
confirm The contents within $SPATH will be $([[ $mirror ]] && echo mirrored || echo placed ) inside $DPATH || return 0
else
confirm the directory $(basename $SPATH) of $SPATH will be $([[ $mirror ]] && echo mirrored || echo put ) at destination $DPATH/$(basename $SPATH)? || return 0
fi
fi
cmd="$usesudo $(which rsync) ${args[*]} $SRC$([[ $merge ]] && echo "/") $DEST"
[[ $list ]] && echo "$cmd" && return 0
if [[ ! $quiet ]]; then [[ $exec ]] && echo executing the copy command || echo dry run of command, use -r to execute; fi
if eval $cmd; then
[[ ! $quiet ]] && echo copy success!
debug $cmd
else
>&2 echo remote copy failed
>&2 echo $cmd
return 1
fi
}
alias fcopy="copy -r -q -m -n -a "
# for copying chromium profile use
# --exclude=Singleton*

View File

@ -12,50 +12,6 @@ isDir() {
fi
}
function dir_copy () {
[[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; }
module_load confirm
local usesudo;local cmd;local src;local noconfirm;local merge
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'sem' OPTION; do
case "$OPTION" in
s)
usesudo=sudo
;;
e)
noconfirm=true
;;
m)
merge=true
;;
*)
echo unknown option $OPTION
return 1
;;
esac
done
shift $(( OPTIND - 1 ))
[ $# -ne 2 ] && echo two directories source and destination need to be passed && return 2
src=$(realpath -s $1)
dest=$(realpath -s $2)
[ ! -d "$src" ] && echo source: $src is not a directory && return 3
[[ $merge ]] && src=${src}/
cmd="$usesudo rsync --exclude *[C]ache* --progress -aAru $src $dest"
echo $cmd
[[ ! -v PS1 ]] && noconfirm=true
[[ ! $noconfirm ]] && { confirm Do you want to $([[ $merge ]] && echo merge $src into || echo copy $src within) $dest? || return 0; }
echo copying.....
eval $cmd
}
dir_size () {
if sudo ls $1 >/dev/null ; then
sudo du -h --apparent-size --max-depth=1 $1 | sort -rh

View File

@ -1,92 +0,0 @@
#!/bin/bash
mirror_rootfs () {
local boot; local dest; local rootfs; local opts
[[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; }
opts=" --dry-run"
[[ $1 == "--run" ]] && opts="" && shift
[[ $1 == "-v" ]] && opts+=" -v" && shift
[[ $1 == "-o" ]] && opts+=" $2" && shift
dest=$(realpath -s $1)
rootfs=$2
[ ! -d "$dest" ] && echo destination $dest is not a directory && return 3
[ ! -d "$rootfs/etc" ] && echo not root filesystem at $rootfs && return 3
echo making mirror of root filesystem at ${rootfs:-/} to $dest/rootfs
#TODO if destination is btrfs then make snapshot otherwise make directory
sudo btrfs subvol create $dest/rootfs >/dev/null
if sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --delete-excluded --force --exclude-from="$(rootfs_excludes)" ${rootfs:-/} $dest/rootfs ; then
echo root parition mirror was made to $dest/rootfs
boot=$rootfs/boot
[[ ! $(mountpoint $boot) ]] && boot=$rootfs/boot/efi
[[ ! $(mountpoint $boot) ]] && echo neither $rootfs/boot nor $boot are mountpoints, no boot parition to mirror && return 2
echo making mirror of boot $boot partition to $dest$boot
sudo btrfs subvol create $dest$boot >/dev/null
if sudo rsync -aHAWXS $opts --info=progress2 --numeric-ids --force --delete $boot/ $dest$boot; then
echo boot partion is mirrored
else
echo unable to sync a copy of $boot, aborted backup
fi
else
echo unable to sync a copy of root file system, backkup failed
fi
}
rclone () {
local boot; local dest; local src; local opts
[[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; }
opts=" --dry-run"
[[ $1 == "--run" ]] && opts="" && shift
[[ $1 == "-v" ]] && opts+=" -v" && shift
[[ $1 == "-o" ]] && opts+=" $2" && shift
src=$(realpath $1)
dest=$(realpath $2)
mkdir -p $dest >/dev/null
echo making clone/mirror of $src to $dest
if [ "$(ls -A $dest)" ]; then
echo WARNING: $dest is not empty but will be erased!, be sure before using --run
# TODO RUN CONFIRM
# if [[ $opt == *"--run"* ]]; then
fi
# if sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --delete-excluded --force $src/ $dest/ ; then
cmd="sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --force $src/ $dest/"
echo "$cmd"
if $cmd ; then
echo mirror/clone was successful
else
echo unable to make the clone
fi
}
rootfs_excludes () {
local list; local delete
[[ $1 == "-l" ]] && list=true && shift
[[ $1 == "-r" ]] && delete=true && shift
[[ "$1" == "-"* ]] && >&2 echo bad option, aborting && return 1
rfse=${1:-/tmp/rootfs_excludes.lst}
touch $rfse
[[ $delete ]] && rm $rfse
cat <<EOF > $rfse
.[Tt]rash*
tmp/*
node_modules
[Cc]ache/*
logs/*
log/*
lost+found
EOF
if [[ $list ]];then
cat $rfse; rm -f $rfse
else
echo $rfse
fi
}
mountpoints () {
cat /proc/mounts | cut -d ' ' -f2 | sort
}