Files
q-tools/files/archive_subfolders
2016-02-16 13:56:26 +02:00

72 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
function helpexit() {
echo Archive the subfolders of the current directory
echo " -z Compress. If not passed, will ask after tar archive"
echo " -n No compression. Exit after tar archive"
exit
}
function listfolders() {
find "$1" -mindepth 1 -maxdepth 1 -type d | sort | tr '\n' '\000'
}
function fsize() {
echo -en "\e[2K\r"
[[ -z "$2" ]] || printf "%s -> " "$2"
printf "%s %s" "$( timeout 1 fastdu -h "$1" )" "$1"
}
[[ "$1" = "-h" ]] && helpexit
echo 'Existing files will be overwritten!'
echo Archive the following folders:
listfolders . | tr "[:cntrl:]" " "
echo ''
echo '<ctrl-c> to quit'
read foo
read -a SLEEPS <<< "0.1 0.5 1 2 3 4 5"
listfolders . | while read -r -d "" d; 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"
for i in ${SLEEPS[@]}; do
sleep $i; kill -0 "$PID" &>/dev/null || break 2
printf .
done
read -a SLEEPS <<< "4 4 4 4"
done
fsize "$d.tar" "$DU"
kill -0 "$DUPID" &>/dev/null || kill "$DUPID" &>/dev/null
echo ''
done
[[ "$1" = "-n" ]] && {
exit
}
[[ "$1" = "-z" ]] || {
echo 'Proceed to compress? <ctrl-c> to quit'
read foo
}
read -a SLEEPS <<< "0.1 0.5 1 2 3 4 5"
listfolders . | while read -r -d "" d; do
DU=$( fastdu -h "$d.tar" )
gzip -f "$d.tar" &
PID=$!
while kill -0 "$PID" >/dev/null 2>&1; do
[[ -f "$d.tar.gz" ]] && fsize "$d.tar.gz" "$DU"
for i in ${SLEEPS[@]}; do
sleep $i; kill -0 "$PID" &>/dev/null || break 2
printf .
done
read -a SLEEPS <<< "4 4 4 4"
done
fsize "$d.tar.gz" "$DU"
echo ''
done