This brief tutorial is going to show you how to create a bash script that will automatically backup your WordPress site and database to a FTP server at a specific time and date.

If you own a personal blog or website and you want to make sure that your blog can be restored in the event something terrible happens, then you’ll want to setup a backup and restore process. Using some WordPress plugins might get the job done, but I haven’t seen a complete plugin for WordPress that will backup your blog’s database and content via FTP automatically. I have tried many out there and they just couldn’t do it correctly.

So, I searched online and came upon this bash script that works. I modified it a bit for it to work in most environment, and all you need to do is add your FTP Server, Password, and the blog directory you wish to backup.

Objectives:

  • Backup WordPress Sites to a FTP Server Automatically
  • Enjoy!

To get started, create a new file called backup.sh   in your /root directory. Then copy and pastes the content below into the file and save.

Replace the highlighted lines with information of your systems.

 

#!/bin/bash

# Copyright 2008, 2010 Guy Rutenberg <http://www.guyrutenberg.com/contact-me>
# WordPress FTP backup 2.0
#
# Easily backup wordpress instances via ftp.
#
# Change Log:
# ===========
# 2.0:
#  - Auto-detect database settings.
#  - Exclude cache data from backups.

BACKUP_DIR=/var/www/html/wordpress   
FTP_HOST=ftp_hostname
FTP_USER=ftp_username
FTP_PASS=ftp_user password
FTP_BACKUP_DIR=/var/www/html/wordpress
# end of configuration - you probably don't need to touch anything bellow

PROG=`basename "$0"`
print_usage () {
    echo "USAGE: ${PROG} [options] BLOG_ROOT"
    echo "Backup a WordPress blog"
}

print_help ()  {
    print_usage
    cat << EOF

Options:
    -h, --help          show this help message and exit

EOF
}

TEMP=`getopt -o h --long help -n "$PROG" -- "[email protected]"`
if (($?)); then
    print_usage
    exit 1
fi

eval set -- "$TEMP"

while true ; do
    case "$1" in
        -h|--help) print_help; exit ;;
        --) shift; break;;
    esac
done

if [ -z "$1" ]
then
 print_usage > /dev/stderr
 exit 1
fi

BLOG_DIR=$1
DB_NAME=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_NAME;" | php`
DB_USER=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_USER;" | php`
DB_PASS=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_PASSWORD;" | php`
DB_HOST=`echo "<?php require_once(\"${BLOG_DIR}/wp-config.php\"); echo DB_HOST;" | php`

BLOG_DIR=`dirname "$BLOG_DIR"`/`basename "$BLOG_DIR"`
BACKUP_DIR=`dirname "$BACKUP_DIR"`/`basename "$BACKUP_DIR"`

echo -n "dumping database... "
DUMP_NAME=${DB_NAME}-$(date +%Y%m%d).sql.bz2
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} \
 --databases ${DB_NAME} \
 | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}
if [ "$?" -ne "0" ]; then
        echo "failed!"
        exit 1
fi
echo "done"

echo -n "Creating tarball... "
TAR_NAME=${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2
tar -cjf ${BACKUP_DIR}/${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2 --exclude cache ${BLOG_DIR}
if [ "$?" -ne "0" ]; then
        echo "failed!"
        exit 2
fi
echo "done"

echo -n "Uploading SQL dump and tarball to FTP... "
ftp -nv <<EOF
open "${FTP_HOST}"
user "${FTP_USER}" "${FTP_PASS}"
put "${BACKUP_DIR}/${DUMP_NAME}"
put "${BACKUP_DIR}/${TAR_NAME}"

quit
EOF

rm -f "${BACKUP_DIR}/${DUMP_NAME}"
rm -f "${BACKUP_DIR}/${TAR_NAME}"

if [ "$?" -ne "0" ]; then
        echo "failed!"
        exit 3
fi
echo "done"

 

After saving the file, run this command:

chmod +x backup.sh

You must run the above command as root.

 

After that run the commands to instantly backup your site.

/root/backup.sh /var/www/html/wordpress/

 

To schedule it to run every Sunday, open crontab and add the line below

0       3       *       *       7      /root/backup.sh /var/www/html/wordpress >/dev/null 2>&1

 

To learn more about how to schedule tasks via cron, read this post.