86 lines
3.3 KiB
Bash
86 lines
3.3 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
|
||
|
###################################################################################
|
||
|
|
||
|
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 <<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} }"
|