How to create or configure cron job in cpanel and backup single/multiple database

December 12, 2013


<?php global $base_url?>


If you running a website or web application, you may worried about your database backup. Database backups really really essential if you run web based software like student management system or library management system or whatever.

So whether you are a website owner or web developer or web designer, its better to learn a little tweaks about cron job.


What is cron job ?


cron is a software utility. cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration like sending email , downloading email at regular intervals.

Requirements for doing cron job

  • Control panel username and password
  • database username and password

Create backup directory

Assume, you entered into control panel. Click file manager in files section. Click your home link and create a folder named backups in /home/username (see figure 03)

STEP 01: Database backup: write a command in cron job settings

Assume, you enter into control panel. Now select cron job from advanced settings area (figure 01)

figure 01: Select cron job from control panel


Now from cron job settings (figure 02), add the following scripts and finally click Add New cron job button. For instant testing set cron job in every five minutes from common settings: Every 5 minutes (*/5 * * * *)

Single database backup

<?php/usr/bin/mysqldump --user=username --password=password --host=localhost database_name gzip &gt; /home/webemani/backups/dbbackup.sql.gz ?>

All database backup

<?php/usr/bin/mysqldump --user=username --password=password --host=localhost --all-databases gzip &gt; /home/webemani/backups/dbbackupall.sql.gz ?>

Code explanation

  • username: Your database user name
  • password: Your database password
  • database_name: Your database name. If you want to backup all databases, replace database_name with --all-databases
  • /home/webemani/backups/dbbackup.sql.gz: here, webemani is control panel username, backups is a backup directory where backup file will be stored and dbbackup.sql.gz backup file name
figure 02: cron job settings

STEP 02: Database backup using shell script

Disadvantages of above script is, each time old database file is replaced by new. So we now write a script and add datetime along with backup file name, so that each time cron is running a new backup file will be create.
For example, if we setup cron setting for every 5 minutes for backup database, the following files will be created every five minutes

  • dbbackup_20131126_1600.sql.gz
  • dbbackup_20131126_1605.sql.gz
  • dbbackup_20131126_1610.sql.gz
  • dbbackup_20131126_1615.sql.gz
  • and so on....
figure 03: database backup using bash script

For this, go to your cpanel home location. Make sure, you clicked home link (figure 03). Now create a bash file , open it for edit and write the bellow code and save it

<?php#!/bin/bash<br />
date=`date +%Y%m%d_%k%M`;<br />
usr/bin/mysqldump --user=username --password=password --host=localhost database_name gzip &gt; /home/webemani/backups/dbbackup_$date.sql.gz ?>


Backup multiple database

<?php#!/bin/bash<br />
date=`date +%Y%m%d_%k%M`;<br />
usr/bin/mysqldump --user=username --password=password --host=localhost database01_name gzip &gt; /home/webemani/backups/dbbackup1_$date.sql.gz /usr/bin/mysqldump --user=username --password=password --host=localhost database01_name gzip &gt; /home/webemani/backups/dbbackup2_$date.sql.gz ?>

Put Shell script file inside command line like: /bin/sh /home/webemani/

Go to your backups directory and wait to see your backup database files

If you found this tutorial useful, then please leave a comment. Thanks in advance.