Easy Automated cPanel BackUp

Hi there,

After attempting a manual backup of 75 sites, i got tired and tried to google an answer to my issue i found this: http://forums.cpanel.net/f49/script-backup-only-sql-only-homedir-139881.html except this meant a cron job, and probably creating a file per domain. for 75 domains i felt too lazy to try that. so I extended it and encapsulated it in a class. Here’s the script I use now:

<?php

// Class that does cPanel backups automatically to a remote FTP server.
// This script will contain passwords.  

/***** KEEP ACCESS TO THIS FILE SECURE! *****/

//(place it in your home dir, not /www/)

class BackUp{

  // Info required for cPanel access
  public $cpuser; // Username used to login to CPanel
  public $cppass; // Password used to login to CPanel
  public $domain; // Domain name where CPanel is run

  public $skin="x3"; // Set to cPanel skin you use (script won't work if it doesn't match).
                     // Most people run the default x3 theme

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

  //in case the domain you need to back up is currently not live (expired or any other reason)
  //specify a domain that is live and is on the same server as the one you are attemting to backup
  //e.g your reseller domain
  public $livedomain="";

  //what email should be notified after the backup is complete?
  public $notifyemail="you@yourdomain.com";

 //what directory should the backup be saved on teh remote server
 //you may need to create this
 public $remotedir="/";

  public function __construct($cpuser,$cppass,$domain,$livedomain="",
                              $skin="", $ftpuser = "", $ftppass = "",
                              $ftphost = "", $ftpmode = "" )
 {
    $this->cpuser = $cpuser;
    $this->cppass = $cppass;
    $this->domain = $domain;
    $this->livedomain = $livedomain;

    //skin,ftppass,ftpuser,ftphost,ftpmode override default when specified via constructor
    if($skin){$this->skin = $skin;}

    if($ftpuser){$this->ftpuser = $ftpuser;}
    if($ftppass){$this->ftppass = $ftppass;}
    if($ftphost){$this->ftphost = $ftphost;}
    if($ftpmode){$this->ftpmode = $ftpmode;}

  }

  function createBackup() {

    $cpuser = $this->cpuser;
    $cppass = $this->cppass;
    //use the live domain if specified
    if($this->livedomain){
      $domain = $this->livedomain;
    } else {
      $domain = $this->domain;
    }
    $skin = $this->skin;

    $ftpuser = $this->ftpuser;
    $ftppass = $this->ftppass;
    $ftphost = $this->ftphost;
    $ftpmode = $this->ftpmode;

    // Notification information
    $notifyemail = $this->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 = 1;

    // *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********

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

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

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

    $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&email_radio=1&port=21&rdir=$remotedir&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");
    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;
    }

    fclose($socket);
  }

  static function runForArray($sites){

    foreach($sites as $tmp_arr){

      $tmp = new BackUp(
        $tmp_arr[0],
        $tmp_arr[1],
        $tmp_arr[2]
      );

      //except for the "$tmp->createBackup()" command,
      //all lines below are just so that we have distinct reports
      echo "<hr />";
      echo "<h1>".$tmp->domain."</h1>";
      echo "<hr />";
      echo '<div style="overflow:hidden; height:300px;">';
      $tmp->createBackup();
      echo "</div>";
      echo '<div style="clear:both; height:20px; background:red;">';
    }
  }
}

?>

Download code: backup.class.php.zip

And this can be called in any of three ways after importing the class file. Use the line below to import the class file:

include 'backup.class.php';

1. Create a new BackUp object and call the backup function thus:

$tmp = new BackUp("cpuser","cppass","domain","livedomain","x3","ftpuser","ftppass","ftphost","ftpmode");

$tmp->createBackup();

Note that except for the first three, you may leave the rest as default and set a generic ftp server to dump your backup files. You can as well set a generic live domain. In case all are under the same reseller account this comes in handy for sites that need backing up but are down for some reason.

2. Create an array and loop through the login details calling backup each time thus:

$sites=array(
  array("site2","passme","site1.com"),
  array("site2","passyou","site2.com"),
  array("site3","one$"."acent","site3.com"),
);

foreach($sites as $tmp_arr){

  $tmp = new BackUp(
    $tmp_arr[0],
    $tmp_arr[1],
    $tmp_arr[2]
  );

  $tmp->createBackup();
}

AND

3. You may just create an array and use the built-in “runForArray” function thus:

$sites=array(
  array("site2","passme","site1.com"),
  array("site2","passyou","site2.com"),
  array("site3","one$"."acent","site3.com"),
);

Backup::runForArray($sites);

And your site backup is complete!