Gitbackup export facility and minor improvements.
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2026-04-12 v0.4
|
||||
# mod: The PAR_BACKUPDIR environment variable changed to PAR_SOURCEDIR.
|
||||
# mod: Introduced the SOURCEPATH environment variable to avoid hardcoding.
|
||||
# 2025-05-21 v0.3.1
|
||||
# fix: Wrong variable name (BASE_DIR instead of SERVICE_BASE) in a check.
|
||||
# 2025-05-20 v0.3
|
||||
@@ -26,7 +29,7 @@ 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
|
||||
SOURCEDIR=${PAR_SOURCEDIR:-""} # Folder to backup into git
|
||||
|
||||
# Basic environment settings.
|
||||
#
|
||||
@@ -48,6 +51,7 @@ MSG_WRONGGIT="Fatal: unusable backup (git) folder"
|
||||
# Other initialisations.
|
||||
#
|
||||
GITPATH="storage/backups/webcontent"
|
||||
SOURCEPATH="storage/volumes/mediawiki_images"
|
||||
YMLFILE="docker-compose.yml"
|
||||
|
||||
# Checks the dependencies.
|
||||
@@ -74,7 +78,7 @@ while [ -h "$SOURCE" ]; do
|
||||
# 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 >/dev/null 2>&1; echo $?) -eq 0 ]; then
|
||||
# We'll use v2 if it is available.
|
||||
@@ -104,14 +108,12 @@ TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
|
||||
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"
|
||||
[[ -z "$SOURCEDIR" ]] && SOURCEDIR="$SERVICE_BASE/$SOURCEPATH"
|
||||
# Gives up here if doesn't found.
|
||||
if [ -z "$SOURCEDIR" -o ! -d "$("$READLINK" -e "$SOURCEDIR")" ]; then
|
||||
echo "$MSG_MISSINGSOURCE $SOURCEDIR"; exit 1
|
||||
@@ -150,6 +152,7 @@ if [ ! -d "$GITDIR/.git" ]; then
|
||||
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.
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/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-04-12 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="mediawiki" # The composed MediaWiki 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! :)
|
||||
Reference in New Issue
Block a user