Created
May 31, 2016 01:19
-
-
Save georg90/7c4afac1115856a402eeeb0659166739 to your computer and use it in GitHub Desktop.
Backup script to use with monit monitoring system: https://lane6.de/posts/update-duply-backup-script-with-logging-and-autonomous-monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# Simple backup script for jottacloud and owncloud/git. | |
# Created by Georg Peters (https://lane6.de) | |
# Version 1.1 | |
# Previous version at: https://lane6.de/posts/how-to-setup-a-simple-online-backup-with-duply-and-owncloud | |
# Use: ./backup.sh owncloud | |
# Check if backup if running | |
if [[ -f /tmp/backup.running ]]; then | |
echo "Error: Backup running" | |
# Optional, send E-Mail: echo "Backup $1 am `date +"%d%m%Y"`" | mailx -s "BACKUP: Error running backup!" $EMAIL_TO | |
exit | |
fi | |
# | |
# Global Settings | |
# | |
export JOTTACLOUD_USERNAME="" | |
export JOTTACLOUD_PASSWORD="" | |
EMAIL_TO="[email protected]" | |
MYSQL_USER="root" | |
MYSQL_PASSWD="mysql password" | |
TODAY=$(date +%d%m%Y) | |
# Create backup dir | |
if [ ! -d /var/log/backup ];then | |
mkdir -p /var/log/backup | |
fi | |
# Trace function for logging, don't change this | |
trace () { | |
stamp=`date +%Y-%m-%d_%H:%M:%S` | |
echo "$stamp: $*" >> ${DAILYLOGFILE} | |
} | |
case $1 in | |
"error") | |
# This will send a report to my email address (triggered from monit monitoring system) | |
# Use: backup.sh error duplyProfile NiceProfileName [email protected] | |
PROFILE=$2 | |
PROFILENAME=$3 | |
EMAIL=$4 | |
/usr/bin/duply /root/.duply/$PROFILE status 2>&1 > /tmp/status.${PROFILE}.${TODAY}.log | |
/usr/bin/zip -j /tmp/logfiles_${PROFILE}_${TODAY}.zip /tmp/status.${PROFILE}.${TODAY}.log /var/log/backup/${PROFILE}.daily.log /var/log/backup/${PROFILE}.log | |
echo "The backup for ${PROFILENAME} failed at ${TODAY}" | mutt -a "/tmp/logfiles_${PROFILE}_${TODAY}.zip" -s "[BACKUP] ${PROFILENAME} backup failed" -- $EMAIL | |
rm -rf /tmp/logfiles_${PROFILE}_${TODAY}.zip | |
;; | |
"owncloud") | |
# | |
# Settings | |
# | |
SOURCE_FOLDER="/path/to/owncloud" | |
MYSQL_ARG="--all-databases" # Backup all the databases | |
PROFILE="jotta" | |
# Only change if you have to.. | |
DUPLY_CMD="/root/.duply/${PROFILE} backup" | |
DUPLY_STATUS="/root/.duply/${PROFILE} status" | |
LOGFILE="/var/log/backup/${PROFILE}.log" | |
DAILYLOGFILE="/var/log/backup/${PROFILE}.daily.log" | |
# | |
# Start script | |
# | |
# Create log if not exists | |
if [ ! -f $LOGFILE ]; then | |
touch $LOGFILE | |
fi | |
# Create daily log if not exists | |
if [ ! -f $DAILYLOGFILE ]; then | |
touch $DAILYLOGFILE | |
fi | |
# Clear the old daily log file | |
cat /dev/null > ${DAILYLOGFILE} | |
# Let only one backup run at a time | |
touch /tmp/backup.running | |
cd /tmp | |
trace "Backup for owncloud started" | |
trace "Creating mysql dump" | |
mysqldump -h127.0.0.1 -u $MYSQL_USER -p${MYSQL_PASSWD} $MYSQL_ARG > ${SOURCE_FOLDER}/sqlbkp_`date +"%Y%m%d"`.bak 2>> ${DAILYLOGFILE} | |
trace "mysql dump finished" | |
trace "Starting backup.." | |
/usr/bin/duply $DUPLY_CMD >> $DAILYLOGFILE 2>&1 | |
BACKUPSTATUS=`cat "$DAILYLOGFILE" | grep Errors | awk '{ print $2 }'` | |
if [ "$BACKUPSTATUS" != "0" ]; then | |
# 'Error' is the trigger word for my monit monitoring solution | |
trace "Backup ERROR - failed to create backup" | |
else | |
trace "Backup complete - no errors" | |
fi | |
trace "------------------------------------" | |
# Add daily log to logfile | |
cat "$DAILYLOGFILE" >> $LOGFILE | |
# Clean up! | |
rm -rf ${SOURCE_FOLDER}/sqlbkp_`date +"%Y%m%d"`.bak | |
;; | |
*) | |
echo "no match" | |
;; | |
esac | |
rm -f /tmp/backup.running | |
unset JOTTACLOUD_USERNAME | |
unset JOTTACLOUD_PASSWORD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment