www/bin/forgejo_backup.sh

66 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Create Forgejo backups
#
# Prep
# groupadd --system bkp
# mkdir /var/xyzzy/backup
# chmod 0750 /var/xyzzy/backup
# chown git:bkp /var/xyzzy/backup
#
# SPDX-License-Identifier: MIT
_VERSION="0.0.2"
BDTS=$(date +"%Y%m%d%H%M")
BDIR="/var/xyzzy/backup"
BFILE="${BDIR}/forgejo-${BDTS}"
BDMP="${BDIR}/forgejo-${BDTS}.tar.xz"
BGRP="bkp"
GCNF="/var/xyzzy/etc/forgejo/app.ini"
GBIN="/var/xyzzy/bin/forgejo"
GDATA="/var/xyzzy/forge"
# healthchecks.io ping URL upon success, uses curl - "none" to disable
HCPING="none"
# delete backups older than
# see 'man find'; "+3" = 3*24h ago
BDEL="-mtime +3"
# we need to chgrp/chmod to a foreign group
if [[ $(id -u) -ne 0 ]]; then
echo "Must run as root user"
exit 1
fi
# just to be sure
cd "${BDIR}" || (echo "Cannot cd to ${BDIR}"; exit 1)
# runuser exits with the error code of the command
_EC=1
runuser -u git -- "${GBIN}" dump --config "${GCNF}" --tempdir "${BDIR}" \
--work-path "${GDATA}" --skip-log --type tar.xz --file "${BFILE}"
_EC=$?
# post processing
if [[ $_EC -eq 0 ]]; then
# forgejo dump writes git:git 0600
if [[ -f "${BDMP}" ]]; then
chgrp "${BGRP}" "${BDMP}"
chmod 0640 "${BDMP}"
fi
# delete older than BDEL backups
# shellcheck disable=SC2086
find "${BDIR}" -type f ${BDEL} \
-regextype egrep \
-regex '.*/forgejo-[0-9]{12}\.tar\.xz' \
-delete
# ping healthchecks.io
if [[ "${HCPING}" != "none" ]]; then
curl -fsS -m 10 --retry 5 -o /dev/null "${HCPING}"
fi
fi