From 25930e35a4208dddd6c53eba61274ea5a087914d Mon Sep 17 00:00:00 2001 From: desktop Date: Tue, 31 Oct 2023 21:10:13 +0300 Subject: [PATCH] bash : archive : add timestamp to the new format & converter. --- .linux/bash/module/archive.sh | 49 +++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/.linux/bash/module/archive.sh b/.linux/bash/module/archive.sh index f2e3cfc..25ea876 100644 --- a/.linux/bash/module/archive.sh +++ b/.linux/bash/module/archive.sh @@ -1,12 +1,14 @@ -_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. # usage: archive [FILES] archive() { - local targets=("${@}") # target file(s). - local count=0 # processed count. - local total=${#} # total to process. + local targets=("${@}") # target file(s). + local count=0 # processed count. + local total=${#} # total to process. + local date=$(_ARCHIVE_DATE) # date stamp. # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then @@ -27,7 +29,7 @@ archive() tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".tar.xz # 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 } @@ -35,9 +37,10 @@ archive() # usage: archive_fast [FILES] archive_fast() { - local targets=("${@}") # target file(s). - local count=0 # processed count. - local total=${#} # total to process. + local targets=("${@}") # target file(s). + local count=0 # processed count. + local total=${#} # total to process. + local date=$(_ARCHIVE_DATE) # date stamp. # set dafult value to target all supported archives. 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 # 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 } @@ -73,7 +76,7 @@ archive_check() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(${_ARCHIVE_PATTERN}) + targets=($(ls | grep -E ${_ARCHIVE_PATTERN})) total=${#targets[@]} fi @@ -89,7 +92,7 @@ archive_check() local status="[${count}/${total}] ${target}" # extract hash from name. - local saved="${target##*_}" + local saved="${target##*-}" saved="${saved%%.*}" # calculate actual hash. @@ -124,7 +127,7 @@ unarchive() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(${_ARCHIVE_PATTERN}) + targets=($(ls | grep -E ${_ARCHIVE_PATTERN})) total=${#targets[@]} fi @@ -137,7 +140,7 @@ unarchive() local status="[${count}/${total}] ${target}" # extract hash from name. - local saved="${target##*_}" + local saved="${target##*-}" saved="${saved%%.*}" # calculate actual hash. @@ -164,7 +167,7 @@ archive_name() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(${_ARCHIVE_PATTERN}) + targets=($(ls | grep -E ${_ARCHIVE_PATTERN})) total=${#targets[@]} fi @@ -206,6 +209,24 @@ archive_name() 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 -f archive archive_fast archive_check unarchive archive_name