archive current folder files

This commit is contained in:
2019-04-26 10:10:57 +03:00
parent 96c852d313
commit 5991f6b9b9
2 changed files with 101 additions and 0 deletions

1
bin/archive-files Symbolic link
View File

@@ -0,0 +1 @@
../files/archive-files

100
files/archive-files Executable file
View File

@@ -0,0 +1,100 @@
#!/bin/bash
VERSION="20190426"
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 " --rm Remove source folders after archival."
exit
}
function listfiles() {
find "$@" -mindepth 1 -maxdepth 1 -type f | sort -V
}
function count_size() {
cat - | python -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
for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; }
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
[[ "${!i}" = "-"* ]] && helpexit
}
archive_name=$( basename $( pwd ) )".$SUFFIX"
if [[ -f "$archive_name" ]]; then
echo Archive $archive_name already exist
exit 1
fi
export IFS=$'\n'
REALFILES=( $( listfiles . ) )
[[ "$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
printf "$S<ctrl-c> to quit$Z\n"
read foo
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