bash : archive : add timestamp to the new format & converter.

This commit is contained in:
Dmitry Voronin 2023-10-31 21:10:13 +03:00
parent 910ff05804
commit 25930e35a4

View file

@ -1,4 +1,5 @@
_ARCHIVE_PATTERN="_[[:alnum:]]{40}.tar.[xg]z" _ARCHIVE_PATTERN="_[0-9]{12}-[[:alnum:]]{40}.tar.[xg]z"
alias _ARCHIVE_DATE="date +%Y%m%d%H%M"
# archive file with maximum compression and checksum. # archive file with maximum compression and checksum.
# usage: archive [FILES] # usage: archive [FILES]
@ -7,6 +8,7 @@ archive()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -27,7 +29,7 @@ archive()
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".tar.xz tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".tar.xz
# append hash to target name. # append hash to target name.
mv "${target%/*}".tar.xz "${target%/*}"_$(sha1sum "${target%/*}".tar.xz | cut -d\ -f1).tar.xz mv "${target%/*}".tar.xz "${target%/*}"_${date}-$(sha1sum "${target%/*}".tar.xz | cut -d\ -f1).tar.xz
done done
} }
@ -38,6 +40,7 @@ archive_fast()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -58,7 +61,7 @@ archive_fast()
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tar.gz tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tar.gz
# append hash to target name. # append hash to target name.
mv "${target%/*}".tar.gz "${target%/*}"_$(sha1sum "${target%/*}".tar.gz | cut -d\ -f1).tar.gz mv "${target%/*}".tar.gz "${target%/*}"_${date}-$(sha1sum "${target%/*}".tar.gz | cut -d\ -f1).tar.gz
done done
} }
@ -73,7 +76,7 @@ archive_check()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(${_ARCHIVE_PATTERN}) targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -89,7 +92,7 @@ archive_check()
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
# extract hash from name. # extract hash from name.
local saved="${target##*_}" local saved="${target##*-}"
saved="${saved%%.*}" saved="${saved%%.*}"
# calculate actual hash. # calculate actual hash.
@ -124,7 +127,7 @@ unarchive()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(${_ARCHIVE_PATTERN}) targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -137,7 +140,7 @@ unarchive()
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
# extract hash from name. # extract hash from name.
local saved="${target##*_}" local saved="${target##*-}"
saved="${saved%%.*}" saved="${saved%%.*}"
# calculate actual hash. # calculate actual hash.
@ -164,7 +167,7 @@ archive_name()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(${_ARCHIVE_PATTERN}) targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -206,6 +209,24 @@ archive_name()
done done
} }
# convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives.
archive_convert()
{
local old_format="_[[:alnum:]]{40}.tar.[xg]z"
local targets=($(ls | grep -E ${old_format}))
for target in "${targets[@]}"; do
local stamp=$(stat --format '%w' -- "${target}" | sed -e 's/\..*//' -e 's/:..$//' -e 's/-//g' -e 's/://' -e 's/\ //')
local name="${target%_*}"
local old_data="${target##*_}"
local new_name="${name}_${stamp}-${old_data}"
echo "${target} -> ${new_name}"
mv "${target}" "${new_name}"
done
}
# export everything, primarily for use with parallel.. # export everything, primarily for use with parallel..
export -f archive archive_fast archive_check unarchive archive_name export -f archive archive_fast archive_check unarchive archive_name