Last updated:
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.
Stuck at this step? Ask us, it is free and include the exact error.
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/wordpressFTP_HOST=ftp_hostnameFTP_USER=ftp_usernameFTP_PASS=ftp_user passwordFTP_BACKUP_DIR=/var/www/html/wordpress# end of configuration - you probably don't need to touch anything bellowPROG=`basename "$0"`print_usage () {echo "USAGE: ${PROG} [options] BLOG_ROOT"echo "Backup a WordPress blog"}print_help () {print_usagecat << EOFOptions:-h, --help show this help message and exitEOF}TEMP=`getopt -o h --long help -n "$PROG" -- "$@"`if (($?)); thenprint_usageexit 1fieval set -- "$TEMP"while true ; docase "$1" in-h|--help) print_help; exit ;;--) shift; break;;esacdoneif [ -z "$1" ]thenprint_usage > /dev/stderrexit 1fiBLOG_DIR=$1DB_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.bz2mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} \--databases ${DB_NAME} \| bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}if [ "$?" -ne "0" ]; thenecho "failed!"exit 1fiecho "done"echo -n "Creating tarball... "TAR_NAME=${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2tar -cjf ${BACKUP_DIR}/${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2 --exclude cache ${BLOG_DIR}if [ "$?" -ne "0" ]; thenecho "failed!"exit 2fiecho "done"echo -n "Uploading SQL dump and tarball to FTP... "ftp -nv <<EOFopen "${FTP_HOST}"user "${FTP_USER}" "${FTP_PASS}"put "${BACKUP_DIR}/${DUMP_NAME}"put "${BACKUP_DIR}/${TAR_NAME}"quitEOFrm -f "${BACKUP_DIR}/${DUMP_NAME}"rm -f "${BACKUP_DIR}/${TAR_NAME}"if [ "$?" -ne "0" ]; thenecho "failed!"exit 3fiecho "done"
After saving the file, run this command:
chmod +x backup.shYou 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.