bashscripts/backup_nc.sh

89 lines
3.5 KiB
Bash

#!/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
# TODO: save current version 'sudo -u www-data php /var/www/html/nextcloud/occ config:system:get version'
###################################################################################
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# variables push notifications
readonly GOTIFY_TOKEN="<GOTIFY_TOKEN>"
readonly GOTIFY_HOST="<GOTIFY_HOST>"
readonly GOTIFY_PRIORITY=5
# backup db variables
readonly BACKUP_DB_FILE="nextcloud-sqlbkp-"`date +\%Y\%m\%d`".sql"
#readonly BACKUP_DIR_FILE="nextcloud-dirbkp_"`date +\%Y\%m\%d`".tar.gz"
readonly DB_HOST="<DB_HOST>"
readonly DB_USER="<DB_USER>"
readonly DB_PASS="<DB_PASS>"
readonly DB_NAME="<DB_NAME>"
# backup data variables
readonly NEXTCLOUD_DATA="<NEXTCLOUD_DATA>"
readonly INCLUDE_LIST="include-list"
# remote settings
readonly REMOTE_SERVER="<REMOTE_SERVER>"
# TODO: Use $(hostname) "/path/to/example/"`hostname`"/nextcloud"
readonly REMOTE_PATH="<REMOTE_PATH>"
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 <<EOF
- **nextcloud.log
- **nextcloud.log*
- **updater.log
EOF
# init notification message
MESSAGE="Backup task init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r"
# dump and backup db nextcloud handycat and rsync deleting origin
mysqldump --single-transaction --verbose -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $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} }"