diff --git a/backup_tryton.sh b/backup_tryton.sh new file mode 100644 index 0000000..8c6da0c --- /dev/null +++ b/backup_tryton.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +################################################################################### +# Tryton Backup: attachments and database +# +# 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 wordpress +# 4. Set X days to preserve wp backups dir +# 5. Get report statistics and send push message throught gotify +# self-hosted server (https://gotify.net/). +# +# Tools needed: pg_dump, 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="pg-sqlbkp-"`date +\%Y\%m\%d`".sql.gz" +readonly BACKUP_DB_CONNECTION="postgresql://handycat:hand15zzycat@localhost:5432/handycat" +# backup data variables +readonly ATTACH_DIR="/home/raimon/attachments" +# remote settings +readonly REMOTE_SERVER="backups.studi7.com" +readonly REMOTE_PATH="/home/debian/backups/handycat" +readonly REMOTE_TRYTON_DB_FOLDER="tryton/db" +readonly REMOTE_TRYTON_DIR_FOLDER="tryton/attachments" +readonly BACKUP_PRESERVE_DAYS=30 + +# init notification message +MESSAGE="Backup task init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" + +# dump postgreSQL +pg_dump --dbname=$BACKUP_DB_CONNECTION | gzip -9 > $BACKUP_DB_FILE +rsync -AaxzPh --remove-source-files $BACKUP_DB_FILE "$REMOTE_SERVER:$REMOTE_PATH/$REMOTE_TRYTON_DB_FOLDER" + +# clear X days old remote db backups +ssh $REMOTE_SERVER 'bash -s' << EOF +find "$REMOTE_PATH/$REMOTE_WP_TRYTON_FOLDER" -maxdepth 1 -mtime +$BACKUP_PRESERVE_DAYS -print -exec "rm" -R {} \; +EOF + +# rdiff-backup all dir of attachments +rdiff-backup $ATTACH_DIR "$REMOTE_SERVER::$REMOTE_PATH/$REMOTE_TRYTON_DIR_FOLDER" +# clear X days old increments of backup dir +rdiff-backup --remove-older-than "${BACKUP_PRESERVE_DAYS}D" "$REMOTE_SERVER::$REMOTE_PATH/$REMOTE_TRYTON_DIR_FOLDER" + +MESSAGE="${MESSAGE} Backup task end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" + +# send gotify notification +TITLE="Custom $(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} }"