diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index 675d55b..25f5488 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -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. - echo -e "${color_green}${status}: Done.${color_default}" + 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. - echo -e "${color_green}${status}: Done.${color_default}" + 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.