Backup your database by setting Cron Job.

Hello Guys, I am sure you have encountered a situation where some of your script demands to be executed at a regular interval of time and I am sure you must have thought once to run it manually by hitting the URL once a day or once in an hour. In today’s automated era doing these things manually doesn’t make sense to me does it makes sense to you?

Well, we have come up with the simple solution for this kind of problem by setting up the CRON JOB. Cron job is nothing but a simple Linux program which runs at a regular interval in order to facilitate you to run your scripts which are vital to you at different intervals over your server.

CRON is also termed as a daemon which simply suggests that you need to activate the CRON once and it will keep on running at regular intervals as defined by you till it is destroyed.

Today we will go through a process on how to backup your database at a regular span of time by using a simple CRON job.

So Let’s Start.

Creating your script which backs up the database and sends emails to specified email address.

For sending the mail we will use Mime extension.

  • Select the database you want to get regular backups. Obtain the username and password which are required to access the database like for localhost (if you have default configuration) its Username: root & password: (Null).
  • Now we will create the actual script which will obtain the copy of database from the server and mail it to your email address.




$dbhost = “localhost”;  // usually localhost

$dbuser = “Your Database Username”;

$dbpass = ” Your Database Password”;

$dbname = “Database you wish to backup”;

$sendto = “Your Email ID”;

$sendfrom = “From which you want mail to be delivered”;

$sendsubject = “Your Subject”;

$bodyofemail = “Body of the Email”;

// don’t need to edit below this section

$backupfile = $dbname . date(“Y-m-d”) . ‘.sql’;     //Name of the database file

system(“mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile”); //Extracting the file from the server.

// Mail the file

include(‘Mail.php’);   //Mime extension for mailing purpose.

include(‘Mail/mime.php’); //Mime extension for mailing purpose.

$message = new Mail_mime();

$text = “$bodyofemail”;



$body = $message->get();

$extraheaders = array(“From”=>”$sendfrom”, “Subject”=>”$sendsubject”);

$headers = $message->headers($extraheaders);

$mail = Mail::factory(“mail”);

$mail->send(“$sendto”, $headers, $body);

// Delete the file from your server



Do not forget to include the mail.php and mime.php file in the backup.php. If you don’t have mime library Download Here

Now after making our script ready we have to create a CRON job at the server which will continuously call the script depending upon the time interval we have specified

Steps to be followed for creating Cron Program.

  1. Now after making our script ready we have to create a CRON job at the server which will continuously call the script depending upon the time interval we have specified.
  2. Log in to your server’s Cpanel and navigate to Cron Job. If you are unable to find Cron job menu make use of the search bar provided to you.
  3. After going to Cron job add a new Cron job.
  4. Select the time interval during which you wish your Cron job will run it can either be a day, Twice a day, Every minute or in 5 minutes.
  5. Next step is to write a command which will specify the path to your file. So your file can be executed at regular intervals.There are many ways to educate cron programme about path of your file but the simplest one is.

     /usr/bin/php public_html/{path to your file}

    For example

    /usr/bin/php public_html/

6.  Just save the CRON job and you are done.

Just wait for the right interval of time and you will receive an Email containing a backup of your database.

So doesn’t it seems awesome when your backups (such a painful activity) gets automated by just going through some simple steps.

We Hope this will help our young developer to expand and face the hurdles of the development

Because Hurdles aren’t really Hurdles.





    • Deepak Shah said:

      thankyou and yes offcourse we are working on more blogs so that we can help our new developers

      May 21, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *