Backup WordPress Automatically

     

Backups are a crucial thing to do if you want to make sure your WordPress blog or site survives the (unlikely but possible) situation that your files are somehow deleted. Unfortunately, people almost always forget the backups, risking their site should something bad happen. If your blog is important to you, read this post to see how to automate WordPress backups easily.

The problem

Backing up your WordPress database (and possibly files) by hand is a time-consuming processor. This is why many people rather don’t do it, relying on their luck that everything will run smooth and clean. However, as we know, something bad out of your control can happen to your site. What will you do then if you don’t have backups of dozens of your long blog posts and they all vanish in the blink of an eye?

The solution

The process of WordPress backups can be fully automated! In this guide, we assume that you will want to put the backups to a different server than your current one, to maximize the odds that you will still have a working WordPress installation somewhere even if your current server crashes. You will need SSH access (PuTTY) to both of the servers.

When you have PuTTY, log in to your server where the WordPress is installed, put the following code to crontab and modify it a bit:

# Backup database "mysite_db" every day at 10:40 to a server called "mybackupserver.com"
40 10 * * * mysqldump -u mysqluser -p'mysqlpassword' mysite_db | gzip -c | sshpass -p sshpasswordhere ssh sshuser@mybackupserver.com "cat > ~/mysite_backup/mysite.com.sql.gz"

# Backup mysite.com public_html every Monday at 10:30 to other Linode and Tsohost
30 10 * * 1 cd /var/www/mysite.com/; tar -zc public_html | sshpass -p passwordhere ssh user@mybackupserver.com "cat > ~/mysite_backup/mysite.com.tar.gz"

Be sure to also install sshpass application which lets you automatically log in to remote server via SSH. On Ubuntu, you can easily get it by running apt-get install sshpass.

The first cron will create a backup of your database, put it to a compressed gzip file, log in to your another server via SSH and put the compressed database file to the another server. You will want to change the mysqluser, mysqlpassword and mysite_db to reflect the real information. Also, the sshpasswordheresshuser and mybackupserver.com must match the SSH login information of your backup server.

The second cron creates a compressed file of your WordPress installation and put its through SSH to the remote backup server. The first line will change to the directory where your WordPress resides, change that line in the code to correct path. Also, remember to change the SSH login information here as well.

The backup files will be put to the user’s home directory on the backup server, in a subdirectory called mysite_backup. You might want to create this folder first to make sure everything works alright. You can create directories with mkdir command in Linux.

In this example, the database cron will run every day while the file cron will run every Monday. You can change the backup frequency freely. Also, using the commands in this example, the new backup will always delete the old one.

Wrapping it up

With the above lines in your WordPress server’s crontab your blog will be safe. I have been using this method for long time and never have to worry about backing up WordPress manually anymore. I suggest you switch to automatic updates as well!

Share Your Thoughts

Have something to say about this post? Then say it by filling the form below!

(required):

: