I read this great post today that shows how to automatically backup cPanel with PHP and a cron job and FTP the backup to a remote host. The guy that wrote this recommends that you keep the file on the server you want to backup, but I have like 50 cPanel sites and so I thought it would be better just to keep all of the login info in one file on my laptop and run it with my local version of PHP (set up WAMP or XAMPP to run php locally on windows). I modified the script by adding an array of sites and then the script loops over each set and backs up that site. Pretty awesome way to do this and the neat thing is that it uses functionality that is built into cPanel to do it.
Here’s the code:
<?php
// 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/)
// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
// Info required for cPanel access
// Domain 1
$sites['1']['cpuser'] = "username1"; // Username used to login to CPanel
$sites['1']['cppass'] = "password1"; // Password used to login to CPanel
$sites['1']['domain'] = "domain1.com"; // Domain name where CPanel is run
$sites['1']['skin'] = "x"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme
// Domain 2
$sites['2']['cpuser'] = "username2";
$sites['2']['cppass'] = "password2";
$sites['2']['domain'] = "domain2.com";
$sites['2']['skin'] = "rvlightblue";
// Domain 3
$sites['3']['cpuser'] = "username3";
$sites['3']['cppass'] = "password3";
$sites['3']['domain'] = "domain3.com";
$sites['3']['skin'] = "x3";
// Info required for FTP host
$ftpuser = "username"; // Username for FTP account
$ftppass = "password"; // Password for FTP account
$ftphost = "ftp.domain.com"; // Full hostname or IP address for FTP host
$ftpmode = "scp"; // FTP mode ("ftp" for active, "passiveftp" for passive, or "scp" for scp - most secure)
$ftpdir = ''; // Directory to save the files in
// Notification information
$notifyemail = "youremail@example.com"; // Email address to send results
// Secure or non-secure mode
$secure = 1; // 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;
// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
foreach ($sites as $site) {
if ($secure) {
$url = "ssl://".$site['domain'];
$port = 2083;
} else {
$url = $site['domain'];
$port = 2082;
}
$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection… Bailing out!\n"; exit; }
// Encode authentication string
$authstr = $site['cpuser'].":".$site['cppass'];
$pass = base64_encode($authstr);
$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=&rdir=$ftpdir&submit=Generate Backup";
// Make POST to cPanel
fputs($socket,"POST /frontend/".$site['skin']."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: {$site['domain']}\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");
// Grab response even if we don't do anything with it.
while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}
echo "\nDone with {$site['domain']}\n";
fclose($socket);
}
?>
So I use this straight from the command line on my local computer and plan to set it up with Windows Scheduler to automate this about every 3 to 5 days. I feel a lot better about having a file with all of my cpanel passwords on my computer rather than on a web server. I think some of the variables I set up like scp usage and the ftp directory are newer so if you have an old version of cpanel then it might not support that. Hope this helps someone else to backup cpanel automatically.