#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="domain.com.$NOW.tar"
BACKUP_DIR="/home/backups"
WWW_DIR="/var/www/html/domain"
# MySQL database credentials
DB_USER="dbnameuser"
DB_PASS="dbpass"
DB_NAME="dbname"
DB_FILE="domain.com.$NOW.sql"
# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^var/www/html/domain,www,'
DB_TRANSFORM='s,^home/backups,database,'
# Create the archive and the MySQL dump
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER $DB_NAME > $BACKUP_DIR/$DB_FILE
# Append the dump to the archive, remove the dump and compress the whole archive.
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
# Transfer the file to remote server
scp $BACKUP_DIR/$FILE.gz root@serverip:/opt/backups
# Remove the file from this server
rm $BACKUP_DIR/$FILE.gz
Note : Add the MySQL user and password in the /etc/my.cnf like the following, so that it wont prompt for password
[mysqldump]
user=wordpressusername
password=whateverthepasswordis
Reference : A Shell Script for a Complete WordPress Backup – Theme.fm