How to Backup your Website Automatically with cPanel

July 26th, 2009

In my last post, I released my Simple FTP Backup Script (v0.9b). This Perl script allows you to perform a backup of your files and MySQL databases as well and upload it to a remote FTP server (read this post for some FTP backup services).

Since the release of this backup script, I’ve made some small adjustments to some minor preference parameters you can now set. Click here to download the Simple FTP Backup Script v0.9c

How to a Schedule a Backup Task using cPanel

This tutorial has been written assuming you’ve already got a remote ftp storage. I will be using my BlueHost control panel (cPanel) so yours may look slightly different.

  1. Rename the backup script file name to “ftp-backup-script.pl” and upload it to your website account through FTP. Make sure you’re uploading it in a non-public folder if possible. If not, make sure to protect this directory so that nobody else invokes the script from a web browser. Personnally, I suggest you create a directory at the same level as /public_html
  2. Use your web browser to access cPanel (http://www.yourwebsite.com/cpanel).
  3. Click the Cron Jobs icon :

    BlueHost cPanel - Cron Jobs

  4. On the next page, click the Standard button.
  5. Enter the command to run :

    /usr/bin/perl /path/to/backup/script/ftp-backup-script.pl

  6. Scheduling the task like shown below would run the backup script once a day at 3 AM :

    BlueHost Cron Jobs

  7. Hit the Save Crontab button and you’re done!

If it doesn’t work, try the following :

  • On the Crontab page, enter an email address where the cron output will be sent to. This might give you valuable information as to why the backup task didn’t run properly.
  • Try setting the permissions on ftp-backup-script.pl to 0755
  • Make sure the path to Perl is /usr/bin/perl. In case you don’t know, contact your web host’s support.

Stay tuned as I’ll show you how to schedule daily, weekly and yearly backup tasks.

Links

20 responses so far ↓

avatar

1. Response by : Damon Flowers on Aug 20, 2009 at 11:28 pm

Hey, thanks a bunch for posting your backup script… I just dropped it in and it works well (I’m on the same host as you), but couldn’t get the ftp piece to work (pointing to drivehq)…keeps saying connection timed out. My perl skills are a bit rusty, so may try debugging later.. If there is an easy suggestion for debugging, please let me know.. Thanks again for posting!

avatar

2. Response by : The Web Hosting Hero on Aug 21, 2009 at 7:23 am

Perhaps you may want to try using the IP address of your FTP server?

avatar

3. Response by : The Web Hosting Hero on Aug 21, 2009 at 7:24 am

(I meant using the IP address instead of the FQDN)

avatar

4. Response by : Damon Flowers on Aug 22, 2009 at 2:23 pm

Hmm, tried the IP as well as a different ftp site altogether, but continue to get a Connection Time Out error. I changed the Debug => 5 on line 78, hoping that would provide more info, but it didn’t.. Strange that yours works at bluehost and mine doesn’t..

avatar

5. Response by : Andy Clark on Sep 1, 2009 at 9:07 am

Great script, thanks. Worked first time with no problems :-) Would it be easy to add the time (as well as the date) to the file name that is created, we want to run the cronjob a couple of times as day without overwriting the file name? Thanks.

avatar

6. Response by : ali on Sep 28, 2009 at 6:10 am

can not extract mysql.gz with cpanel , says not a gzip file ??

avatar

7. Response by : The Web Hosting Hero on Sep 28, 2009 at 6:25 am

@ali: have you tried extracting the content locally to your pc to make sure the archive is not corrupted?

avatar

8. Response by : ali on Sep 28, 2009 at 6:33 am

I just loged in ssh and tried on remote machine

gunzip mysql_databases-28-8-2009.sql.gz

gunzip: mysql_databases-28-8-2009.sql.gz: not in gzip format

avatar

9. Response by : The Web Hosting Hero on Oct 2, 2009 at 7:14 am

@ali: What is the size of the file? Is it null?

avatar

10. Response by : ali on Oct 2, 2009 at 7:32 am

I solve it now.

in script I choose backup ALL DB..

that should be no so I create a user and assign it to all DB.

that fixed..

avatar

11. Response by : eric on Nov 25, 2009 at 6:29 pm

I tried to ftp the full bakcup file, form hostmonster to drivehq, but even with time out of 300 it’s still getting time out, what should the problem be..?

avatar

12. Response by : The Web Hosting Hero on Nov 27, 2009 at 8:08 am

Do you have an idea what is the approximate size of your website?

avatar

13. Response by : Tony on Mar 31, 2010 at 9:07 am

Will this back/restore also work if Innodb is place?

avatar

14. Response by : The Web Hosting Hero on Mar 31, 2010 at 9:16 am

@Tony : if there are some foreign key constraints, you’ll have to add this line manually at the beginning of the SQL dump file :

SET FOREIGN_KEY_CHECKS=0;

And also add this line at the very end of the same file :

SET FOREIGN_KEY_CHECKS=1;

Hope this helps.

15. Response by : Bluehost outage results in Twitter love and backup plans | First Impressions on Sep 17, 2010 at 2:44 pm

[...] of you, nerdy but not geeky folks affected by the outage and looking into auto-backup solutions, here is a step-by-step guide on how to set up cron jobs. Even if you haven’t had a website disappearance experience before, it might be a good idea [...]

avatar

16. Response by : The Coupon Mole on Nov 22, 2010 at 7:51 am

I just had a database crash for the first time. Luckily it wasn’t that important and I was able to repair it. But, its sort of like when your hard drive first crashes without a recent backup. You then backup everyday! Thanks, was looking to set this up.

avatar

17. Response by : Arik on May 24, 2011 at 3:15 pm

My site was attacked by hackers, and the whole public_html structure was corupted. My JustHost plan has only weekly backup without historys, and I don’t want to pay more. So I’ve found that fantastic article that finely allowed me to configure backups by my own, without any charge from the host provider.
I’m using it to save weekly backups on a defferent folder on host.
Thanks alot!!!

avatar

18. Response by : Mk1586 on Dec 24, 2011 at 6:25 am

I installed this script and ran it. The cron shows the following errors.

Found = in conditional, should be == at /home/site/public_html/cgi-bin/ftp-backup-script.pl line 96.
tar: Removing leading `/’ from member names
tar: /home/site/dailybackups/site_backup-24-11-2011.tar.gz: file changed as we read it
tar: Removing leading `/’ from member names Cannot login Login incorrect.

Any idea what these error mean exactly?

avatar

19. Response by : Arik on Dec 24, 2011 at 4:40 pm

I’ve got the same error:
tar: Removing leading `/’ from member names
tar: Removing leading `/’ from member names

but it’s work perfectly for me.

avatar

20. Response by : dalinex on Dec 28, 2011 at 8:35 am

This cant be right. There is such an obvious error in the code.

# DELETING FULL SITE BACKUP
if($delete_backup = 1)
{
unlink($full_backup_file);
}

should be

# DELETING FULL SITE BACKUP
if($delete_backup == 1)
{
unlink($full_backup_file);
}

Plus the FTP part doesnt work. I know i have the correct FTP user and password set yet it keeps telling me

tar: Removing leading `/’ from member names Cannot login Login incorrect.

Leave a Comment




Webmaster Resources: Web Hosting Blog
© Copyright 2012 - TheWebHostingHero.com
Follow us on TwitterFacebookSubscribe to our RSS Feed