diff --git a/.config/bash/module/Archive.sh b/.config/bash/module/Archive.sh index 8c5f5e6..3df10c2 100644 --- a/.config/bash/module/Archive.sh +++ b/.config/bash/module/Archive.sh @@ -26,6 +26,35 @@ function archive() { _iterate_targets process ${targets[@]} } +# Archive using multiple threads. Uses 50% of free RAM. +# All directories by default. +# Usage: archive_mt [DIRS] +function archive_mt() { + local IFS=$'\n' + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(_ls_dir)) + + process() { + local date=$(_archive_date) + + # Parse name. + local name=$(parse_pascal ${target}) + + # Determine memory limit. + local mem_free=$(_mem_free) + local mem_limit=$((mem_free/2)) + + # create archive. + local hash=$(tar -c ${target} | pv -s $(/usr/bin/du -sb ${target} | awk '{print $1}') | xz -9e --threads=0 --memlimit=${mem_limit}MiB | tee ${name}.txz | sha1sum | cut -d\ -f1) + + # append hash to target name. + local new_name="${name}_${date}-${hash}.txz" + mv -- ${name}.txz ${new_name} && echo ${new_name} + } + + _iterate_targets process ${targets[@]} +} + # Archive directories with fast compression. # All directories by default. # Usage: archive_fast [DIRS] diff --git a/.config/bash/module/Util.sh b/.config/bash/module/Util.sh index 041c527..ac2a6d9 100644 --- a/.config/bash/module/Util.sh +++ b/.config/bash/module/Util.sh @@ -3,6 +3,11 @@ function _core_count() { cat /proc/cpuinfo | grep ^processor | wc -l } +# Get the number of available memory (in mebibytes). +function _mem_free() { + free -m | sed -n -e '2p' | awk '{print $7}' +} + # Parse integers from mixed string. function _parse_ints() { echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'