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() {
|
2024-01-09 20:44:14 -05:00
|
|
|
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_GROUP=${GITEA_GROUP:-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-13 13:59:28 -05:00
|
|
|
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)"
|
2023-12-13 14:15:52 -05:00
|
|
|
echo "Found Gitea in $GITEA_BIN"
|
2023-12-13 13:59:28 -05:00
|
|
|
else
|
|
|
|
echo "FATAL: Cannot execute Gitea"
|
|
|
|
return 3
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-01-09 20:44:14 -05:00
|
|
|
if [[ -d "$giteaDir" ]]; then
|
|
|
|
echo "Cleaning out old Gitea backups..."
|
|
|
|
rm -f "$giteaDir"/* || return 1
|
|
|
|
else
|
|
|
|
mkdir -p "$giteaDir" | return 1
|
|
|
|
chgrp "$GITEA_GROUP" "$giteaDir"
|
|
|
|
chmod g+wx "$giteaDir"
|
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:)
|
2024-01-09 22:50:29 -05:00
|
|
|
pushd "${giteaDir}" &>/dev/null
|
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"
|
2024-01-09 22:50:29 -05:00
|
|
|
popd &>/dev/null
|
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
|