From 85a3da7b6ccbdde34c16842c20d27cf9f031ed2d Mon Sep 17 00:00:00 2001 From: Salt Date: Sun, 3 May 2020 06:37:58 -0500 Subject: [PATCH] Add backup module to Gitea --- roles/backups/templates/backup.sh | 10 +++++-- roles/gitea/tasks/main.yml | 5 ++++ roles/gitea/templates/gitea.sh | 46 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 roles/gitea/templates/gitea.sh diff --git a/roles/backups/templates/backup.sh b/roles/backups/templates/backup.sh index 1b93482..3a931da 100644 --- a/roles/backups/templates/backup.sh +++ b/roles/backups/templates/backup.sh @@ -10,6 +10,7 @@ set -e export BACKUPSDIR="/backups" +export OUTDIR="$BACKUPSDIR/out" export MODULESDIR="/opt/backups/modules" export DATE="$(date -Iseconds)" @@ -22,7 +23,7 @@ log() { # Sanity checks if ! [ -d "$MODULESDIR" ]; then log "Unable to find modules directory: $MODULESDIR" - exit 2 + exit 1 fi # Source an RC, if we have it if [ -r "$MODULESDIR/backuprc" ]; then @@ -31,7 +32,7 @@ fi # More sanity checks if ! [ -d "$BACKUPSDIR" ]; then log "Unable to find backups directory: $BACKUPSDIR" - exit 1 + exit 2 fi # Do the do log "Beginning backups" @@ -41,6 +42,11 @@ for file in "$MODULESDIR"/*; do # Execute the module and alert if it fails log "Executing module: $file" ( + # Define a log function for our module to use + log() { + [ -z "$1" ] && return 1 + printf "$(date -Iseconds): $1\n" + } source "$file" ) || { log "Error executing module: $file" diff --git a/roles/gitea/tasks/main.yml b/roles/gitea/tasks/main.yml index 08a184c..b85dde3 100644 --- a/roles/gitea/tasks/main.yml +++ b/roles/gitea/tasks/main.yml @@ -126,4 +126,9 @@ name: "gitea.service" enabled: yes state: "started" + - name: Template out backup module + template: + src: "gitea.sh" + dest: "/opt/backups/modules" + mode: "0600" become: yes diff --git a/roles/gitea/templates/gitea.sh b/roles/gitea/templates/gitea.sh new file mode 100644 index 0000000..79dea2d --- /dev/null +++ b/roles/gitea/templates/gitea.sh @@ -0,0 +1,46 @@ +#! /bin/bash +# +# gitea.sh +# Backup script for Gitea. Meant to be sourced by our main backup script +# Copyright (C) 2020 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +set -e + +export OUTDIR="$BACKUPSDIR/gitea" +retention=7 # 7-day retention period + +# Sanity checks +if [ -z "$BACKUPSDIR" ]; then + log "BACKUPSDIR was undefined. Run the main backup script instead of this one." + return 1 +fi +if ! [ -d "$OUTDIR" ]; then + if ! mkdir "$OUTDIR"; then + log "Unable to find or create output directory: $OUTDIR" + return 2 + fi +fi +# Enforce permissions on our output directory since the git user will need them +chown root.git "$OUTDIR" +chmod 770 "$OUTDIR" + +# Purge oldest backup if we need to +currentbackupcount="$(ls -1 "$OUTDIR" | wc -l)" +if (( currentbackupcount >= retention )); then + lastbackup="$(find "$OUTDIR" -name \*.zip 2>/dev/null | sort | head -n 1)" + if [ -f "$lastbackup" ]; then + log "Removing old backup: $lastbackup" + fi +fi +# WE MAKE BACKUP NOW SERGEI +if cd "$OUTDIR"; then + log "Initiating gitea dump" + su git -c "gitea dump -c /etc/gitea/app.ini" +else + log "Could not change directory: $OUTDIR" + return 3 +fi +