From 10e235ab0b431b6726333118a3032f2e3e23c4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20P=C3=A0mies?= Date: Thu, 30 Nov 2023 23:12:24 +0100 Subject: [PATCH] new nextcloud backup script with no dependencies --- backup_nc.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 backup_nc.sh diff --git a/backup_nc.sh b/backup_nc.sh new file mode 100644 index 0000000..85ef974 --- /dev/null +++ b/backup_nc.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +################################################################################### +# Nextcloud Backups as incremental mode, using rdiff-backup +# +# 1. Backup database and send to remote backups server throught rsync using +# ssh public/private key configuration. +# 2. Set X days to preserve db backups +# 3. Backup data folder of nextcloud using rdiff-backup (https://rdiff-backup.net/) +# 4. Set X days to preserve rdiff-backups increments +# 5. Get report statistics of rdiff-backup and send push message throught gotify +# self-hosted server (https://gotify.net/). +# +# Tools needed: mysqldump, gzip. rsync, rdiff-backup, curl +################################################################################### + +PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin + +# variables push notifications +readonly GOTIFY_TOKEN="ABlynaRwExiItgY" +readonly GOTIFY_HOST="https://push.studi7.com" +readonly GOTIFY_PRIORITY=5 +# backup db variables +readonly BACKUP_DB_FILE="nextcloud-sqlbkp-"`date +\%Y\%m\%d`".sql" +readonly DB_HOST="localhost" +readonly DB_USER="nextcloud" +readonly DB_PASS="3mZlNTeS" +readonly DB_NAME="nextcloud" +# backup data variables +readonly NEXTCLOUD_DATA="/mnt/md0/nextcloud/data" +readonly INCLUDE_LIST="include-list" +# remote settings +readonly REMOTE_SERVER="backups.studi7.com" +readonly REMOTE_PATH="/home/debian/backups/studi7/nextcloud" +readonly REMOTE_NC_DATA_FOLDER="files" +readonly REMOTE_NC_DB_FOLDER="db" +readonly BACKUP_PRESERVE_DAYS=60 + +# create include list file and set exclude logs +touch $INCLUDE_LIST +cat > $INCLUDE_LIST < $BACKUP_DB_FILE +gzip $BACKUP_DB_FILE +# TODO +# force create multiple paths (mkdir -p) if not exist with rsync trick +# https://www.schwertly.com/2013/07/forcing-rsync-to-create-a-remote-path-using-rsync-path/ +rsync -AaxzPh --remove-source-files "$BACKUP_DB_FILE.gz" "$REMOTE_SERVER:$REMOTE_PATH/$REMOTE_NC_DB_FOLDER" + +# clear X days old remote db backups +ssh $REMOTE_SERVER 'bash -s' << EOF +find "$REMOTE_PATH/$REMOTE_NC_DB_FOLDER" -maxdepth 1 -mtime +$BACKUP_PRESERVE_DAYS -print -exec "rm" -R {} \; +EOF + +# rdiff-backup all data of nextcloud +OUT=$(rdiff-backup backup --print-statistics --include-globbing-filelist $INCLUDE_LIST $NEXTCLOUD_DATA \ + "$REMOTE_SERVER::$REMOTE_PATH/$REMOTE_NC_DATA_FOLDER") +# set output lines into array and append notification message +readarray -t stats <<<"$OUT" +for val in "${stats[@]}"; do + line=$(echo $val | tr -d '-') + MESSAGE="${MESSAGE} $line \r" +done + +# clear X days old increments of backup data +rdiff-backup remove increments --older-than "${BACKUP_PRESERVE_DAYS}D" \ + "$REMOTE_SERVER::$REMOTE_PATH/$REMOTE_NC_DATA_FOLDER" + +MESSAGE="${MESSAGE} Backup task end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" + +rm $INCLUDE_LIST + +# send gotify notification +TITLE="NC $(hostname) Backup" +EXTRAS="{\"client::display\": {\"contentType\": \"text/markdown\"}}" +curl -X POST "$GOTIFY_HOST/message?token=$GOTIFY_TOKEN" -H "accept: application/json" -H "Content-Type: application/json" \ + -d "{ \"message\": \"${MESSAGE}\", \"priority\": ${GOTIFY_PRIORITY}, \"title\": \"${TITLE}\", \"extras\": ${EXTRAS} }"