<?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)
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_select_cron.jpg" rel="prettyPhoto" title="Select cron job from control panel">
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_select_cron.jpg" />
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 > /home/webemani/backups/dbbackup.sql.gz ?>
All database backup
<?php/usr/bin/mysqldump --user=username --password=password --host=localhost --all-databases | gzip > /home/webemani/backups/dbbackupall.sql.gz ?>
- 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
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_add_new_job2.jpg" rel="prettyPhoto" title="cron job settings">
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_add_new_job2.jpg" />
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
- and so on....
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_bashscript.jpg" rel="prettyPhoto" title="database backup using bash script">
<?php print $base_url;?>/sites/default/files/tuts/cron_job_cpanel_bashscript.jpg" />
For this, go to your cpanel home location. Make sure, you clicked home link (figure 03). Now create a bash file backup.sh , open it for edit and write the bellow code and save it
date=`date +%Y%m%d_%k%M`;<br />
/usr/bin/mysqldump --user=username --password=password --host=localhost database_name | gzip > /home/webemani/backups/dbbackup_$date.sql.gz ?>
Backup multiple database
date=`date +%Y%m%d_%k%M`;<br />
/usr/bin/mysqldump --user=username --password=password --host=localhost database01_name | gzip > /home/webemani/backups/dbbackup1_$date.sql.gz /usr/bin/mysqldump --user=username --password=password --host=localhost database01_name | gzip > /home/webemani/backups/dbbackup2_$date.sql.gz ?>
Put backup.sh Shell script file inside command line like: /bin/sh /home/webemani/backup.sh
Go to your backups directory and wait to see your backup database files