more simple archiver
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
VERSION="20160801"
|
||||
VERSION="20170907"
|
||||
|
||||
function helpexit() {
|
||||
BS=$( basename "$0" )
|
||||
echo "Archive the subfolders of the current directory, version: $VERSION"
|
||||
echo "Usage: $BS [-z/-n] [folder name(s)]"
|
||||
echo " -z Compress. If not given, will ask action after archiving"
|
||||
echo " -n No compression. Exit after TARchive"
|
||||
echo "Requires pv, tar and python"
|
||||
echo "Usage: $BS [-z/-n/--rm] [folder name(s)]"
|
||||
echo " -z Compress."
|
||||
echo " -n No compression. [default]"
|
||||
echo " --rm Remove source folders after archival"
|
||||
echo " folder name: If given, archive only the named folder(s)."
|
||||
echo " If not, for loop over all folders in the current directory."
|
||||
@@ -17,105 +18,91 @@ function helpexit() {
|
||||
function listfolders() {
|
||||
find "$@" -mindepth $DEPTH -maxdepth $DEPTH -type d | sort
|
||||
}
|
||||
function fsize() {
|
||||
echo -en "\e[2K\r"
|
||||
[[ -z "$2" ]] || printf "%s -> " "$2"
|
||||
printf "%s %s" "$( timeout 1 fastdu -h "$1" )" "$1"
|
||||
}
|
||||
function killtrap() {
|
||||
kill "$PID" &>/dev/null
|
||||
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 counting size: %s \r'%(sys.argv[1],sizeof_fmt(sum)))
|
||||
sys.stderr.flush()
|
||||
except KeyboardInterrupt:
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
pass
|
||||
print(sum)
|
||||
" "$1"
|
||||
}
|
||||
|
||||
function exitokay() {
|
||||
[[ "$REMOVE" -eq 1 ]] && { exit; }
|
||||
echo -ne "\nIf all looks okay, delete source folders with: # rm -r "
|
||||
echo -ne "\n${G}If all looks okay, delete source folders with: ${Z}# rm -r "
|
||||
for folder in "${REALFOLDERS[@]}"; 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
|
||||
DEPTH=1
|
||||
FOLDERS=( )
|
||||
REMOVE=0
|
||||
for ((i=1; i<=${#@}; i++)) {
|
||||
[[ "${!i}" = "-h" ]] && helpexit
|
||||
[[ "${!i}" = "--help" ]] && helpexit
|
||||
[[ "${!i}" = "-n" ]] && { NOCOMPRESSION=1; continue; }
|
||||
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; continue; }
|
||||
[[ "${!i}" = "-n" ]] && { continue; } # obsolete behaviour
|
||||
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; }
|
||||
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
|
||||
[[ "${!i}" = "-"* ]] && helpexit
|
||||
FOLDERS+=( "${!i%/}" )
|
||||
DEPTH=0
|
||||
}
|
||||
[[ ${#FOLDERS[@]} -eq 0 ]] && FOLDERS+=( "." )
|
||||
which pv &>/dev/null || { echo pv command missing; exit 1; }
|
||||
export IFS=$'\n'
|
||||
REALFOLDERS=( $( listfolders "${FOLDERS[@]}" ) )
|
||||
echo 'Existing files will be overwritten!'
|
||||
[[ "$REMOVE" -eq 1 ]] && {
|
||||
echo 'Source folders will be deleted!'
|
||||
}
|
||||
echo -n "Archive the following folders "
|
||||
echo $R'Existing files will be overwritten!'$Z
|
||||
[[ "$REMOVE" -eq 1 ]] && echo 'Source folders will be deleted!'
|
||||
echo -n $S"Archive the following folders "
|
||||
[[ "$NOCOMPRESSION" -eq 1 ]] && {
|
||||
echo -n without compression;
|
||||
} || {
|
||||
[[ "$COMPRESSION" -eq 1 ]] && echo -n with gzip compression
|
||||
}
|
||||
echo ':'
|
||||
echo ':'$Z
|
||||
for folder in "${REALFOLDERS[@]}"; do echo -n "'$folder', "; done | sed -e "s/..$//"
|
||||
echo ""
|
||||
echo '<ctrl-c> to quit'
|
||||
printf "\n$S<ctrl-c> to quit$Z\n"
|
||||
read foo
|
||||
IFS=" " read -a SLEEPS <<< '0.1 0.5 1 2 3 4 5'
|
||||
trap killtrap 9 15
|
||||
set -o pipefail
|
||||
|
||||
for d in "${REALFOLDERS[@]}"; do
|
||||
printf "%s" $d
|
||||
tar cvvf "$d.tar" "$d" > "$d.tar.lst" &
|
||||
PID=$!
|
||||
DU=$( nice fastdu -h "$d" & )
|
||||
DUPID=$!
|
||||
while kill -0 "$PID" &>/dev/null; do
|
||||
[[ -f "$d.tar" ]] && fsize "$d.tar" "$DU"
|
||||
[[ -f "$d.tar" ]] || { sleep 2; continue; }
|
||||
for i in ${SLEEPS[@]}; do
|
||||
sleep $i; kill -0 "$PID" &>/dev/null || break 2
|
||||
printf .
|
||||
done
|
||||
IFS=" " read -a SLEEPS <<< '4 4 4 4'
|
||||
done
|
||||
fsize "$d.tar" "$DU"
|
||||
kill -0 "$DUPID" &>/dev/null || kill "$DUPID" &>/dev/null
|
||||
echo ''
|
||||
if wait $PID; then
|
||||
# tar exists okay
|
||||
[[ "$REMOVE" -eq 1 ]] && {
|
||||
echo Removing folder $d
|
||||
which rm-progress &> /dev/null && rm-progress -f "$d"
|
||||
[[ -d "$d" ]] && rm -r "$d"
|
||||
}
|
||||
fi
|
||||
done
|
||||
trap - 9 15
|
||||
[[ "$NOCOMPRESSION" -eq 1 ]] && {
|
||||
exitokay
|
||||
}
|
||||
[[ "$COMPRESSION" -eq 1 ]] || {
|
||||
echo 'Proceed to compress? <ctrl-c> to quit'
|
||||
read foo
|
||||
}
|
||||
echo 'Compressing..'
|
||||
IFS=" " read -a SLEEPS <<< '0.1 0.5 1 2 3 4 5'
|
||||
trap killtrap 9 15
|
||||
set -e
|
||||
set -o pipefail
|
||||
for d in "${REALFOLDERS[@]}"; do
|
||||
DU=$( fastdu "$d.tar" )
|
||||
DUh=$( fastdu -h "$d.tar" )
|
||||
which pv &> /dev/null || { echo "You dont have pv"; exit 1; }
|
||||
cat "$d.tar" | pv -s $DU | gzip > "$d.tar.gz" && {
|
||||
rm "$d.tar"
|
||||
printf "$d"
|
||||
SIZE=$( find "$d" -type f -printf %s"\n" | count_size "$d" )
|
||||
printf "\n"
|
||||
eval "tar cvv --index-file=\"$d.${SUFFIX}.lst\" \"$d\" | \
|
||||
pv -s \"$SIZE\" $COMPRESSCMD > \"$d.${SUFFIX}\"" && {
|
||||
# tar exists okay
|
||||
if [ "$REMOVE" -eq 1 ]; then
|
||||
printf "${Y}Removing folder $d$Z\n"
|
||||
which rm-progress &> /dev/null && rm-progress -f "$d"
|
||||
[[ -d "$d" ]] && rm -r "$d"
|
||||
fi
|
||||
}
|
||||
printf '\r\033[F'
|
||||
fsize "$d.tar.gz" "$DUh"
|
||||
printf '\033[0K\n'
|
||||
done
|
||||
exitokay
|
||||
|
||||
Reference in New Issue
Block a user