How to create a cronjob cPanel backup

→ Are you a new visitor? Please visit the page guidance for new visitors ←

There is no better time to create backups, well at least that’s what I personally think. Backups are important, expecially if these are databases.

In this article I’m going to explain how you can create automatic backups using cPanel Cronjobs.

So let’s get started, first go into your cPanel account in section mysql databases and create your self a mysql user. This will be used to backup your databases. For eg. you can name it “backup”.

Cpanel Dataabase

Cpanel Dataabase

This new user you created needs to be added to every mysql database that you want to create a backup, of course with all privileges that are needed.

Next please create a folder named backups in /home/username

cPanel backup folder

cPanel backup folder

In folder backups create the file dbbackup.sql.gz ; In fact, this file is an archive, just we create the archive the same way we create any other file, but with the extension .sql.gz

Now we need to create the bash script that will handle the actual backup. In my example, the bash script will be named “” and we add the follow lines to this file:

A little explanation may be needed … So the 3rd line of code will always save the current date in a variable to use it in the mysql backup dbbackup.sql.gz for us to always have a new backup.

The next line is the actual backup which will dump the mysql database and gzip it.

username – this is your cPanel username
backup – this the mysql user you created before
DBPASS – the mysql password used with your mysql user (If using your main cPanel user, that’s your cPanel user password)
–all-databases – we are just telling that we want to backup all databases, no changes needed here

If you want to create individual backups for each mysql databae, you can use the following code:

–databases – by this we are telling that we want a specific database

Now that we are ready with the bash script, we now need to create a cronjob which will run our script at a specific time. I set it to run every 30 minutes, but you should really run it once a day.

This should look similar to this picture:

Comanda Cronjob cPanel

Comanda Cronjob cPanel

That’s all, if you have any problems or you believe there’s anything else that you want me to write about, feel free to contact me.

➥ File backup script using cronjob

  • Really informative blog article.Much thanks again. Really Cool.’s a Very good, very useful to me, Thank you very much

  • I like what you guys are up also. Such intelligent work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my site :).

  • This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!

  • Hey i tried your command but its only backing up the schema of the database not the records and only the first table is unlocked rest of the tables are locked

    • That is not normal, mysqldump will use the mysql user privileges to dump your tables in a file, so make sure your mysql user has the proper grant privileges.
      If using cPanel you can easily do this when adding the mysql user to your database.

  • Nam Nhi Chí

    Thank you so much. This script is a very useful method for mysql cron backup. How about directories backup? Could you show me please!!!! Thanks again.

    • You can use a similar script with the zip command I believe, haven’t tested it and its just a quick thought, you can use something like:

      • Nam Nhi Chí

        Could you explain this script, please?. And also, it seems to need the file “mysqldump” in server root to execute the backup command. But I can’t find any file to execute the directories backup command in the above script. I’m still wondering.

        • The mysqldump and zip commands should already be on the server, you don’t need to have them in your cpanel account as actual files. The above script is really similar to the mysqldump, it creates a filebackup_$ archive witht he contents from the folder_to_backup . Again I did not tested it, can’t really at the moment, I may be creating a new article specifically for this after New Years!

          • Nam Nhi Chí

            I used your method to backup my web data. But it didn’t work. Here is my code:


            date=date +"%Y-%m-%d";
            /usr/bin/mysqldump -h localhost -u admin -ppassword --databases domain | gzip > /home/admin/backups/domain_$date.sql.gz
            zip -9 -r /home/admin/backups/domain_$ /home/admin/

            I can’t find any problem in the code. I created cron job through cpanel so I think it had no problem with the crontab.

        • I had to remove your comment, please do not post usernames and passwords on this site, it is a public website and you can have surprises afterwards.

          Try using the full command of the zip:

          As I said before, I can not test it at the moment, so please wait until after New Years and I’ll be happy to make a new article similar to this.

Request an article ←