Initial commit - forked from the corresponding Smartfront repositories.
This commit is contained in:
78
tools/backup.d/configs_backup.sh
Executable file
78
tools/backup.d/configs_backup.sh
Executable file
@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Backups the configuration files of this docker-composed service.
|
||||
# This is a general purpose worker script, doesn't requires customization.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2021-09-03 v0.1 Initial release
|
||||
|
||||
# Accepted environment variables and their defaults.
|
||||
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
|
||||
PAR_BACKUPDIR=${PAR_BACKUPDIR:-""} # Folder to dump within
|
||||
|
||||
# Other initialisations.
|
||||
BACKUPDIR="storage/backups/tarballs" # Folder to dump within
|
||||
USER=${USER:-LOGNAME} # Fix for cron enviroment only
|
||||
YMLFILE="docker-compose.yml"
|
||||
|
||||
# Messages.
|
||||
MSG_MISSINGDEP="Fatal: missing dependency"
|
||||
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
|
||||
MSG_NONWRITE="The target directory isn't writable"
|
||||
|
||||
# Checks the dependencies.
|
||||
TR=$(which tr 2>/dev/null)
|
||||
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
|
||||
for item in date dirname hostname readlink tar
|
||||
do
|
||||
if [ -n "$(which $item)" ]
|
||||
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item)
|
||||
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
|
||||
done
|
||||
|
||||
# Where I'm?
|
||||
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
|
||||
SOURCE="$0"
|
||||
while [ -h "$SOURCE" ]; do
|
||||
# resolve $SOURCE until the file is no longer a symlink
|
||||
SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #"
|
||||
SOURCE="$("$READLINK" "$SOURCE")"
|
||||
# if $SOURCE was a relative symlink, we need to resolve it
|
||||
# relative to the path where the symlink file was located
|
||||
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
|
||||
done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #"
|
||||
|
||||
# Searches the base folder, containing a docker-compose.yml file.
|
||||
# Called from the base folder (./)?
|
||||
BASE_DIR="$PAR_BASEDIR"
|
||||
TEST_DIR="$SCRPATH"
|
||||
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
||||
# Called from ./tools?
|
||||
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
|
||||
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
||||
# Called from ./tools/*.d?
|
||||
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
|
||||
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
||||
# On failure gives it up here.
|
||||
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
|
||||
echo "$MSG_MISSINGYML" >&2; exit 1
|
||||
fi
|
||||
# Sets the absolute paths.
|
||||
BACKUPDIR="${PAR_BACKUPDIR:-$BASE_DIR/$BACKUPDIR}"
|
||||
|
||||
# The dump target folder must be writable.
|
||||
[[ ! -w "$BACKUPDIR" ]] \
|
||||
&& echo "$MSG_NONWRITE: $BACKUPDIR" >&2 && exit 1
|
||||
|
||||
# Tries the FS backup.
|
||||
if [ -w "$BACKUPDIR" ]; then
|
||||
BACKUP_NAME="configs.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")"
|
||||
( cd "$BASE_DIR"
|
||||
"$TAR" czf "$BACKUPDIR/$BACKUP_NAME.tgz" \
|
||||
"$YMLFILE" configs docker \
|
||||
2>>"$BACKUPDIR/$BACKUP_NAME.log"
|
||||
)
|
||||
fi
|
||||
|
||||
# That's all, Folks! :)
|
Reference in New Issue
Block a user