121 lines
3.3 KiB
Bash
Executable File
121 lines
3.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
VERSION="20160217"
|
|
|
|
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 " --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."
|
|
|
|
exit
|
|
}
|
|
function listfolders() {
|
|
find "$@" -mindepth $DEPTH -maxdepth $DEPTH -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"
|
|
}
|
|
function killtrap() {
|
|
kill "$PID" &>/dev/null
|
|
}
|
|
function exitokay() {
|
|
echo -ne "\nIf all looks okay, delete source folders with: # rm -r '"
|
|
listfolders "${FOLDERS[@]}" | sed -e "s/\x0/' '/g" -e "s,..$,,"
|
|
echo ''
|
|
exit
|
|
}
|
|
|
|
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}" = "--rm" ]] && { REMOVE=1; continue; }
|
|
[[ "${!i}" = "-"* ]] && helpexit
|
|
FOLDERS+=( "${!i%/}" )
|
|
DEPTH=0
|
|
}
|
|
[[ ${#FOLDERS[@]} -eq 0 ]] && FOLDERS+=( "." )
|
|
echo 'Existing files will be overwritten!'
|
|
echo -n "Archive the following folders "
|
|
[[ "$NOCOMPRESSION" -eq 1 ]] && {
|
|
echo -n without compression;
|
|
} || {
|
|
[[ "$COMPRESSION" -eq 1 ]] && echo -n with gzip compression
|
|
}
|
|
echo ':'
|
|
echo -n "'"
|
|
listfolders "${FOLDERS[@]}" | sed -e "s/\x0/' '/g" -e "s,..$,,"
|
|
echo ""
|
|
echo '<ctrl-c> to quit'
|
|
read foo
|
|
read -a SLEEPS <<< "0.1 0.5 1 2 3 4 5"
|
|
trap killtrap 9 15
|
|
|
|
listfolders "${FOLDERS[@]}" | 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"
|
|
[[ -f "$d.tar" ]] || { sleep 2; continue; }
|
|
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 ''
|
|
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..'
|
|
read -a SLEEPS <<< "0.1 0.5 1 2 3 4 5"
|
|
trap killtrap 9 15
|
|
|
|
listfolders "${FOLDERS[@]}" | 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
|
|
sleep 0.1
|
|
[[ -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
|
|
exitokay
|