From cb7bcebc278463372dfdc775304ee771f51a1507 Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Wed, 13 Dec 2023 13:59:28 -0500 Subject: [PATCH] Updated gitea to be more powerful and configurable --- scripts/gitea | 64 ++++++++++++++++++++++++++++--- scripts/mysql_backup | 2 - templates/config/gitea_backup.cfg | 6 +++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 templates/config/gitea_backup.cfg diff --git a/scripts/gitea b/scripts/gitea index 8570c9c..f140651 100755 --- a/scripts/gitea +++ b/scripts/gitea @@ -1,22 +1,74 @@ #!/bin/bash restoreDir=/etc/restore -giteaDir="$restoreDir/gitea" +giteaDir="/var/backups/gitea" +scriptDir="$(dirname "$0")" -hook_before() { +readConfig() { + if [[ -r "${scriptDir}/../config/gitea_backup.cfg" ]]; then + source "${scriptDir}/../config/gitea_backup.cfg" + fi + +} + +check() { if [[ -d "$giteaDir" ]]; then - rm -rf "$giteaDir" || exit 1 + 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 + fi +} + +runBackups() { + local backupDate + backupDate=$(date +"%Y-%m-%d") giteaHome=$(getent passwd git | cut -f6 -d:) + sudo -u "$GITEA_USER" "${GITEA_BIN}" dump -c "${GITEA_CONFIG}" --type tar.xz --file - > "${giteaDir}/gitea-dump-${backupDate}.tar.xz" +} - mkdir -p "$giteaDir" - sudo -u git "${giteaHome}/bin/gitea" dump --type tar.xz --file - > "${giteaDir}/gitea-dump-${backupDate}.tar.xz" +hook_before() { + check || exit $? + runBackups || exit $? } hook_after() { - rm -rf "$giteaDir" || exit 1 + : } hook_fail() { diff --git a/scripts/mysql_backup b/scripts/mysql_backup index 737eb4b..74074ed 100644 --- a/scripts/mysql_backup +++ b/scripts/mysql_backup @@ -5,8 +5,6 @@ mysqlDir="$restoreDir/mysql" scriptDir="$(dirname "$0")" function readConfig() { - local $config - if [[ -r "${scriptDir}/../config/mysql_backup.cfg" ]]; then source "${scriptDir}/../config/mysql_backup.cfg" fi diff --git a/templates/config/gitea_backup.cfg b/templates/config/gitea_backup.cfg new file mode 100644 index 0000000..f3385b0 --- /dev/null +++ b/templates/config/gitea_backup.cfg @@ -0,0 +1,6 @@ +# This configuration file is used for providing configuration parameters to +# the gitea backup agent. +# GITEA_BIN Default: Found in PATH +# GITEA_CONFIG Default: /etc/gitea/app.ini +# GITEA_USER Default: git +# GITEA_HOME Default: Home Directory of GITEA_USER