How to Setup a Cron Job for taking Nightly Backups of Databases

Written by: Muninder

Updated: March, 27, 2011

Sometimes you are in such a situation where your blog gets hacked or broken and you don’t have a single backup. Such a situation is a nightmare for any blogger.
Plugins help you in this situation by automating the process of backing up your WordPress databases. But what if the plugins don’t do their job properly ? As you may know we moved from BlueHost to HostGator a few months ago.
I used to use a popular plugin – WP DB Backup for backing up my SQL database and sending it to my Email ID daily.

After the migration of hosts, I found that WP DB Backup stopped doing it’s work. It never sent me a backup after that. The reason behind this was that, WP DB Backup still had the old host’s i.e. BlueHost’s directory path.
Then I decided to remove it and try some other backup options. Unfortunately other popular backup options also used the WP DB Backup plugin’s concept. They were just an extension to WP DB Backup.
So, I was left with an option to either manually backup every day or to setup a Cron Job. Manually backing up is such a waste of time. So I setup a Cron Job to take Nightly Backups. I know setting up Cron jobs is not an easy task.
It requires some basic knowledge of Linux commands. So today I will be listing out some steps to Setup a Cron Job which will take Nightly  Backups of your WordPress Database.

Setting Up a Cron Job for Nightly Backups

The first thing you need to do is to ensure that your host supports Cron Jobs. Most popular hosts like HostGator, BlueHost, DreamHost,etc  do support it.
If you don’t have a host that supports Cron Jobs, then you may consider purchasing 3 Years Unlimited Hosting for just 0.99$ from Hostable.

Cron Job for Nightly Backups
Set up a Cron Job correctly

  • Download this file (cronjob.sh).
  • Open it with Notepad++ or whichever editing tool you prefer.
  • Find DBNAME, DBPASS, DBUSER & EMAIL. Replace their values with your Database Name, User ID, password and the email where you want the Database Backup to be mailed to.
  • Now upload this file to the ‘etc’ folder in your directory. This is to prevent visitors from accessing your file.
  • After you finish the upload, just change the File Permissions to 711.
  • Now, click on the Cron Jobs icon (normally located in the Advanced section).
  • Ensure that the email address in the Email field points to your own ID.
  • In the Add New Cron Job section, click on the Common Settings dropdown & select Once a Day. This will automatically fill the rest of the fields (See the Above Image for ref.)
  • In the Command Input field, enter the path where you’ve uploaded the cronjob.sh file (i.e. /home/youraccountname/etc/cronjob.sh).
  • Finally, click on the ‘Add New Cron Job’ button.

With this you’ve successfully set up a Cron Job to deliver Nightly Backups right into your Inbox. Note that your backup will be sent only when it is midnight at the server location and not when it is midnight in your country or place.
If you liked this WordPress tutorial, then you can Subscribe to Our RSS Feeds / Daily Updates. You may also take a look at the Top 10 WordPress Plugins I use on DailyBlogging. Feel free to drop a comment if you’ve any problems in setting up the Cron Job.
Which plugin or method do you use to Setup Automatic WordPress Database Backups ?

by