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" -- "$@"`
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.

Frequently Asked Questions

How to automatically backup Wordpress site to FTP server?

To automatically backup your Wordpress site to a FTP server, you can create a bash script that schedules backups at specific times. Follow the instructions in the provided article to set up the backup process.

What is the importance of backing up a Wordpress site to FTP server?

Backing up your Wordpress site to a FTP server ensures that your website data and content are safe in case of any unforeseen events or data loss. It allows for easy restoration of your site when needed.

Why use a bash script for Wordpress site backups?

Using a bash script for Wordpress site backups allows for automation and scheduling of backup processes, making it convenient and reliable. It provides a customizable solution for backing up both site files and databases.

Can Wordpress plugins backup sites to FTP servers automatically?

While some Wordpress plugins may offer backup features, it is not common to find a complete plugin that can automatically backup a site to a FTP server. Using a custom bash script, as described in the article, can be a more reliable option.

What information is needed to set up Wordpress site backup to FTP server?

To set up Wordpress site backup to a FTP server, you will need to configure the backup script with your FTP server details, including hostname, username, password, and the directory path for backups.

How to modify the provided bash script for Wordpress site backups?

You can modify the provided bash script for Wordpress site backups by editing the configuration variables such as BACKUP_DIR, FTP_HOST, FTP_USER, FTP_PASS, and FTP_BACKUP_DIR. Make sure to replace the highlighted lines with your own server information.

What are the benefits of scheduling Wordpress site backups?

Scheduling Wordpress site backups ensures regular and automatic backups without manual intervention. This helps in maintaining the latest backup copies of your site files and database for disaster recovery purposes.

How to ensure the security of Wordpress site backups on FTP server?

To ensure the security of Wordpress site backups on a FTP server, make sure to use secure FTP protocols (such as SFTP) and keep your FTP credentials confidential. Regularly monitor and verify the integrity of your backup files.