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

setup cron job

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 (
  • 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 file (i.e. /home/youraccountname/etc/
  • 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 ?

Posted by

I like using and tweaking WordPress. Understrap, Tailwind, Bootstrap are a few words I would add to my Tech Profile. Besides that, I constantly observe my website's technical and SEO performance.


  1. Thanks for the your advice but in my case, I have been using your previous solution are I have to say they works very well. I receive data backup daily and no worry about losing my data. Anyways, this alternative perhaps will be a coming solution as well.

    BTW, I am using Hostgator too.

  2. I was just using a plugin for getting backup. Now, I will try this alternative too. Thanks for sharing.

    1. even i used a DB backup plugin which did the work weekly.. looks like we set it ourselves… thanks for the tutorial Mani.. Even almost all hosts support Cron Jobs..

  3. I currently have two plugins that run jobs in the early morning hours to backup both my WordPress files and my database. Both then e-mail the compressed file to another e-mail address not associated with my blog.

    The only issue I can see with this is eventually I may reach a point where the file is too large to e-mail. In such a situation I may be looking at using FTP to transfer the backup files.

  4. Nice Way to backup, Good Job, I will enable this features today only, Thanks for the share mani 🙂

  5. This cron job method is good. I think it should run a little faster compared to using a plugin.

Comments are closed.