Skip to content

Instantly share code, notes, and snippets.

@iainmcampbell
Last active August 29, 2015 14:13
Show Gist options
  • Save iainmcampbell/070dd8e6b99724fc49d2 to your computer and use it in GitHub Desktop.
Save iainmcampbell/070dd8e6b99724fc49d2 to your computer and use it in GitHub Desktop.
Web Server Remote Backup
#!/bin/bash
# PRE SETUP: create bkpath dir, ie /user/backups. suggested: put this script there.
name="" # set this to the name of the project
bkpath="/var/backups/" # directory to store backups in locally
# Grab a DB dump from the MySQL server -> $bkpath/db/name_YYYY-MM-DD.sql
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
dbhost="" # localhost or remote
dbuser=""
dbpass=""
dbname=""
# use $(date +%a) for Mon/Tue/Wed/Thu/Fri
filename="$name_$(date +%F).sql"
file="$bkpath/db/$filename"
$MYSQLDUMP -h $dbhost -u $dbuser -p$dbpass $dbname > $file && $GZIP -f $file
# Send the DB backup to a remote server
ssh_user=""
ssh_server=""
remote_directory="/var/backups/$name"
scp -c blowfish $file.gz $ssh_user@$ssh_server:$remote_directory/db/$filename
# rsync uploads and plugins
args="-a --delete --exclude=/dev --exclude=/sys --exclude=/proc --exclude=/tmp"
plugins="/var/www/wp-content/plugins"
rsync $args $plugins $ssh_user@$ssh_server:$remote_directory
uploads="/var/www/wp-content/uploads"
rsync $args $uploads $ssh_user@$ssh_server:$remote_directory
# success!
echo "Backed up successfully at $(date)" >> $bkpath/remote_backup_log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment