2023-12-03 02:09:46 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
restoreDir=/etc/restore
|
2023-12-13 13:59:28 -05:00
|
|
|
giteaDir="/var/backups/gitea"
|
|
|
|
scriptDir="$(dirname "$0")"
|
2023-12-03 02:09:46 -05:00
|
|
|
|
2023-12-13 13:59:28 -05:00
|
|
|
readConfig() {
|
|
|
|
if [[ -r "${scriptDir}/../config/gitea_backup.cfg" ]]; then
|
|
|
|
source "${scriptDir}/../config/gitea_backup.cfg"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
2023-12-03 02:09:46 -05:00
|
|
|
if [[ -d "$giteaDir" ]]; then
|
2023-12-13 13:59:28 -05:00
|
|
|
echo "Cleaning out old Gitea backups..."
|
|
|
|
rm -f "$giteaDir"/* || return 1
|
|
|
|
else
|
|
|
|
mkdir -p "$giteaDir" | return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$GITEA_BIN" ]]; then
|
|
|
|
if [[ -x "${GITEA_BIN}" ]]; then
|
|
|
|
echo "Found Gitea in $GITEA_BIN"
|
|
|
|
else
|
|
|
|
echo "FATAL: Cannot execute Gitea"
|
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if ! which gitea &>/dev/null; then
|
|
|
|
echo "Cannot find Gitea in PATH"
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
if [[ -x "$(which gitea)" ]]; then
|
|
|
|
GITEA_BIN="$(which gitea)"
|
|
|
|
echo "Found Gita in $GITEA_PATH"
|
|
|
|
else
|
|
|
|
echo "FATAL: Cannot execute Gitea"
|
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
GITEA_CONFIG=${GITEA_CONFIG:-/etc/gitea/app.ini}
|
|
|
|
if [[ ! -r "$GITEA_CONFIG" ]]; then
|
|
|
|
echo "ERROR: Cannot read gitea app.ini in $GITEA_CONFIG"
|
|
|
|
return 4
|
|
|
|
fi
|
|
|
|
|
|
|
|
GITEA_USER=${GITEA_USER:-git}
|
|
|
|
GITEA_HOME=${GITEA_HOME:-$(getent passwd ${GITEA_USER} | cut -f6 -d:)}
|
|
|
|
if [[ ! -d "$GITEA_HOME" ]]; then
|
|
|
|
echo "ERROR: Cannot find Gitea home directory"
|
|
|
|
return 5
|
2023-12-03 02:09:46 -05:00
|
|
|
fi
|
2023-12-13 13:59:28 -05:00
|
|
|
}
|
2023-12-03 02:09:46 -05:00
|
|
|
|
2023-12-13 13:59:28 -05:00
|
|
|
runBackups() {
|
|
|
|
local backupDate
|
|
|
|
|
2023-12-03 02:09:46 -05:00
|
|
|
backupDate=$(date +"%Y-%m-%d")
|
|
|
|
giteaHome=$(getent passwd git | cut -f6 -d:)
|
2023-12-13 14:11:33 -05:00
|
|
|
sudo -u "$GITEA_USER" "${GITEA_BIN}" dump --config "${GITEA_CONFIG}" --type tar.xz --file - > "${giteaDir}/gitea-dump-${backupDate}.tar.xz"
|
2023-12-13 13:59:28 -05:00
|
|
|
}
|
2023-12-03 02:09:46 -05:00
|
|
|
|
2023-12-13 13:59:28 -05:00
|
|
|
hook_before() {
|
|
|
|
check || exit $?
|
|
|
|
runBackups || exit $?
|
2023-12-03 02:09:46 -05:00
|
|
|
}
|
|
|
|
|
2023-12-03 06:44:35 -05:00
|
|
|
hook_after() {
|
2023-12-13 13:59:28 -05:00
|
|
|
:
|
2023-12-03 02:09:46 -05:00
|
|
|
}
|
|
|
|
|
2023-12-03 06:44:35 -05:00
|
|
|
hook_fail() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
|
|
|
hook_final() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-03 02:09:46 -05:00
|
|
|
case "$1" in
|
2023-12-03 06:44:35 -05:00
|
|
|
before) hook_before || exit $?;;
|
|
|
|
after) hook_after || exit $?;;
|
|
|
|
fail) hook_fail || exit $?;;
|
|
|
|
finally) hook_final || exit $?;;
|
2023-12-03 02:09:46 -05:00
|
|
|
esac
|