Use Cron Jobs to Automatically Backup your Website Daily

For the successful and safe running of a Website, many points need to be considered. But one that requires 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 backups of your website.

In this tutorial, I will tell you how to back up 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 that 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 a 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 backups can become a tough job if you are taking backups on an hourly or daily basis.

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

Recommended Article: Plugin to Save WordPress Blog from Virus

Almost all Linux server-based web hosting companies like Hostgator and 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) that is used for the 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 tasks.

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

Cron command consists of 6 fields. The first 5 fields define minutes, hour, day, month, and weekday respectively and the last field is for the command that you want to run.

Recommended Post: Pinterest Looking WordPress Themes

Prerequisite for Backup Cron Job

I. Go to the root (parent directory) of your hosting and create a folder called backup or whatever you want. Here for this tutorial, I have created a “backup_t2b” folder to take a 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 the below code in it and save it (.sh is an extension for the 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 the above code in a notepad file and upload it to your hosting using FTP or a control panel. The above code will save your backup data in a backup folder that you have created and will save it according to the date in zip format.

In place of “yourblog.com” enter your domain name. You can also take a full backup of every website in your hosting account by using the 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 the above script only if you have unlimited web space in your hosting account. Some of the good hosting providers that give unlimited space are Interserver, 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 the 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 the cron job setting panel. Here first of all put the 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 settings which are given there. Let’s say you want to run your website backup every day throughout the year at sharp 9PM, then use the setting provided in above image.

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

~/backup_t2b/script/bck.sh

Tilde (~) sign means your home directory and the 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 the current cron job list.

Cron Job Schedule

When your scheduled 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 an example of such a 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 backups 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 backups of your website automatically. If you have any problem with the cron job setup, please let me know through your comments.

If you like the post please consider sharing it with others on Facebook and other social media channels so that others can also take advantage of it.

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.