Schedule Automated Backups of Your Website in cPanel Using cron Jobs

Take control of your cPanel backups and make sure you generate and download backups of your files and MySQL databases automatically on a regular basis.

Most hosting providers that offer cPanel also offer some kind of backup functionality for your files and MySQL databases.

I prefer to have control over the backups, to know what is backed up, when, where and how often.

This article will show how to create a backup of your website files and database on a regular basis and how to download those backups to a different machine.

Create a MySQL account with limited privileges

You want to create a new account just for the purpose of backing up your database(s). The reason to use a different account (other than the account used by your website to access the database) is because the backup does not require too many privileges and you should not grant more privileges than required to any account.

Create account

In cPanel, go to Databases > MySQL Databases and scroll down to MySQL Users. Give your new account a username and a password and click on Create User. To keep things simple, in this example I gave the new user the name backup.
NOTE: The actual name is mydomain_backup (where mydomain is the prefix added automatically by cPanel based on your account name)

Grant privileges required for backup

After you create the user, you need to click Add User To Database to grant it the privileges required to perform the backup. In this case the minimum required privileges are: SELECT and LOCK TABLES

Create a folder to store the backup

You don’t want to store the backup in your public_html directory as this could be a security vulnerability. Any user would be able to download the backup if they could guess your backup file name. To prevent this, you should create a new folder to store the backup, outside of the public_html folder.

Go to Files > File Manager and press on “+ Folder” to create a new folder. For this example I used the folder name backup.

Create cPanel cron jobs to backup the databases and files

Now we have to create cron jobs to backup the MySQL database and to backup the files of your website. If you have multiple databases and/or you want to create different backups for different folders, you may create several cron jobs. In this example we will have on job for a MySQL database and one job for the public_html files

In cPanel, go to Advanced > Cron Jobs > Add New Cron Job.

Use the Common Settings to choose the job’s frequency (e.g. Once Per Week). This will populate the fields Minute, Hour, Day, Month, Weekday. You can then modify these fields as per your requirements

In the Command field, enter

mysqldump -u mydomain_backup --password=mypassword --no-tablespaces mydomain_db > /home/mydomain/backup/mydomain_db.sql

This command will backup the MySQL database mydomain_db using account mydomain_backup and store the backup in /home/mydomain/backup/mydomain_db.sql

Click Add New Cron Job to save the new job

For more details on how to use mysqldump, go to: Backup & Restore MySQL / MariaDB Databases with mysqldump

Now let’s add a new job to backup the files

Press Add New Cron Job again setup the frequency just like you did with the previous job and in the Command field, enter

tar -pczf /home/mydomain/backup/mydomain_www.tgz /home/mydomain/public_html

This command will create an archive called mydomain_www.tgz in the folder /home/mydomain/backup/ with the files  from you public_html directory (your website).

Setup FTP account to access the backup folder

After the account is created, it will be listed under FTP Accounts (below the account creation form) and there will be a link called Configure FTP Client which shows connection details like FTP Username, Server and Port.

NOTE: If you called your account mrBackup, the actual name will be mrBackup@mydomain.com (replace mydomain.com with your domain name)

You now have everything setup to create backups of your database and files on a regular basis on the same server as your website. Whenever the cron jobs are run, a new backup will be created that overwrites the previous backup. To be safe, you need several versions of these backups. Depending on how often the data is changed on your website you can decide how often to generate the backups and how many versions to keep for each period (e.g. daily backups for the last week, weekly backups for the last three months, monthly backups for the last year, yearly backups since the website was live, etc.). For most websites, I usually keep weekly backups for a few months and then monthly or quarterly/yearly backups. It’s important to keep older backups because sometimes an issue may occur and you don’t notice it immediately and you may want to check how your website was 3 months ago, etc.

The next step is to download these files on a different machine (a local machine or somewhere else) to preserve them and have them ready when you need them.

Write a Linux bash script to download the backups

Here is a simple script that you can run on your Linux box to download the backups from the server and store them on your machine. In this example we create a new file called retrieveBackups.sh and store it in /home/me (you may want to replace me with your user’s home folder or any other folder you prefer).

vi /home/me/retrieveBackup.sh

Here is the script content which assumes folder /home/me/backup exists and then creates a subfolder with today’s date and stores the backup which is retrieved using wget.

#!/bin/bash
mkdir /home/me/backup/$(date +"%Y-%m-%d")
cd /home/me/backup/$(date +"%Y-%m-%d")
wget --ftp-user=mrBackup@mydomain.com --ftp-password=mypassword ftp://ftp.mydomain.com/*

To run the script we need to give it execution rights:

chmod +x /home/me/retrieveBackup.sh

After we verified that the cPanel cron job already populated the backup folder on the server with a backup, let’s test the script

cd /home/me
./retrieveBackup.sh

Create a cron job to run the script regularly

The final step is to create a cron job on your Linux box to run the script that downloads the backups on regular basis

If you set up weekly backups on your cPanel, here is an example (Linux) cron job to download the backups one hour later.

crontab -e
0 1 * * 0 /home/me/retrieveBackup.sh

For more information on how to create a cron Job in Linux, go to: How to Create cron Jobs – Schedule Tasks in Linux.

Check your backup folder after a week to see everything is fine.

Don’t forget to clear some of the older / unnecessary backups from time to time as at some point you may run out of disk space. It happened to me several times 🙂

You might also want to move some backups to a different storage from time to time as you never know when a disc may fail.

Good luck keeping your data backed up and safe!

Leave a Reply

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