archive : fix prune.

This commit is contained in:
Dmitry Voronin 2023-11-22 13:49:12 +03:00
parent 4f68139a17
commit f4f1a0664e

View file

@ -11,6 +11,7 @@ archive()
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local failed=0
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
@ -30,13 +31,17 @@ archive()
echo -e "${status}"
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz || ((failed++))
# append hash to target name.
mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz
mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz || ((failed++))
# report success.
if [[ ${failed} = 0 ]]; then
echo -e "${color_green}${status}: Done.${color_default}"
else
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
}
@ -47,6 +52,7 @@ archive_fast()
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local failed=0
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
@ -66,13 +72,17 @@ archive_fast()
echo -e "${status}"
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz || ((failed++))
# append hash to target name.
mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz
mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz || ((failed++))
# report success.
if [[ ${failed} = 0 ]]; then
echo -e "${color_green}${status}: Done.${color_default}"
else
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
}
@ -134,25 +144,46 @@ archive_prune()
local IFS=$'\n'
local targets=("${@}")
local count=0
local failed=0
local total=${#}
# All archives by default.
if [[ "${target}" = "" ]]; then
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# Iterate each target.
for target in "${targets[@]}"; do
# Only work with files.
# Only work with existing files.
[[ -f "${target}" ]] || continue
# Iterate counter.
((count++))
local name="${target%_*}"
# local copies=("$()")
local data="${target##*_}"
local time="${data%%-*}"
local copies=($(ls ${name}_*))
# Iterate each copy.
for copy in "${copies[@]}"; do
local copy_data="${copy##*_}"
local copy_time="${copy_data%%-*}"
if [[ "${copy_time}" -lt "${time}" ]]; then
echo -e "${name}: prune ${copy_time}."
rm -- "${copy}" || ((failed++))
fi
done
done
# Report result.
if [[ ${failed} = 0 ]]; then
echo -e "${color_green}All successful.${color_default}"
else
echo -e "${color_bred}Items failed to prune: ${failed}.${color_default}"
fi
}
# extract previously created archive with checksum validation.