WordPress How to Setup Automatic Backup Entire Blog

If you run a WordPress blog on a Linux server, let me show you how to very easily set up an automatic backup.
You can set the frequency of the backups and the saved files of the entire blog/website will be added to a directory of your choice.

I’m assuming that you have basic knowledge of the Linux command prompt and know how to edit files. I will be using vim but you can accomplish the same tasks with your favorite editor instead.

Step One: Create Backup Shell Script
From a linux prompt on the server that hosts your WordPress blog, open a text editor and create the backup script filename.
I’m including sudo to allow access to each command that would require root or superuser privileges.

sudo vi WPBlogBackup.sh

then in the editor, copy and paste the following: (tip – using vim, the insert key or “i” opens the file for editing)

#!/bin/sh
NOWDATE=`date +%d%m%y`
tar -zcf /home/backups/WPBlogBkup$NOWDATE.tar.gz /var/lib/mysql/streaming/*
#done

Save file and exit vim ( :x)

Note: the filenames and directories should be set to your specific needs and server locations.


Step Two: Test the script

From the same directory that you created the script

sudo sh WPBlogBackup.sh

You will be returned to the prompt when the script is completed running. If there are no errors, you can now list out the contents of your backup file to ensure that all the files in your blog were included.

ls *.gz -t

The most recent file should be the backup file you just created with today’s date.

This command will show you all the files in the tar without extracting them.

sudo tar -tvf newfile.tar.gz

Note: You should create a second backup script for your blog’s database. To do this, you’d simply include the directory where your database resides.
For example: tar -zcf /home/backups/WPDBBkup$NOWDATE.tar.gz /var/lib/mysql/myblog/*
However, if you are using a shared server, your DB may reside on a remote server. In that case, you won’t be able to automate your DB backups.

Step Three: Create cron job to automate the backup script
From a Linux prompt open your crontab for editing: (If you are on a shared server, you may have to skip this step)

crontab -e

In the cron editor, include a line like this…

@daily sh /home/backups/WPBlogBkup.sh

Take care to change the name of the file here to the shell filename you created in your backups directory.
@daily runs this script every day at midnight server time. You can set it to any number of frequencies – check out crontab’s configuration for more options.


Leave a Reply

Your email address will not be published. Required fields are marked *