-
-
Save bonelifer/85df3e83aa185b2741bc554c62004af8 to your computer and use it in GitHub Desktop.
Backup script with Pushover notifications. Pushover is optional, you can edit it out if not required. To get this script running check all the parameters at the top.
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 | |
DATE=`date +%Y%m%d-%H%M%S` | |
PROGNAME=$(basename $0) | |
APPEND=-$DATE.tar.gz | |
# Set the local backup directory, ensure that this directory exists | |
LOCALDIR=$HOME/backups/ | |
# Set the remote directory to backup | |
REMOTEDIR=/var/www | |
# Set the remote user | |
REMOTEUSER=www | |
# Set the remote IP | |
REMOTEIP=0.0.0.0 | |
# Set the MySQL user | |
MYSQLUSER=mysqlusername | |
# Set the MySQL password | |
MYSQPASSWORD=mysqlpassword | |
# Set the MySQL database | |
MYSQLDATABASE=mysqldatabase | |
# Set the Pushover token | |
PUSHOVERTOKEN=pushovertoken | |
# Set the Pushover token | |
PUSHOVERUSER=pushoveruser | |
# Success message | |
MESSAGE='Backup SUCCESS' | |
# Generic error and exit function | |
function error_exit { | |
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2 | |
exit 1 | |
} | |
# Send push notifications to admin pushover account | |
function pushover { | |
curl -s \ | |
-F "token=$PUSHOVERTOKEN" \ | |
-F "user=$PUSHOVERUSER" \ | |
-F "message=$MESSAGE" \ | |
https://api.pushover.net/1/messages.json | |
return $TRUE | |
} | |
# Step 1: ssh into remote server, tar all files | |
ssh $REMOTEUSER@$REMOTEIP "mkdir tmp-backup-$DATE && cd tmp-backup-$DATE && tar cvfz files-$APPEND $REMOTEDIR && mysqldump -u $MYSQLUSER -$MYSQPASSWORD $MYSQLDATABASE | gzip > sql-$DATE.sql.gz && cd .. && tar cvf files-$APPEND tmp-backup-$DATE" | |
if [ "$?" != 0 ]; then | |
MESSAGE="Backup FAILED [Line $LINENO]" | |
pushover | |
error_exit "$MESSAGE" | |
fi | |
# Step 2: download remote tar archive | |
scp -c arcfour $REMOTEUSER@$REMOTEIP:files-$APPEND "$LOCALDIR" | |
if [ "$?" != 0 ]; then | |
MESSAGE="Backup FAILED [Line $LINENO]" | |
pushover | |
error_exit "$MESSAGE" | |
fi | |
# Step 3, cleanup remote tar archive | |
ssh $REMOTEUSER@$REMOTEIP "rm files-$APPEND && rm -rf tmp-backup-$DATE" | |
if [ "$?" != 0 ]; then | |
MESSAGE="Backup FAILED [Line $LINENO]" | |
pushover | |
error_exit "$MESSAGE" | |
fi | |
# Send success push notification | |
pushover |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment