136 lines
5.1 KiB
Bash
136 lines
5.1 KiB
Bash
#!/bin/bash
|
|
#
|
|
# An optional additional backup operation designed to export a
|
|
# Time Machine-style, git-based backup for our customers of their data
|
|
# we manage. The script compresses the local git backup repository into
|
|
# a tarball that can be downloaded from the web.
|
|
#
|
|
# This script has special naming conventions:
|
|
# - If the script file name (without extension) ends in "_w", the script will
|
|
# only run on Sundays.
|
|
# - If the script file name (without extension) ends in "_m", the script will
|
|
# only run on the first day of the month.
|
|
#
|
|
# Author: Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
|
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
|
# 2026-03-03 v0.1 Initial release
|
|
|
|
# Accepted environment variables and their defaults.
|
|
#
|
|
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
|
|
PAR_EXPORTDIR=${PAR_EXPORTDIR:-""} # Absolute path to export dir
|
|
PAR_GITDIR=${PAR_GITDIR:-""} # Absolute path to tgz dumps
|
|
|
|
# Other initialisations.
|
|
#
|
|
EXPORTPATH="storage/backups/export" # Default path to export dir
|
|
GITPATH="storage/backups/webcontent" # Default path to git repository
|
|
SERVICENAME="redmine" # The composed Redmine service
|
|
USER=${USER:-LOGNAME} # Fix for cron enviroment only
|
|
YMLFILE="docker-compose.yml"
|
|
|
|
# Messages.
|
|
#
|
|
MSG_DOCKERGRPNEED="You must be a member of the docker group."
|
|
MSG_DOESNOTRUN="This service doesn't run."
|
|
MSG_MISSINGDEP="Fatal: missing dependency"
|
|
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
|
|
MSG_NOLOCATE="Cannot locate the service container."
|
|
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 basename cp cut date dirname docker grep hostname id 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
|
|
# All dependencies are available via "$THECOMMAND" (upper case) call.
|
|
#
|
|
# Let's find which version of docker-compose is installed.
|
|
if [ $($DOCKER compose version 2>&1 >/dev/null; echo $?) -eq 0 ]; then
|
|
# We'll use v2 if it is available.
|
|
DOCKER_COMPOSE="$DOCKER"
|
|
commandstring="compose"
|
|
else
|
|
# Otherwise falling back to v1.
|
|
DOCKER_COMPOSE="$(which docker-compose)"
|
|
commandstring=""
|
|
fi
|
|
# One of the two is mandatory.
|
|
if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi
|
|
# Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence.
|
|
|
|
# Checks the timing conventions.
|
|
#
|
|
# Last two characters of the filename.
|
|
TIMING="$("$BASENAME" "$0")"; TIMING="${TIMING%.*}"; TIMING="${TIMING:0-2}"
|
|
# If this "_w" continues only on Sundays.
|
|
[ "$TIMING" = "_w" ] && [[ $("$DATE" +%u) -ne 0 ]] && exit 0
|
|
# If this "_m" continues only on the 1st day of the month.
|
|
[ "$TIMING" = "_m" ] && [[ $("$DATE" +%d) -ne 1 ]] && exit 0
|
|
# There is no time limit, go on.
|
|
|
|
# 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 )" #"
|
|
|
|
# Need to be root or a Docker manager user.
|
|
#
|
|
[[ "$USER" != 'root' ]] \
|
|
&& [[ -z "$(echo "$("$ID" -Gn "$USER") " | "$GREP" ' docker ')" ]] \
|
|
&& echo "$MSG_DOCKERGRPNEED" >&2 && exit 1 #"
|
|
|
|
# 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.
|
|
EXPORTDIR="${PAR_EXPORTDIR:-$BASE_DIR/$EXPORTPATH}"
|
|
GITDIR="${PAR_GITDIR:-$BASE_DIR/$GITPATH}"
|
|
# Exits silently if EXPORTDIR or GITDIR isn't present.
|
|
[[ ! -e "$EXPORTDIR" ]] && exit 0
|
|
[[ ! -e "$GITDIR" ]] && exit 0
|
|
# EXPORTDIR must be writable.
|
|
[[ ! -w "$EXPORTDIR" ]] \
|
|
&& echo "$MSG_NONWRITE: $EXPORTDIR" >&2 && exit 1
|
|
|
|
# Converts the service name to an actual running container's name.
|
|
#
|
|
MYCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps -q "$SERVICENAME") | "$CUT" -c2-)"
|
|
# Gives up here if failed.
|
|
if [ -z "$MYCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
|
|
|
|
# Tries the backup.
|
|
#
|
|
pushd "$GITDIR" > /dev/null
|
|
EXPORTNAME=$MYCONTAINER-git.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")
|
|
"$TAR" czf "$EXPORTDIR/$EXPORTNAME.tgz" . 2>"$EXPORTDIR/$EXPORTNAME.log"
|
|
popd > /dev/null
|
|
|
|
# That's all, Folks! :)
|