archiver with split

This commit is contained in:
2025-02-05 14:11:52 +02:00
parent e12deaab73
commit f850131713

View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
VERSION="20230307" VERSION="20250205"
function helpexit() { function helpexit() {
BS=$( basename "$0" ) BS=$( basename "$0" )
@@ -12,6 +12,7 @@ function helpexit() {
echo " -y Dont ask before starting the archival." echo " -y Dont ask before starting the archival."
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 " folder name: If given, archive only the named folder(s)." echo " folder name: If given, archive only the named folder(s)."
echo " If not, for loop over all folders in the current directory." echo " If not, for loop over all folders in the current directory."
@@ -67,6 +68,7 @@ FOLDERS=( )
REMOVE=0 REMOVE=0
KEEP_LIST=1 KEEP_LIST=1
YES=0 YES=0
SPLIT=""
for ((i=1; i<=${#@}; i++)) { for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-h" ]] && helpexit [[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "--help" ]] && helpexit
@@ -75,12 +77,16 @@ for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; } [[ "${!i}" = "-z" ]] && { COMPRESSION=1; NOCOMPRESSION=0; COMPRESSCMD="| gzip"; SUFFIX=tgz; continue; }
[[ "${!i}" = "--nl" ]] && { KEEP_LIST=0; continue; } [[ "${!i}" = "--nl" ]] && { KEEP_LIST=0; continue; }
[[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; } [[ "${!i}" = "--rm" ]] && { REMOVE=1; continue; }
[[ "${!i}" = "--split" ]] && { i=$(( i + 1 )); SPLIT="${!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 [[ -n "$SPLIT" ]]; then
which split &>/dev/null || { echo split command missing; exit 1; }
fi
if tar --version | grep -q GNU; then if tar --version | grep -q GNU; then
SORTFILES="--sort=name" SORTFILES="--sort=name"
fi fi
@@ -90,6 +96,7 @@ REALFOLDERS=( $( listfolders "${FOLDERS[@]}" ) )
echo $R'Existing files will be overwritten!'$Z echo $R'Existing files will be overwritten!'$Z
[[ "$REMOVE" -eq 1 ]] && echo 'Source folders will be deleted!' [[ "$REMOVE" -eq 1 ]] && echo 'Source folders will be deleted!'
[[ "$KEEP_LIST" -eq 0 ]] && echo 'File list will be removed.' [[ "$KEEP_LIST" -eq 0 ]] && echo 'File list will be removed.'
[[ -n "$SPLIT" ]] && echo 'Archives split to size: '$SPLIT
echo -n $S"Archive the following folders " echo -n $S"Archive the following folders "
[[ "$NOCOMPRESSION" -eq 1 ]] && { [[ "$NOCOMPRESSION" -eq 1 ]] && {
echo -n without compression; echo -n without compression;
@@ -107,15 +114,27 @@ set -o pipefail
for d in "${REALFOLDERS[@]}"; do 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
STORECMD="| split -d -a 4 -b $SPLIT - $d.${SUFFIX}."
else
STORECMD="> \"$d.${SUFFIX}\""
fi
printf "\n" printf "\n"
eval "tar cvv $SORTFILES --index-file=\"$d.${SUFFIX}.lst\" \"$d\" | \ eval "tar cvv $SORTFILES --index-file=\"$d.${SUFFIX}.lst\" \"$d\" | \
pv -s \"$SIZE\" $COMPRESSCMD > \"$d.${SUFFIX}\"" && { pv -s \"$SIZE\" $COMPRESSCMD $STORECMD" && {
# tar exists okay # tar exists okay
NEWEST="$( awk '{ print $4 " " $5 }' "$d.${SUFFIX}.lst" | sort | tail -n 1 )" NEWEST="$( awk '{ print $4 " " $5 }' "$d.${SUFFIX}.lst" | sort | tail -n 1 )"
touch \ if [[ -n "$SPLIT" ]]; then
-d "$NEWEST" \ touch \
"$d.${SUFFIX}" \ -d "$NEWEST" \
"$d.${SUFFIX}.lst" "$d.${SUFFIX}".*[0123456789] \
"$d.${SUFFIX}.lst"
else
touch \
-d "$NEWEST" \
"$d.${SUFFIX}" \
"$d.${SUFFIX}.lst"
fi
if [ "$KEEP_LIST" -eq 0 ]; then if [ "$KEEP_LIST" -eq 0 ]; then
rm -f "$d.${SUFFIX}.lst" rm -f "$d.${SUFFIX}.lst"