Coolest Guides on the Planet

coolest guides on the planet

Coolest Guides On The Planet

  • Home
  • macOS
  • WebDev
  • All Posts
  • Contact

Backup your website in cPanel the manual way and automatically with a script

February 14, 2011 13 Comments

Backup your website in cPanel the manual way and automatically with a script

cPanel gives you the option to back  up your website, but if you are on a shared or reseller plan it only a manual process which you initiate.

Various cPanels offered by hosting providers are different, this one is offered by HostGator on a shared hosting plan. In cPanel scroll down to the Files section and there are 2 options; Backups and Backup Wizard.

backup-options-cpanel

backup-options-cpanel

Both options offer the same thing, either a back up or a restore from a backup. Backup options are essentially the whole nine yards or various components. You can back up the entire website including root folder with mysql database and email forwarders and filters known as a full back up, or just the home directory known as a partial backup or only just the mysql database or email forwarders or filters separately also as partial backups.

choose whole back up, home or separate mysql and email options

choose whole back up, home or separate mysql and email options

When backing up you can either save the backup to a specified area on your webserver or ftp it to a remote site or simply download it compressed to your desktop.

save entire backup to your desktop

save entire backup to your desktop

Above is an entire website compressed ready to be saved to the desktop.

This is a great thing but relies on you remembering to do it!, a better way would be to set up a script that runs every right and backs up your website to a specified directory which you can download at anytime, this method backups your home folder contents only so a mysql database back up will also be required separately if required.

Basically we need to set up a script and run that in “Cron” which is a linux tool that allows commands/scripts to be run at set intervals.

1. First create a directory in your root level / (in a Linux environment with cPanel this will be one level higher than public_html) and name it /backups.

2. Create a file called backup.sh in /backups, either through a simple text editor like NotePad/TexEdit or TextWrangler or via Vi or Pico in the command line and paste the following:

#!/bin/bash
tar czf ~/backups/backup_`date +%Y_%m_%d`.tgz ~/public_html

 

This tells Linux that is a command and will save a compressed zipped file and file it in your /backups folder, name it backup_Year/Month/Day  and it is backing up the entire contents of the /public_html folder.

3. Change the file permissions of backup.sh to 700 so just the owner has read/write/execute permissions. Do this through either file manager in cPanel or FTP .

4. The final thing to do is to set this script/command to run automatically in Cron in your cPanel. Log into your cPanel and scroll to Advanced and click on Cron:

cron-in-cpanel-backup

cron-in-cpanel-backup

And in the resulting screen, add your email address in, the frequency of the backup, once a day should be fine and lastly and most importantly in the command field –

~/backups/backup.sh

 

Click add New Cron Job and all should be OK.

cron-settings-backup-cpanel

cron-settings-backup-cpanel

Don’t run this command more than once per 15 minutes, part of the terms of service on some ISPs (mine is HostGator) won’t allow this frequency, daily is fine. And to check just look in your /backups folder to see if the backup completed – any errors will be redirected to your email.

Doing it this way will make a cumulative back ups per day eventually filling up your allowed hard disk quota, you either can either manually mange this issue by deleting old back ups periodically or you can adjust the script so only a recent back up is kept.

This can be acheieved by making an extra line to the script:

#!/bin/bash
rm -rf ~/backups/*
tar czf ~/backups/backup_`date +%Y_%m_%d`.tgz ~/public_html

 

So the additional line “rm -rf ~/backups/*” will remove any previous backups prior to backing up the site so only one daily back up is available. – Make sure you get the tilde “~” in there! The tilde represents your home folder.

Taking this concept a step further would be to keep a daily, a weekly and a monthly back up on hand; so you would create three directories first at your root level such as at /public_html/:

/backups/daily/
/backups/weekly/
/backups/monthly/

Create three scripts:

backupdaily.sh:

#!/bin/bash
rm -rf ~/backups/daily/*
tar czf ~/backups/daily/backup_`date +%Y_%m_%d`.tgz ~/public_html

 

backupweekly.sh:

#!/bin/bash
rm -rf ~/backups/weekly/*
tar czf ~/backups/weekly/backup_`date +%Y_%m_%d`.tgz ~/public_html

 

backupmonthly.sh:

#!/bin/bash
rm -rf ~/backups/monthly/*
tar czf ~/backups/monthly/backup_`date +%Y_%m_%d`.tgz ~/public_html

 

Last thing to do is to add the three scripts as “cron” jobs following the previous steps above and set the schedules to daily, weekly and monthly to the relevant back up scripts.

To have these backups also backed up on your local computer automatically check out the follow up post on scripting your ftp to login and download the backups for you.

Cats: Open Source Tags: automatic, backup, cpanel, daily, email, hostgator, isp, monthly, mysql, weekly

Tags

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls

Donate a Beer to the Coolest Guides

Get Beaver Builder Now!

Discuss

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls
Get DesktopServer

Lynda

Lynda.com Online Training Videos

TreeHouse

smlinks

Learn WordPress
osx-modify-shell-path

How to Add to the Shell Path in macOS Big Sur and Catalina using Terminal

October 19, 2019

virtual-hosts osx 10.10 yosemite

Set up Virtual Hosts on macOS Catalina 10.15 in Apache

October 19, 2019

Installing Homebrew on macOS Catalina 10.15, Package Manager for Linux Apps

October 18, 2019

Where is the bash shell in macos Catalina?

October 12, 2019

Refine your search

  • All
  • Modules
  • Themes
  • Documentation
  • Forums & Issues
  • Groups

RSS ars technica

  • Apple details the end of Intel Mac support and a phaseout for Rosetta 2
  • Apple tiptoes with modest AI updates while rivals race ahead
  • Apple drops support for just three iPhone and iPad models from iOS and iPadOS 26
  • macOS Tahoe signals that the end is near for Intel Macs, dumping all but four models
  • With visionOS 26, Apple begins to zero-in on what the Vision Pro is actually used for

RSS mac surfer

Donate

Copyright © 2025· Neil Gee - All Rights Reserved - Hosted by Runcloud

Copyright © 2025 · gee on Genesis Framework · WordPress · Log in