From 4c54833b7e4f688261734f51266ab1cca26b1b3f Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 6 May 2020 02:01:30 -0500 Subject: [PATCH] Actually add that file --- roles/nextcloud/templates/backup.sh | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 roles/nextcloud/templates/backup.sh diff --git a/roles/nextcloud/templates/backup.sh b/roles/nextcloud/templates/backup.sh new file mode 100644 index 0000000..dd50b41 --- /dev/null +++ b/roles/nextcloud/templates/backup.sh @@ -0,0 +1,57 @@ +#! /bin/bash +# +# nextcloud.sh +# Backup script for Nextcloud. 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/{{ nextcloud_url }}" +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" + rm "$lastbackup" + fi +fi +# WE MAKE BACKUP NOW SERGEI +if cd "{{ nextcloud_webroot }}"; then + log "Enabling maintenance mode" + sudo -u www-data ./oc maintenance:mode --on + trap 'sudo -u www-data ./occ maintenance:mode --off' EXIT + date="$(date -Iseconds)" + log "Creating data backup" + tar czf "$OUTDIR/$date-data.tar.gz" "/var/nextcloud" + log "Creating webroot backup" + tar czf "$OUTDIR/$date-webroot.tar.gz" "{{ nextcloud_webroot }}" + log "Creating DB backup" + mysqldump nextcloud --single-transaction | gzip > "$OUTDIR/$date-db.sql.gz" + log "Unsetting maintenance mode" + sudo -u www-data ./occ maintenance:mode --off +else + log "Could not change directory: $OUTDIR" + return 3 +fi +