mirror of
https://github.com/deuill/coreos-home-server.git
synced 2024-09-21 21:50:46 +00:00
Alex Palaistras
dad3d1c12c
Container volume backup logic has been moved to a (largely equivalent) external script, allowing for future expansion of functionality. In addition, a `rclone-pull@` service has been added and set up as a default dependency for the `container-volume-restore@` service, allowing for automatic set up of servers based on latest remote backups.
53 lines
1.6 KiB
Bash
Executable File
53 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Archive all files in source directory into destination directory.
|
|
|
|
set -euo pipefail
|
|
|
|
# Parse command-line arguments.
|
|
unset SRCDIR DESTDIR BASENAME
|
|
|
|
while getopts 's:d:n:' arg; do
|
|
case "${arg}" in
|
|
s) SRCDIR="${OPTARG}" ;;
|
|
d) DESTDIR="${OPTARG}" ;;
|
|
n) BASENAME="${OPTARG}-" ;;
|
|
*) echo "Unknown argument '${arg}' given. Exiting." >&2 && exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
# Check and return true if source files are newer than any archive already created.
|
|
function archive-should-update() {
|
|
local latest="${DESTDIR}/${BASENAME}latest.tar.gz"
|
|
|
|
if ! test -e "${latest}"; then
|
|
return 0
|
|
elif test "$(find "${SRCDIR}" -type f -newer "${latest}" -print -quit | wc -l)" -gt 0; then
|
|
return 0
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
# Create compressed archive for source path into destination path for pre-defined, date-based name.
|
|
function archive-create() {
|
|
local name="${BASENAME}$(date +%w%H)"
|
|
|
|
# Create uncompressed archive for source path and compress separately to ensure minimal blocking.
|
|
tar --verbose --create --file "${DESTDIR}/${name}.tar" --directory "${SRCDIR}" .
|
|
gzip --force "${DESTDIR}/${name}.tar"
|
|
|
|
# Point the latest archive to a "special" name for future use.
|
|
(cd /backup && ln --symbolic --force "${name}.tar.gz" "${BASENAME}latest.tar.gz")
|
|
}
|
|
|
|
# Entry-point for script. Create tar archive for source directory into destination directory, checking
|
|
# for any changes since last run, and ensuring files are archived correctly for subsequent restore.
|
|
function main() {
|
|
if archive-should-update "${SRCDIR}" "${DESTDIR}"; then
|
|
archive-create "${SRCDIR}" "${DESTDIR}"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|