Skip to content

Instantly share code, notes, and snippets.

@da-n
Last active October 8, 2023 08:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save da-n/6079367 to your computer and use it in GitHub Desktop.
Save da-n/6079367 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.
#!/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