How to create and download website backup automatically

Create a directory called /backup under the /home directory of your cPanel account:

Then, create a file called backup_server.sh inside:

The file must be executable, so don’t forget to set the permissions to 0755 following these steps.

Once done, Edit the backup_server.sh file and paste the following code inside:

#!/usr/bin/env bash
NAMEDATE=`date +%F_%H-%M_%s`_`whoami`
mkdir ~/backup/$NAMEDATE -m 0755
mysqldump -u dbusername -p"dbpassword" dbname | gzip > ~/backup/$NAMEDATE/db.sql.gz
tar czf ~/backup/$NAMEDATE/files.tar.gz ~/public_html
chmod -R 0644 ~/backup/$NAMEDATE/*
exit 0
The database parameters are to be adjusted, namely:
  • Replace dbusername with the actual database username (we’ve used nctest_production as an example). This can be checked in your website’s config file or in the MySQL Databases menu.
  • Replace dbpassword with the actual password for the selected database user. Note that there is no space between -p and the password itself.
  • Replace dbname with the name of the database.

You can also use the absolute paths (e.g., /home/username/…), specifying a different directory to be backed up and/or a different one to store your backups. Don’t forget to double-check if all the paths are correct.

Once you have edited the file to have the correct parameters, save it:

Open the Cron Jobs menu in your cPanel account and add a cron:

  • Minute: 30
  • Hour: 0
  • Day: *
  • Month: *
  • Weekday: 1,4
  • Command:
/bin/bash ~/backup/backup_server.sh 2>&1

With these settings, the backups will be created at 12:30 AM EDT every Monday and Thursday.
MinuteHourDayMonth, and Weekday can be adjusted according to your needs.

Do not forget to change the script path in the Command line if you’ve placed it in a different folder and/or under a different name.

The script creates a folder with the name that contains the date and time of the backup creation; it looks like this: /home/nctests/backup/2018-07-25_18-54_1532559254_nctests
The folder contains two .qz files inside - the backups of the website files and database.

 

To avoid overusing your account’s disk space, let’s set up another cron job which will clean up the backups older than one week. Acting similarly to the way described above, create the following job:

  • Minute: 0
  • Hour: 0
  • Day: *
  • Month: *
  • Weekday: 0
  • Command:

cd ~/backup; find . -type d -mtime +7 -exec rm -rf {} \; 2>&1

This job will run every Sunday at midnight EDT and will remove all backups older than 7 days. You can adjust these settings according to your needs.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Disk space

Disk space is the amount of server disk space allotted per hosting account by a hosting company....

What is an index page?

The index page is the URL or local file that automatically loads when a web browser starts and...

Point your domain to a simple "Coming soon" page

Using Edit 1. Log into cPanel and navigate to File Manager menu in Files section: 2. Navigate...

How to optimize hosting account space

In this article we will explain how to clean up your hosting account making some disk space for...

How to use the Browser Console

Browser Console Browser Console is the analog of command-line interface, but for the whole...