Backup Drupal Website and MySQL DB

I use this script to backup my drupal website and upload backups to a remote location.

#!/bin/bash
erroruser="varun.verma@email.com"
errorsubject="Website backup error:"

if [ "X$*" != "X" ]; then
#Name your own backup type
MODE="-$*";
else
grep "^AuthUserFile /home/httpd/.htpasswd" /home/httpd/.htaccess
if [ $? -eq 0 ]; then
MODE="-STAGE"
echo "Running in mode www${MODE}"
fi
fi

DATECMD="date +%c"
TIMESTAMP=`date +%F-%H%M`
echo $TIMESTAMP

MYSQLDMPOPT="--ignore-table=www_drupal.users --ignore-table=www_drupal.uc_orders --ignore-table=www_drupal.uc_order_log --ignore-table=www_drupal.uc_order_line_items --ignore-table=www_drupal.uc_order_statuses --ignore-table=www_drupal.uc_recurring_orders --ignore-table=www_drupal.uc_recurring_users"
MYSQLDMPOPTCACHE="--ignore-table=www_drupal.cache --ignore-table=www_drupal.cache_filter --ignore-table=www_drupal.cache_menu --ignore-table=www_drupal.cache_page --ignore-table=www_drupal.watchdog"

MYSQLRETAIN="users uc_orders uc_order_log uc_order_line_items uc_order_statuses uc_recurring_orders uc_recurring_users"
MYSQLRETAINCACHE="cache cache_filter cache_menu cache_page watchdog"

websqlfile="drupal_db_backup_$TIMESTAMP.sql"
websqlfileretain="drupal_db_retain_$TIMESTAMP.sql"
webfile="web_pages_$TIMESTAMP.tar"
blogsqlfile="wordpress_db_backup_$TIMESTAMP.sql"

echo
echo `$DATECMD`  Creating Backup of files
cd /home/httpd
tar -cf /home/varun.verma/backup_location/$webfile site || mail -s "$errorsubject scripts tar creation error" $erroruser </dev/null

echo `$DATECMD` Creating Backup of Databases
cd /home/varun.verma/backup_location

echo `$DATECMD` Creating Backup File
websqlfileretain=""
blogsqlfile=""
tar --remove-files -zcf web${MODE}_backup_$TIMESTAMP.tgz $blogsqlfile $websqlfile $websqlfileretain $webfile || mail -s "$errorsubject tgz creation error" $erroruser </dev/null

echo "put web${MODE}_backup_$TIMESTAMP.tgz" > sftp_backup
echo `$DATECMD` Copying file to SFTP Server
sftp -b sftp_backup -i /home/varun.verma/.ssh/remote_account_private.key remote_account@ftp.location.net:/home/remote_account/Share/WebBackups/ || mail -s "$errorsubject sftp upload error" $erroruser </dev/null

echo `$DATECMD` "Removing old backup file."
find /home/varun.verma/backup_location -mtime +2 -exec rm -rf {} \; || mail -s "$errorsubject old backup cleanup error" $erroruser </dev/null

echo `$DATECMD` Done
mail -s "www${MODE}.website.com backup completed successfully" "varun.verma@email.com " <<EOM
Backup file created and uploaded successfully to ftp.location.net:/home/remote_account/Share/WebBackups/web${MODE}_backup_$TIMESTAMP.tgz.
EOM
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s