113 lines
2.9 KiB
Bash
Executable File
113 lines
2.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
VERSION="20250626"
|
|
|
|
function helpexit() {
|
|
BS=$( basename "$0" )
|
|
echo "Archive the files of the current directory, version: $VERSION"
|
|
echo "Requires tar, pv and python"
|
|
echo "Usage: $BS [-z/-n/--rm]"
|
|
echo " -z Compress."
|
|
echo " -n No compression. [default]"
|
|
echo " -y Dont ask before starting the archival."
|
|
echo " --rm Remove source folders after archival."
|
|
|
|
exit
|
|
}
|
|
function listfiles() {
|
|
find "$@" -mindepth 1 -maxdepth 1 -type f | sort -V
|
|
}
|
|
function count_size() {
|
|
|
|
cat - | python3 -c "import sys
|
|
def sizeof_fmt(num, suffix='B'):
|
|
for unit in ['','K','M','G','T','P','E','Z']:
|
|
if num < 1024.0:
|
|
return '%3.1f %s%s' % (num, unit, suffix)
|
|
num /= 1024.0
|
|
return '%.1f %s%s' % (num, 'Y', suffix)
|
|
sum=0
|
|
try:
|
|
for line in sys.stdin:
|
|
sum += int(line)
|
|
sys.stderr.write('\r%s \r'%(sizeof_fmt(sum),))
|
|
sys.stderr.flush()
|
|
except KeyboardInterrupt:
|
|
sys.stdout.flush()
|
|
sys.stderr.flush()
|
|
pass
|
|
print(sum)
|
|
"
|
|
}
|
|
|
|
function exitokay() {
|
|
[[ "$REMOVE" -eq 1 ]] && { exit; }
|
|
echo -ne "\n${G}If all looks okay, delete source folders with: ${Z}# rm -r "
|
|
for folder in "${REALFILES[@]}"; do echo -n "'$folder' "; done
|
|
echo ''
|
|
exit
|
|
}
|
|
|
|
_qCol(){ true; } # incase qolop missing
|
|
. qolop &>/dev/null
|
|
Z=$( _qCol z ) #reset color
|
|
S=$( _qCol S ) #Bold
|
|
R=$( _qCol z R ) #Red
|
|
G=$( _qCol z g ) #green
|
|
Y=$( _qCol z Y ) #Yellow
|
|
|
|
NOCOMPRESSION=1
|
|
COMPRESSION=0
|
|
SUFFIX=tar
|
|
REMOVE=0
|
|
YES=0
|
|
FILES=( )
|
|
for ((i=1; i<=${#@}; i++)) {
|
|
[[ "${!i}" = "-h" ]] && helpexit
|
|
[[ "${!i}" = "--help" ]] && helpexit
|
|
[[ "${!i}" = "-y" ]] && { YES=1; continue; }
|
|
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; }
|
|
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
|
|
[[ "${!i}" = "-"* ]] && helpexit
|
|
FILES+=( "${!i}" )
|
|
}
|
|
archive_name=$( basename $( pwd ) )".$SUFFIX"
|
|
if [[ -f "$archive_name" ]]; then
|
|
echo Archive $archive_name already exist
|
|
exit 1
|
|
fi
|
|
|
|
export IFS=$'\n'
|
|
if [[ ${#FILES[@]} -eq 0 ]]; then
|
|
REALFILES=( $( listfiles . ) )
|
|
else
|
|
REALFILES=("${FILES[@]}")
|
|
fi
|
|
|
|
[[ "$REMOVE" -eq 1 ]] && echo 'Source files will be deleted!'
|
|
echo -n $S"Archive the following files to '$archive_name' "
|
|
[[ "$NOCOMPRESSION" -eq 1 ]] && {
|
|
echo -n without compression;
|
|
} || {
|
|
[[ "$COMPRESSION" -eq 1 ]] && echo -n with gzip compression
|
|
}
|
|
echo $Z
|
|
for file in "${REALFILES[@]}"; do echo "'$file'"; done
|
|
if [[ $YES -eq 0 ]]; then
|
|
printf "$S<ctrl-c> to quit$Z\n"
|
|
read foo
|
|
fi
|
|
set -o pipefail
|
|
|
|
SIZE=$( find "${REALFILES[@]}" -type f -printf %s"\n" | count_size )
|
|
tar c "${REALFILES[@]}" | \
|
|
eval pv -s "$SIZE" $COMPRESSCMD > "$archive_name" && {
|
|
# tar exists okay
|
|
if [ "$REMOVE" -eq 1 ]; then
|
|
printf "${Y}Removing files $d$Z\n"
|
|
#which rm-progress &> /dev/null && rm-progress -f "${REALFILES[@]}"
|
|
rm -f "${REALFILES[@]}"
|
|
fi
|
|
}
|
|
exitokay
|