Add backup module to Gitea

This commit is contained in:
Salt 2020-05-03 06:37:58 -05:00
parent ed04787e9b
commit 85a3da7b6c
3 changed files with 59 additions and 2 deletions

View File

@ -10,6 +10,7 @@
set -e set -e
export BACKUPSDIR="/backups" export BACKUPSDIR="/backups"
export OUTDIR="$BACKUPSDIR/out"
export MODULESDIR="/opt/backups/modules" export MODULESDIR="/opt/backups/modules"
export DATE="$(date -Iseconds)" export DATE="$(date -Iseconds)"
@ -22,7 +23,7 @@ log() {
# Sanity checks # Sanity checks
if ! [ -d "$MODULESDIR" ]; then if ! [ -d "$MODULESDIR" ]; then
log "Unable to find modules directory: $MODULESDIR" log "Unable to find modules directory: $MODULESDIR"
exit 2 exit 1
fi fi
# Source an RC, if we have it # Source an RC, if we have it
if [ -r "$MODULESDIR/backuprc" ]; then if [ -r "$MODULESDIR/backuprc" ]; then
@ -31,7 +32,7 @@ fi
# More sanity checks # More sanity checks
if ! [ -d "$BACKUPSDIR" ]; then if ! [ -d "$BACKUPSDIR" ]; then
log "Unable to find backups directory: $BACKUPSDIR" log "Unable to find backups directory: $BACKUPSDIR"
exit 1 exit 2
fi fi
# Do the do # Do the do
log "Beginning backups" log "Beginning backups"
@ -41,6 +42,11 @@ for file in "$MODULESDIR"/*; do
# Execute the module and alert if it fails # Execute the module and alert if it fails
log "Executing module: $file" 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" source "$file"
) || { ) || {
log "Error executing module: $file" log "Error executing module: $file"

View File

@ -126,4 +126,9 @@
name: "gitea.service" name: "gitea.service"
enabled: yes enabled: yes
state: "started" state: "started"
- name: Template out backup module
template:
src: "gitea.sh"
dest: "/opt/backups/modules"
mode: "0600"
become: yes become: yes

View File

@ -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 <rehashedsalt@cock.li>
#
# 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