2
0

163 lines
6.0 KiB
Bash

#!/bin/bash
#
# Humble tool to commit the content of a docker-composed MediaWiki service's
# upload files into a git repository to make a daily backup of documents.
# Also makes the repository with a metastore file if doesn't exist yet.
#
# This script called usually by the cron (but indirectly).
# Depends loosely on metastore package, which isn't absolutely necessary,
# but strongly recommended to backup file time attributes and permissions,
# which the git tool doesn't do.
#
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
# 2024-08-25 v0.2
# new: docker-compose v2 compatibility - tested with Ubuntu 24.04 LTS.
# mod: Doesn't do backup if the service is down.
# 2021-11-21 v0.1 Initial release
# Accepted environment variables and their defaults.
#
BOTEMAIL=${BOTEMAIL:-"backupbot@example.com"} # Git repo owner's email (fake)
BOTNAME=${BOTNAME:-"Backup Bot"} # Git repo owner's name (fake)
SERVICE_BASE=${PAR_BASEDIR:-""} # Corresponding service's base
GITDIR=${PAR_GITDIR:-""} # Folder containing .git
SOURCEDIR=${PAR_BACKUPDIR:-""} # Folder to backup into git
# Basic environment settings.
#
LANG=C
LC_ALL=C
# We need also the sbin directories.
if ! [[ "$PATH" =~ '/sbin:' ]]; then
PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"; fi
# Messages.
#
MSG_GITCOMMIT="Automated backup"
MSG_MISSINGBASE="Fatal: missing SERVICE_BASE"
MSG_MISSINGDEP="Fatal: missing dependency"
MSG_MISSINGGIT="Fatal: unable to find the backup (git) folder"
MSG_MISSINGSOURCE="Fatal: unable to find the source folder"
MSG_WRONGGIT="Fatal: unusable backup (git) folder"
# Other initialisations.
#
GITPATH="storage/backups/webcontent"
YMLFILE="docker-compose.yml"
# Checks the dependencies.
#
TR=$(which tr 2>/dev/null)
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
for item in cut date dirname docker git readlink
do
if [ -n "$(which $item)" ]
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]')=$(which $item)
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
done
# All dependencies are available via "$THECOMMAND" (upper case) call.
# 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" )" && echo "$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" )" && echo "$PWD" )" #"
# 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.
# Searches the base folder, containing a docker-compose.yml file.
#
# Called from the base folder (./)?
TEST_DIR="$SCRPATH"
[[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR"
# Called from ./tools?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR"
# Called from ./tools/*.d?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$SERVICE_BASE" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && SERVICE_BASE="$TEST_DIR"
# On failure gives it up here.
if [ -z "$SERVICE_BASE" -o ! -r "$SERVICE_BASE/$YMLFILE" ]; then
echo "$MSG_MISSINGYML" >&2; exit 1
fi
# Sets the absolute paths.
BACKUPDIR="${PAR_BACKUPDIR:-$SERVICE_BASE/$BACKUPDIR}"
# Locates the source folder.
#
# Maybe given as a command line parameter.
[[ -n "$1" ]] && SOURCEDIR="$1" && shift
[[ -z "$SOURCEDIR" ]] && SOURCEDIR="$SERVICE_BASE/storage/volumes/wordpress_html"
# Gives up here if doesn't found.
if [ -z "$SOURCEDIR" -o ! -d "$("$READLINK" -e "$SOURCEDIR")" ]; then
echo "$MSG_MISSINGSOURCE $SOURCEDIR"; exit 1
fi
# Locates the backup (git) folder.
#
# Maybe given as a command line parameter.
[[ -n "$1" ]] && GITDIR="$1" && shift
[[ -z "$GITDIR" ]] && GITDIR="$SERVICE_BASE/$GITPATH"
# Gives up here if doesn't found.
if [ -z "$GITDIR" -o ! -d "$("$READLINK" -e "$GITDIR")" ]; then
echo "$MSG_MISSINGGIT $GITDIR"; exit 1
fi
# Does it writable?
( cd "$GITDIR" 2>/dev/null
if [ ! "$PWD" = "$GITDIR" -o ! -w "$PWD" ]; then
echo "$MSG_WRONGGIT $GITDIR"; exit 1
fi
) || exit 1
# We've the folders localized.
# The service must be running - silently gives up here if not.
#
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \
&& exit 1
# Attempts the backup commit.
#
# Initializes the git backup if it doesn't exist yet.
if [ ! -d "$GITDIR/.git" ]; then
# Initializes the repo itself.
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" init --quiet
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" config user.name "$BOTNAME"
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" config user.email "$BOTEMAIL"
fi
# Stages all the files and non-empty folders.
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" add . >/dev/null
# Stores the file system metadata as well, if the tool has been installed.
if [ ! -z "$(which metastore)" -a -x "$(which metastore)" ]; then
# This commamd silently creates the metastore file if it doesnt' exist yet.
( cd "$SOURCEDIR"
"$(which metastore)" -smqq --file ".metadata"
)
# Stages it as well.
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" add ".metadata" >/dev/null
fi
# Makes the commit.
"$GIT" --git-dir="$GITDIR/.git" --work-tree="$SOURCEDIR" commit --quiet -m "'$MSG_GITCOMMIT $("$DATE" '+%Y%m%d-%H%M%S')'"
# Git done.
# That's all, Folks! :)