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