How to Schedule Database Backup Using Cron Job

For backing up of your WordPress database, there are plenty of plugins around that can get the job done. Some of them backup only your database while others backup the whole WP directory. If you have used WordPress long enough, you will realize that these plugins are not reliable. They will work one day and fail another day, for thousand and one reasons. It could be that they are not compatible with the new WordPress version that you have upgraded to, or that they are conflicting with a newly activated plugin. As in my case, it could also because your database is getting too big for them to handle.

Whatever the reasons, rather than relying on plugins, a better alternative is to use a cron job in your server and schedule it to backup your database everyday.

Note: The following instruction can be used to backup any database. The only requirement is that you are using a Linux server to host your files.

Here is how you do it:

1. Login to your web host control panel. If you are using cPanel, look for a Cron Job icon.


2. You can set the time and frequency to backup the database. Under the command field, copy and paste the following command:

  • Replace dbusername with the database user
  • Replace dbpassword with the database user password
  • Replace dbname with the database that you are backing up
  • Replace the path-to-store-the-backup-file to the file path in your server where you want to save the backup

Note: In some server, you might need to put a pair of single quote around the dbpassword for it to work. For example:

Email yourself the database

Storing the database on your server could take up plenty of storage space. One way you can do is to email the backup file to yourself.

1. Download this php script.

2. Extract the zip file and open the backup.php with a text editor.

3. Change the database detail:

4. Save the file. Upload the whole folder to your server. Note: The folder includes a .htaccess file to prevent any access from the browser. If you want to access from the browser, remove the .htaccess file.

5. In the cron job section of the cPanel, enter the following command:

That’s it.

Damien Damien

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.


  1. Nice tutorial.  I use AutoMySQLBackup script to do backups and then make a cron job for it.

  2. Good tuto… i use Website 2 Backup because the backup is encrypted and compressed

    1. Great website, but it requires a fee. I don’t see the point of paying for
      something that can be done easily.

      1. Agree but backup encryption is needed for e-commerce or websites with sensitive data.

    2. Hello Axel,

      I would also like to use Website2Backup, but the only thing that can’t get working is the part with the Cronjobs, maybe you could help me ???

      Thanks in Advance

      Tony Riches

  3. Nice HowTo!

    But as an Linux SysAdmin I really recommend to use “/usr/bin/php -q /path-to…” instead of “php” only when using cron jobs.

  4. Nice script, any comments on how to encrypt the attachment before emailing? I’m pretty paranoid with this kind of stuff…

    1. Encryption will require additional PHP code or bash command and I am not familiar with it. I am sorry I can’t help you with this.

  5. Great script, I have implemented it just fine….. HOWEVER the ‘sending’ email is also receiving a copy of the email that is sent.

    How can i disable this so that it is ONLY sent to the ‘send to’ address not copied to ‘from’ address.



      1. Yeah that would work, but two issues could arise.

        a) someone could actually own the dummy address I specify and then would receive a copy of my site database every day.
        b) if i used a address, the server may continue to attempt to resend over and over (until eventually giving in) which would pile up over a few days causing unnecessary processing.

        No quick fix comes to mind? ;)


        1. It seems that this only happen to you as I am not facing this problem. Perhaps you can set a schedule to delete email from the send inbox at regular interval?

Comments are closed.