From 5991f6b9b98d8a87b24de569ef27b16662b8e5e5 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Fri, 26 Apr 2019 10:10:57 +0300 Subject: [PATCH] archive current folder files --- bin/archive-files | 1 + files/archive-files | 100 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 120000 bin/archive-files create mode 100755 files/archive-files diff --git a/bin/archive-files b/bin/archive-files new file mode 120000 index 0000000..da89f7e --- /dev/null +++ b/bin/archive-files @@ -0,0 +1 @@ +../files/archive-files \ No newline at end of file diff --git a/files/archive-files b/files/archive-files new file mode 100755 index 0000000..540b4d5 --- /dev/null +++ b/files/archive-files @@ -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 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