ansible-role--duplicity/files/duplicity_helper_for_dokku

111 lines
2.7 KiB
Text
Raw Permalink Normal View History

2024-07-12 12:36:06 +00:00
#!/bin/sh
# vim: set ts=2 sw=2 et:
set -u
set -e
APP_DOKKU_ROOT=/home/dokku
APP_VOLUMES_ROOT=/var/lib/dokku/data/storage
APP_SERVICE_ROOT=/var/lib/dokku/services
if [ -r /etc/duplicity/config ]; then
. /etc/duplicity/config
else
echo "Unable to read config file"
fi
DUPLICITY_HOSTNAME="${DUPLICITY_HOSTNAME:-}"
DUPLICITY_WORK_DIR="${DUPLICITY_WORK_DIR:-}"
DUPLICITY_ENABLE="${DUPLICITY_ENABLE:-}"
make_archive() {
app="$1"
dir="$2"
age="7D"
echo "Creating backup for $app..."
echo "- data dir $dir"
if [ ! -d "$dir" ]; then
>&2 echo "ERROR: missing data dir $dir"
exit 1
fi
if [ "$DUPLICITY_ENABLE" != "true" ]; then
echo "- dryrun mode (skipping the real work)"
return
fi
duplicity \
--file-prefix-manifest "hot_${app}_" \
--file-prefix-signature "hot_${app}_" \
--file-prefix-archive "cold_${app}_" \
--full-if-older-than "$age" \
"$dir" \
'multi:///root/.duplicity/config.json?mode=mirror&onfail=abort'
}
clean_archive() {
age="1D"
echo "Removing old backups (> $age)"
if [ "$DUPLICITY_ENABLE" != "true" ]; then
echo "- dryrun mode (skipping the real work)"
return
fi
duplicity \
remove-older-than "$age" \
--force 'multi:///root/.duplicity/config.json?mode=mirror&onfail=abort'
}
## VALIDATE INPUT
if [ -z "$DUPLICITY_HOSTNAME" ]; then
>&2 echo "ERROR: Variable DUPLICITY_HOSTNAME must be declared in /etc/duplicity/config"
exit 1
fi
if [ -z "$DUPLICITY_WORK_DIR" ]; then
>&2 echo "ERROR: Variable DUPLICITY_WORK_DIR must be declared in /etc/duplicity/config"
exit 1
fi
PASSPHRASE="$(cat /etc/duplicity/gpg_backup_passphrase)"
TMPDIR="$DUPLICITY_WORK_DIR"
# /mnt/one.data.infra.glenux.net/duplicity
export PASSPHRASE
export TMPDIR
## DOKKU DATA
for app in $( dokku apps:list |tail -n+2 |xargs echo ) ; do
NAME="DOKKU_${app}@${DUPLICITY_HOSTNAME}"
DATA_DIR="$APP_DOKKU_ROOT/$app"
make_archive "$NAME" "$DATA_DIR"
done
## VOLUMES DATA
for app in $( dokku apps:list |tail -n+2 |xargs echo ) ; do
NAME="VOLUMES_${app}@${DUPLICITY_HOSTNAME}"
DATA_DIR="$APP_VOLUMES_ROOT/$app"
make_archive "$NAME" "$DATA_DIR"
done
## MARIADB DATA
for app in $( dokku mariadb:list |tail -n+2 |awk '{ print $1; }' |xargs echo ) ; do
NAME="MARIADB_$(dokku mariadb:list |grep "^$app " |awk '{ print $1 "@" $5; }')"
DATA_DIR="$APP_SERVICE_ROOT/mariadb/$app"
make_archive "$NAME" "$DATA_DIR"
done
## POSTGRES DATA
for app in $( dokku postgres:list |tail -n+2 |awk '{ print $1; }' |xargs echo ) ; do
NAME="POSTGRES_$(dokku postgres:list |grep "^$app " |awk '{ print $1 "@" $5; }')"
DATA_DIR="$APP_SERVICE_ROOT/postgres/$app"
make_archive "$NAME" "$DATA_DIR"
done
clean_archive