HTTP/1.1 // 38.107.191.86 // // 47642 // // CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

ThinkGeek - Cool Stuff for Geeks and Technophiles

Automatic cPanel Backup

Dec
18
2008

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.


9 Comments »

Allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
MyAvatars 0.2

Comment by Web Development Manila
on 13 Feb 2009 at 8:18 pm #

nice post, thanks for sharing!

 
MyAvatars 0.2

Pingback by Sauvegarde automatique de son site » In Mulot Veritas
on 19 Feb 2009 at 12:49 pm # Subscribed to comments via email

[...] adaptation (à lire en anglais) à ce script existe si vous avez plusieurs sites à sauvegarder ainsi [...]

 
MyAvatars 0.2

Comment by Justin
on 11 Sep 2009 at 9:49 am #

nice, will definately give this a try rather than paying for CP Site Saver…. Cheers!!

 

[...] moet installeren (eventueel met xampp). Dit script komt overigens flink in de buurt met wat ik wil: http://www.devtrench.com/automatic-cpanel-backup/ Alvast [...]

 
MyAvatars 0.2

Comment by Warwick
on 31 Dec 2009 at 5:56 am # Subscribed to comments via email

Great Script, works well a few small bugs but saves heaps of time.

Bug 1 = If a sites password has symbols (or certain symbols anyway) you get the following error when in debug mode and the backup fails

Sorry, all fields are required when using FTP or scp.

I don’t have a fix for this as yet

Bug 2 = Improve reliability when backing up lots of sites lots of sites
I added the following lines
#2 set_time_limit(600);
#55 sleep(5);
#62 sleep(5);
#64 sleep(5);
#79 sleep(5);

 
MyAvatars 0.2

Comment by Suni
on 09 Jun 2010 at 11:26 am #

This is a good choice for small account . But for bigger accounts the remote ftp backup solution is not good. Because it will take good resource to gzip the file. Here you can see a cPanel remote incremental backup pluggin which will help you to manage incremental backups remotely . See here http://www.sherin.co.in/cpremote/

 
MyAvatars 0.2

Comment by BALAGURU
on 23 Jun 2010 at 5:26 am #

hello,
This script is working fine.I am getting backup to my root directory..
IF I WANT TO GET BACKUP DIRECTLY TO MY LOCAL PC.WHAT CAN I DO?

PLS SUGGEST ME A SOLUTIONS.THANKS IN ADVANCE

 
MyAvatars 0.2

Comment by James Ehly
on 23 Jun 2010 at 9:44 am #

you’ll want to research tools like rsync, scp or WinSCP’s scripting feature. cPanel doesn’t have a feature like this – it can only save to your root directory or send to an FTP server. So you have 2 options: 1) you can set up your local machine to be an ftp server, or 2) you can use one of the tools above to write a script that logs in periodically and downloads your backup.

If you only need files, then you can use rsync to do all of this, but if you want the backup that cPanel generates, then the above 2 methods are all I can think of at the moment.

 
MyAvatars 0.2

Comment by Will Ashworth
on 09 Aug 2010 at 1:27 pm # Subscribed to comments via email

Great script. I simplified the arrays a little so I don’t have to add the initial array key (ie., 1, 2, 3) every time I add another website. That’s pretty much the only thing I changed.http://pastebin.com/VjAipQ11