Preview:
#!/bin/sh

### SSH Info ###
SUSER="serverusername";
SHOST="example.com";
SDIR="/srv/www/example.com";

echo 'starting rsync'

rsync -e "/usr/bin/ssh" --compress --stats -rlDHS $SUSER@$SHOST:$SDIR /srv/www/example.com

WPDBHOST=`cat /srv/www/example.com/wp-config.php | grep DB_HOST | cut -d \' -f 4`;
WPDBNAME=`cat /srv/www/tomjn.com/wp-config.php | grep DB_NAME | cut -d \' -f 4`;
WPDBUSER=`cat /srv/www/tomjn.com/wp-config.php | grep DB_USER | cut -d \' -f 4`;
WPDBPASS=`cat /srv/www/tomjn.com/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`;

echo 'finished rsync, grabbing database'

FILE=$SDIR/mysql-$WPDBNAME.sql.gz;        # Set the backup filename

#echo "mysqldump -q -u $WPDBUSER -h $WPDBHOST -p$WPDBPASS $WPDBNAME | gzip -9 > $FILE";
ssh $SUSER@$SHOST "mysqldump -q -u $WPDBUSER -h $WPDBHOST -p$WPDBPASS $WPDBNAME | gzip -9 > backup.sql.gz"

scp $SUSER@$SHOST:./backup.sql.gz .              # copy all the files to backup server
ssh $SUSER@$SHOST rm ./backup.sql.gz             # delete files on db server

gunzip -d backup.sql.gz

mysql -u root -e "CREATE DATABASE IF NOT EXISTS $WPDBNAME"
mysql -u root -e "GRANT ALL PRIVILEGES ON $WPDBNAME.* To 'wp'@'localhost'"
mysql -u wp -pwp $WPDBNAME < backup.sql

sed -i "/DB_HOST/s/'[^']*'/'localhost'/2" wp-config.php
sed -i "/DB_USER/s/'[^']*'/'wp'/2" wp-config.php
sed -i "/DB_PASSWORD/s/'[^']*'/'wp'/2" wp-config.php
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter