Use Cron Jobs to Automatically Backup your Website Daily

For successfully and safely running of a Website, there are many points which need to be considered. But one which require more attention is a backup of your website files and database. If you have a full backup of your website/blog you can again recover your website in minutes, if your website has been hacked or compromised.

So, it is always advisable to take regular backup of your website. In this tutorial I will tell you how to backup your all websites/blogs or any particular website using a Cron Job in cPanel. A website can be WordPress, Joomla, Drupal, HTML, or any other which you have hosted.

 

Using Cron to Backup Website blog

 

Every hosting company gives your backup facility using their backup wizard but none of them take 100% guarantee to restore your website when required, so taking manual backup yourself is a good option and choice. Logging into your hosting control panel and taking manual backup can become a tough job if you are taking backups on hourly or daily basis.

Thus to solve this problem and making an automatic backup process in cPanel, we can use Cron jobs.

 

Recommended Article: Plugin to Save WordPress Blog from Virus

 

Almost all Linux servers based web hosting companies like Hostgator, Bluehost provide the facility to schedule a cron job. And these Cron jobs can be used to make your daily, weekly or monthly backup of websites on a regular basis.

 

What is a Cron Job?

 

Above I talked a lot about Cron job, so what exactly is a CRON JOB ?

A cron job is a Linux command (crontab) which is used for permanent scheduling of a task which can be a command or script on your server. Usually this command is used for auto scheduling of repetitive task.

Suppose you have to log cleaning script which needs to be run on daily basis through out the year, then this can be easily done by use of Crontab command and thus making your job automatic.

Cron command consist of 6 fields. First 5 field define minutes, hour, day, month and weekday respectively and the last field is for command which you want to run.

 

Recommended Post: Pinterest Looking WordPress Themes

 

Prerequisite for Backup Cron Job

 

I. Go to root (parent directory) of your hosting and create a folder called backup or whatever you want. Here for this tutorial I have created “backup_t2b” folder to take backup of Tech2Blog.com.

II. Create another folder called “script” and create a blank file in it called bck.sh. Now edit this blank file and paste below code in it and save it (.sh is extension for script file).

#!/bin/bash
# Backup Script by Tech2Blog.com
NOW=$(date +"%Y-%m-%d")
tar cfz ~/backup_t2b/bck_$NOW.tar.gz ~/public_html/yourblog.com

You can also copy above code in notepad file and can upload it to your hosting using ftp or control panel. The above code will save your backup data in backup folder which you have created and will save it according to date in zip format.

At place of “yourblog.com” enter your domain name. You can also take full backup of every website in your hosting account by using below modified script.

#!/bin/bash
# Backup Script by Tech2Blog.com
NOW=$(date +"%Y-%m-%d")
tar cfz ~/backup_t2b/bck_$NOW.tar.gz ~/public_html

Use above script only if you have unlimited web space in your hosting account. Some of good hosting provide which gives unlimited space are Bluehost and Hostgator.

 

Note: Don’t forget to give execute permission to this script file. You can set your script permission to 0755.

 

Step by Step Guide to use Cron Job to take Backup of Website

 

The main component of this backup Cron job is the backup script, which will initiate backup for you. I have already given you the backup script above, now let’s begin with the other steps.

1. Go to your hosting control panel and search for Cron Jobs, basically it in advance as shown below.

 

hosting Cron Jobs

 

2. Now you will see cron job setting panel. Here first of all put email address where you want to get email alerts when your job is finished.

 

Cron Job setting

 

Now you can choose some of the common setting which are given there. Let say you want to run your website backup every day through out the year at sharp 9PM, then use setting as provided in above image.

Now in command line put the location of your script file as below.

~/backup_t2b/script/bck.sh

 

Tilde (~) sign means your home directory and rest is the path of your script. After completing your Cron settings as per your requirement click on Add New Cron Job. Once the job is saved it will show in current cron job list.

 

Cron Job Schedule

 

When your schedule cron job is completed you will get an email about the success of the job. And you can verify it by going to your backup folder. Below is the example of such backup.

 

cron job successful backup

 

This is how you can schedule automatic backup for your website using Cron Jobs. You can similarly set different cron jobs for weekly and monthly backup by modifying the above script. This backup zip file can be downloaded to your personal computer easily. This configuration was through cPanel and can also be done using SSH and FTP.


Hope you have found this tutorial helpful and it will help you in taking regular backup of your website automatically. If you have any problem with cron job setup, please let me know through your comments. If you like the post please consider sharing it with others on Google+, Facebook so that others can also take advantage of it.

2 Comments

Leave a Reply