output for folder archiver

This commit is contained in:
Q
2025-11-30 21:10:40 +02:00
parent a05f8dd6b9
commit 8c72f19a9a

View File

@@ -1,6 +1,6 @@
#!/bin/bash
VERSION="20250225"
VERSION="20251130"
function helpexit() {
BS=$( basename "$0" )
@@ -10,6 +10,7 @@ function helpexit() {
echo " -z Compress."
echo " -n No compression. [default]"
echo " -y Dont ask before starting the archival."
echo " --output DIR Output directory for archives."
echo " --nl Do not store a separate file list."
echo " --rm Remove source folders after archival."
echo " --split SIZE Split archive to SIZE. ex. 500G"
@@ -77,6 +78,7 @@ REMOVE=0
KEEP_LIST=1
YES=0
SPLIT=""
OUTPUT_DIR="."
CHUNKSIZE=100M
for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-h" ]] && helpexit
@@ -88,12 +90,17 @@ for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
[[ "${!i}" = "--split" ]] && { i=$(( i + 1 )); SPLIT="${!i}"; continue; }
[[ "${!i}" = "--chunk" ]] && { i=$(( i + 1 )); CHUNKSIZE="${!i}"; continue; }
[[ "${!i}" = "--output" ]] && { i=$(( i + 1 )); OUTPUT_DIR="${!i}"; continue; }
[[ "${!i}" = "-"* ]] && helpexit
FOLDERS+=( "${!i%/}" )
DEPTH=0
}
[[ ${#FOLDERS[@]} -eq 0 ]] && FOLDERS+=( "." )
which pv &>/dev/null || { echo pv command missing; exit 1; }
if [[ ! -e "$OUTPUT_DIR" ]]; then
echo "Output folder must exist: $OUTPUT_DIR"
exit 1
fi
if [[ -n "$SPLIT" ]]; then
which split &>/dev/null || { echo split command missing; exit 1; }
if [[ "$SPLIT" =~ ^[0-9]+$ ]] || [[ "$SPLIT" =~ ^[0-9]+[KMGTPEZ]$ ]] || [[ "$SPLIT" =~ ^[0-9]+[KMGTPEZ]B$ ]]; then
@@ -138,36 +145,36 @@ for d in "${REALFOLDERS[@]}"; do
printf "$d"
SIZE=$( find "$d" -type f -printf %s"\n" | count_size "$d" )
if [[ -n "$SPLIT" ]]; then
STORECMD="| split -d -a 4 -b $SPLIT - $d.${SUFFIX}."
STORECMD="| split -d -a 4 -b $SPLIT - ${OUTPUT_DIR}/$d.${SUFFIX}."
else
STORECMD="> \"$d.${SUFFIX}\""
STORECMD="> \"${OUTPUT_DIR}/$d.${SUFFIX}\""
fi
printf "\n"
eval "tar cvv $SORTFILES --index-file=\"$d.${SUFFIX}.lst\" \"$d\" | \
eval "tar cvv $SORTFILES --index-file=\"${OUTPUT_DIR}/$d.${SUFFIX}.lst\" \"$d\" | \
pv -s \"$SIZE\" $COMPRESSCMD $CHUNKCMD $STORECMD" && {
# tar exists okay
NEWEST="$( awk '{ print $4 " " $5 }' "$d.${SUFFIX}.lst" | sort | tail -n 1 )"
NEWEST="$( awk '{ print $4 " " $5 }' "${OUTPUT_DIR}/$d.${SUFFIX}.lst" | sort | tail -n 1 )"
if [[ -n "$SPLIT" ]]; then
touch \
-d "$NEWEST" \
"$d.${SUFFIX}".*[0123456789] \
"$d.${SUFFIX}.lst"
"${OUTPUT_DIR}/$d.${SUFFIX}".*[0123456789] \
"${OUTPUT_DIR}/$d.${SUFFIX}.lst"
shopt -s nullglob
set -- "$d.${SUFFIX}".*[0123456789]
set -- "${OUTPUT_DIR}/$d.${SUFFIX}".*[0123456789]
if [[ $# -eq 1 ]]; then
echo "Split produced only one file:"
mv -v "$d.${SUFFIX}".*[0123456789] "$d.${SUFFIX}"
mv -v "${OUTPUT_DIR}/$d.${SUFFIX}".*[0123456789] "${OUTPUT_DIR}/$d.${SUFFIX}"
fi
shopt -u nullglob
else
touch \
-d "$NEWEST" \
"$d.${SUFFIX}" \
"$d.${SUFFIX}.lst"
"${OUTPUT_DIR}/$d.${SUFFIX}" \
"${OUTPUT_DIR}/$d.${SUFFIX}.lst"
fi
if [ "$KEEP_LIST" -eq 0 ]; then
rm -f "$d.${SUFFIX}.lst"
rm -f "${OUTPUT_DIR}/$d.${SUFFIX}.lst"
fi
if [ "$REMOVE" -eq 1 ]; then
printf "${Y}Removing folder $d$Z\n"