Wordpress & Web Development

Automate Your WordPress Backup With Simple Shell Scripting & CRON

James Bruce 06-05-2011

<firstimage=”//static.makeuseof.com/wp-content/uploads/2011/04/cron-scripting.png”>wordpress site backupLast time we talked about WordPress backups, I showed you how incredibly easy it was to backup your entire database and files though SSH How to Back Up Your Website Through SSH Command Line Need to back up your website in a pinch? Forget plugins! Here's how to back up a website using SSH on GoDaddy and other webhosts. Read More with only a few commands. This time, I’m going to show how to automate those commands, giving you fresh backups of your entire site every week, with very little effort. This will also serve as a great introduction to both shell scripting and CRON if you’ve never touched them before – the key to learning such vast topics is to start straight off by using them to do something useful for you.


Recap: Backup everything

We covered this last time, but a quick recap on the two commands needed to backup your database and and files, assuming you’ve already logged in and moved yourself to the website directory (read the first tutorial if you don’t understand). Make sure you do them in this order, so that your file backup includes the database file you output in the first command:

mysqldump --add-drop-table -u username -p databasename > databasebackup.sql
tar -cf backupfile.tar .

Replace the username and databasename items with your database and username details.

Automation Step One: Scripting

For now, we’re going to make a new script that simply runs the commands you learnt for backup, with a few alterations to include the password too (since this will be automated, you won’t be able to type in the password everytime). When we’re finished, you should be left with just one command to run that will perform two commands for you!

It’s also about time you learnt how to edit text files through the command line as well, as you can’t rely on FTP and GUI’s all the time. You can use a simple text-based editor called vi to do this.

To start the app and create your first script, type:

vi mybackupscript.sh

If the file doesnt exist already, it will be created and you’ll be shown a rather daunting screen similar to this:

wordpress site backup

vi has two modes – edit and command mode. To get into edit mode, press i. Then you can begin typing. You’ll know it worked, because the bottom left will turn to –INSERT–

wordpress site backup


Start by typing out the following:

mysqldump --add-drop-table -uusername -ppassword tablename > dbbackup.sql
tar -cf backup.tar .

Notice that this time, we are including the password in the command. Also notice that when we use the -p switch to specify the password, we then put the password immediately after it with no space between them. If you’d prefer, you can write the command like this instead, but functionally there is no difference:

mysqldump --add-drop-table --user=username --password=password tablename > dbbackup.sql
tar -cf backup.tar .

Now we need to save it. Press ESC once to get out of edit mode and into command mode of the text editor. Type:


and press enter, then


and enter again.

So by now you will have figured out that any commands you give must be preceded by a colon. That’s all with vi for now.

Back on the command line, go ahead and make your new script executable by typing in the following:

chmod 744 mybackupscript.sh

And finally, test it out with:


Obviously, depending on the size of your site and speed of your server, it may take a while. At the end of it, you can list the files and should find a backup.tar. On my virtual private server it took about 5 seconds to create the 100MB WordPress site backup.

Automation Step Two: CRON

CRON is a task scheduler for Linux. We won’t be covering it in-depth here, but I’ll give you what you need to run your backup script every week. We’ve also covered how to run CRON jobs from your GUI based website control panel. To add a task to the CRON scheduler, you simply add a line to the “crontab”. Edit this by typing:

crontab -e

This will open up the CRON file in your text editor, most likely vi again. If you’ve never added anything before, it’s also likely to be blank. No worries. Add these lines:

00 4 * * 0 /httpdocs/mybackupscript.sh

The format this command follows is a little difficult, but goes like this:

minute hour day-of-the-month month day-of-the-week

A * in the pattern ignores that item. So in the example above, we are going to run our backup script at 00 minutes 4 hours, every 0 (Sunday) of the week.

Here are some other examples to help you understand:

01 * * * * echo "This command is run at one min past every hour"
17 8 * * * echo "This command is run daily at 8:17 am"
17 20 * * * echo "This command is run daily at 8:17 pm"
00 4 * * 0 echo "This command is run at 4 am every Sunday"
* 4 * * Sun echo "So is this"
42 4 1 * * echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * echo "This command is run hourly on the 19th of July"

Once you’ve entered that, save the file by pressing ESC, then typing :write followed by :quit. A shortcut version of this is to just type :wq , which will both write the file and quit. It’s handy, but if you’re anything like me you forget these little shortcuts.

That’s it! You’ll now have an up to date copy of your database and entire site in the root, called backup.tar (or whatever you chose to name it). You might want to learn a little more scripting to add the date on the end of the filename and avoid overwriting the same one each time, but that’s up to you to discover. I hope you can see how powerful the command line actually is now!

Related topics: Computer Automation, Data Backup, Programming, Wordpress.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. Joseph Edward Joe Greenwood
    August 4, 2011 at 6:53 am

    Very helpful, sir!  Thank you.  What say you to elaborating on the scripting to add the date on the end of the filename to avoid overwriting the same one each time?  And you had mentioned a way to send this to something like an Amazon storage space ...  those two things added to this would seal the deal and I could walk away knowing my site is being backed up every morning at 4 a.m.  Thanks again for your contributions here ...  I'm checking out a bunch more of your posts.  -- Joe

  2. Alex
    May 6, 2011 at 8:36 pm

    mysqldump --add-drop-table -uusername -ppassword tablename > dbbackup.sqlLock all tables before dumping them. The tables are locked with READLOCAL to allow concurrent inserts in the case of MyISAM tables. Fortransactional tables such as InnoDB and BDB, --single-transaction isa much better option, because it does not need to lock the tables atall, but default WP tables is MyISAM. When you use mysqldump your blog is locked.Use replication to another mysql server and dump it, or use InnoDB tables

  3. pceasies
    May 6, 2011 at 8:04 pm

    You can achieve the same affect with Cygwin on Windows. Install Cygwin and open up a regular Cygwin prompt and follow the above directions. Adding a cron job should also add a task in Task Scheduler.