PHP Code, SQL Code, Web

Automatic cPanel backup (domain & MySQL) with cron & PHP

OK, so I posed the question, and I found the answer. (Of course I had to find it myself, I just don't have people who regularly read my posts and do research for me!)

The situation is this: I use cPanel on my web hosting server. I use the cPanel backup tool to regularly backup my home directory (includes my web files, mail, etc), and my MySQL databases. I love the fact that you can use it to backup to a remote FTP server, and I do that on a fairly regular basis.

The major drawback is that you have to remember/schedule to perform this backup manually. This becomes especially difficult if you have multiple cPanel accounts. It would be amazing if you could just schedule the cPanel backup to run at regular intervals, perhaps with cron. Well, although there's no option for that in cPanel, the script below will allow you to do exactly that!

This backup script includes SSL support. This isn't necessary if you run the script on the server for which you're generating the backup; but the SSL support is very important if you're running the script somewhere else to connect to your cPanel hosting account.


// PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
// This script contains passwords.  KEEP ACCESS TO THIS FILE SECURE! (place it in your home dir, not /www/)


// Info required for cPanel access
$cpuser = "username"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = ""; // Domain name where CPanel is run
$skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme

// Info required for FTP host
$ftpuser = "ftpusername"; // Username for FTP account
$ftppass = "ftppassword"; // Password for FTP account
$ftphost = ""; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = ""; // Email address to send results

// Secure or non-secure mode
$secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 0;


if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
  $response = fgets($socket,4096);
  if ($debug) echo $response;



To schedule the script to run regularly, save it as fullbackup.php in your top directory (not /public_html, which would be less secure), and enter a new cron job like the following:

15 2 * * * /usr/local/bin/php /home/youraccount/fullbackup.php 

(Runs every night at 2:15 a.m.)


15 2 * * 1 /usr/local/bin/php /home/youraccount/fullbackup.php 

(Runs every Sunday night at 2:15 a.m.)



  1. Automatic cPanel Backups

    […] the script I use to do this. It's based on work initially done by Justin Cook back in 2006 but cPanel has changed a bit since then and I've had to tweak it a little to […]

  2. I know an it's old script, but I thought I would share how to adapt this script to work with the current cpanel version.

    Mandatory ftp port and remote dir during post has broken the script.
    Also cpanel have introduced session tokens.

    A few new lines and minor modification of the post gets this beaut of a script up an running again.

  3. Automatic cPanel Backup via Curl&Cron

    […] Cook wrote an article how to backup your cPanel stuff with PHP&Cron. Well, the PHP part didn't really work for me, so I had to create an alternative […]

  4. domain registration and web hosting | How to make automatic backup in cPanel?

    […] The script is taken from *here. Quick Nav […]

  5. RockOnHost - Portal Home

    […] Here is an easy DIY way to back up your whole site with cPanel. […]

  6. Can someone post a updated version of this script ? i really need something like this to create automatic backups.

  7. Κατασκευή ιστοσελίδων

    Is there any way to make backups from all accounts of a reseller account?

  8. I would like to automatically backup selected accounts using cPanel and then download the resulting .tar.gz file to my local hard drive. I am presuming that I should use a cron job. Please would you advise me what command I should use in the cron job setting (I can probably manage the time/frequency element and what code/script I should load and where I should load it. I am a novice so please help.

  9. Schedule Cpanel Backup – System Toy

    […] Justin Cook's Blog – Automatic cPanel backup … – I use the cPanel backup tool to regularly backup my home directory … It would be amazing if you could just schedule the cPanel backup to run at regular intervals, … […]

  10. Hi, It is helpful, But I have a concern with database backup which is related to the website. For example:
    If I have a WordPress website hosted on the CPanel. In that case, I can backup all files from File Manager, but I am not able to the backup database. I am not sure that I am asking you right query or it's just a stupid question.

Leave a Reply

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

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image