Howto: Automatic cPanel Backups

Intro: As many web devs know, the web hosting panel, cPanel, has quite a powerful “full backup” feature. The only problem is that if you want a full backup, you have to log into cPanel, navigate to the backup page, click the backup button, wait for the backup to finish, and then download it. If you’re like me, and would like to make sure I have frequent backups of everything, making backups can get annoying.

The following is a short 5 minute tutorial to set up your cPanel to automatically create backups and store them (for free) in another remote location via FTP using a PHP script and cPanel’s cron jobs.

Requirements:
1. Your cPanel login information
2. (Optional) A remote FTP location to store backups

Step 0 (Optional) – Get a FTP server to store backups:
Getting a remote place to store your backups is important. I highly recommend you check out Box (similar to Dropbox), which has a FTP uploading feature. For a list of free web hosts, click here (Make sure you read their AUP). Any server/web host will do, as long as you can FTP into them.

Step 1 – Get the backup script:
Visit this page to generate the PHP script that will run the backups. Fill out your cPanel information as well as your FTP server info, if you are using one. After you hit the submit button, copy the generated PHP code and save it as backup.php. Put the backup.php file into your cPanel Home directory (Go into your file manager and click “Home”)

Step 2 – Set up cron:
Go to your cPanel homepage and press the “Cron jobs” link. In the “Add New Cron Job” section, select from “Common Options” how often you want to create a backup (or customize your own interval). I suggest once a day. You can choose once a week if your website is static or doesn’t change much. In the “Command” field, type php -f ~/backup.php. Press the add button and you’re done!

That’s it! Pretty simple and effective way to make sure you always have backups. It’s even possible to upload to two different FTP servers, just create two backup scripts and cron jobs. Make sure you log into your remote backup FTP server to download and remove old backups.

Bonus: Here’s a script you can put onto your remote server (in the same directory as your backups) that will automatically delete backups older than 7 days to save storage space. Add it as a cron job and you’ll be set for worry-free backups!

38
Leave a Reply

32 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
33 Comment authors
ali

can u please fix the script ?
its not working on new HWM update

Adding an $email = “you@yourdomain.com” variable to the top of the php script and inserting &email=$email into the $params line will generate a confirmation email as well.

Joseba

I tried the email stuff but with not success.

Thank you Eric. You just saved me an hour writing a script to do this.

John Pignone Reed

How can I have the script also backup my SQL databases??? THANK YOU!!!!

works great….

but how can i do cronjob for bonus script …
how can i delete backups older 7 days from ftp server?

Bruno Riggs

Hi,

1. put the home folder
2. added the cron php -f ~ / backup.php

Returns the error:

HTTP / 1.1 401 Access Denied
Set-Cookie: cprelogin = no; HttpOnly; path = /; port = 2082
Set-Cookie:cpsession=%3avM2FectrBfFAJgNgwmpLP5wmiUQA5LiqzRyfnm7xdy3EA7Tr0vtbeJfcgrDJaJ35%2cf2b6e4f3e75e3f43b45eb3625aab0df52dd4f481e22146d7f702e74450c4163b; HttpOnly; path = /; port = 2082
Server: cpsrvd / 11.44.3.4
Content-type: text / html; charset = “utf-8”
Connection: close
Date: Mon, 11 May 2015 19:34:03 GMT
Content-Length: 18151

Cody

Very helpful. Thank you for the article. Is there a way to exclude files and folders? Or only include specific folders? The full backup is grabbing everything and my backup files created are huge :S

Brandon

Hello,

I have this script working for 7 of my clients on hostgator. However, when I try to implement on GoDaddy, it doesn’t work. The email content that I receive from Chron Jobs is exactly the same, so it appears to work – but no backup is created.

This is what I get back from Chron Jobs

HTTP/1.1 301 Moved
Server: cpsrvd/11.46.1.3
Connection: close
Content-length: 137
Location: /frontend/x3/backup/dofullbackup.html?submit=Generate
Content-type: text/html; charset=”utf-8″

Greg

Hi,

I am getting the same problem, worked on Saturday now not working at all :-S

Has CPanel changed somthing?

Greg

Brandon,

I worked mine out, when I tested my script I used basic passwords for both the Cpanel and FTP passwords, seemingly it doesn’t like any “$#&/” chars in the passwords. Once I changed my password everything seems to be working okay.

Just thought I better give you the heads up in case you where experiencing the same issue?

Cheers
Greg