Last active
July 4, 2016 20:28
-
-
Save mark-kubacki/2ef600e906dad13711dea1dc6293e649 to your computer and use it in GitHub Desktop.
naive multi-processing backup script in BASH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Backup script that tries to utilize all available disc spindles and cores. | |
# For every target archive ("sink") create a file "sink.list" | |
# which contains what to backup ("sources") linewise. | |
# (Prefer plzip to xz if you have the former at hand.) | |
# | |
# wrapper to: tar sort xargs xz | |
# | |
# run: ./backup.sh run *.list | |
# | |
# author: W-Mark Kubacki <[email protected]> | |
set -euo pipefail | |
: ${now:="$(date --iso-8601 --utc)"} | |
: ${spindles:=2} | |
: ${jobs:="$(nproc --ignore=1)"} | |
if (($# < 1)); then | |
>&2 printf "argument expected, but not given\n" | |
exit 2 | |
fi | |
case "${1}" in | |
compress) | |
if (($# < 2)); then | |
# xargs runs this even if there are no params | |
exit 0 | |
fi | |
if ! [[ -s "${2}" ]]; then | |
>&2 printf "tarball not found: ${2}\n" | |
exit 4 | |
fi | |
exec xz "${2}" | |
;; | |
tar) | |
if (($# < 3)); then exit 0; fi | |
: ${tarball="${2}/${3//.list}.tar"} | |
if [[ -s "${tarball}.xz" ]]; then | |
exit 0 | |
fi | |
if [[ -s "${tarball}" ]]; then | |
printf "${tarball}\n" | |
exit 0 | |
fi | |
tar -cf "${tarball}" "${3}" $(cat "${3}") | |
printf "${tarball}\n" | |
;; | |
do|run) | |
mkdir -p "${now}/$(hostname)" | |
shift | |
for LISTFILE; do | |
printf "${LISTFILE}\n" | |
done \ | |
| sort -h \ | |
| xargs --max-args=1 --max-procs=${spindles} "${0}" tar "${now}/$(hostname)" \ | |
| xargs --max-args=1 --max-procs=${jobs} "${0}" compress | |
;; | |
*) | |
printf "${0} run [file...]\n" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment