We help IT Professionals succeed at work.

cpanel ftp backup question

mpaert
mpaert asked
on
Medium Priority
557 Views
Last Modified: 2013-12-24
Is it possible to run 1 command in SSH and have all my cpanel accounts backed up separately to an FTP location?
Comment
Watch Question

Are you on your own setup or through a hosting company?

Author

Commented:
I've got my own setup with ssh access

Commented:
i can give you a php script that you can run by cron and it will do what you asked for.

Author

Commented:
yes please, thatwould be great. So, would i just put the php scrip on my space, and run a cron job (dont' exactly know how to set it up) to run the php file?

Commented:
<?php
  /*
   
  What this script does:
  It will backup all accounts under a cPanel Reseller Account
     
  How it works:
  Reads all accounts (domain and username specifically) from the listaccts page in WHM
  Calls the dofullbackup.html routine of cpanel for each account,
    using the account's username and the reseller's password
  Creates filenames like "backup-mm-dd-yyyy-hh-mm-ss-mydomain_com.tar.gz"
  Saves the backup file to an ftp destination if specified
   
  Caveats:
  This script doesn't require cron, but can be executed from one.
   
  Instructions:
  1. Modify "config" variables below, to suit
  2. From the command prompt:
      >php /www/cgi-bin/Backup_reseller.php
  3. ([Or] Optionally) Set up a cron job to call this script
   
  */
   
  /*** BEGIN CONFIG ***/

  // Reseller Domain (e.g. myDomain.com), UserID and Password
  $whmDomain  = '70.83.64.106'; //your reseller domain or ip
  $whmUser    = 'user'; //your web host manager user name
  $whmPass    = 'pass'; //your web host manager password

  // Destination; Same as offered within cPanel; Choose your poison...
  //  $dest = 'homedir'; //Home Directory
  //  $dest = 'ftp'; //Remote FTP Server
  $dest = 'passiveftp'; //Remote FTP Server (Passive mode transfer)
 
  // Skin used in cPanel
  $skin = 'monsoon'; //cpanel skin in use

  // Email to send completion notification eMail
  $email = 'blah@blah.net';

  // Backup FTP Domain (e.g. myDomain.com), UserID and Password  
  $server = 'backup15.gnax.net'; // Remote Server (FTP/SCP only)
  $user = 'user2'; // Remote User (FTP/SCP only)  
  $pass = 'pass2'; // Remote Password (FTP/SCP only)

  // Be kind to your kindred...  Take a nap!
  $sleep = 90; // How many seconds to pause between each account
   
  // BackUp the Reseller Domain too?
  $bkupReseller = false; // Set to 'true' to backup the reseller's account,
                        //       'false' to skip the reseller's account
     
  /*** END CONFIG ***/

   
  set_time_limit(0);
  echo ">> Reseller Full Backup <<\n";
   
  // Base64 Encode Reseller UserID & Password
  $authstr = "$whmUser:$whmPass";
  $whmUidPass = base64_encode($authstr);
           
   
  // Get the WHM 'list accounts' page
  $socket = fsockopen($whmDomain,2086,$errno,$errstr);
  if (!$socket) {
     echo "fsockopen Error to WHM: $errstr ($errno)<br />\n";
     die();
  } else {
    fputs($socket,"GET /scripts2/listaccts?viewall=1\r\n");
    fputs($socket,"HTTPS/1.0\r\n");
    fputs($socket,"Host: $whmDomain\r\n");
    fputs($socket,"Authorization: Basic $whmUidPass \r\n");
    fputs($socket,"Connection: close\r\n\r\n");
     
    $page = '';
    while (!feof($socket)) {
      $page .= fgets($socket,4096);
    }
    fclose($socket);
  }
   
  // Get each account's table row
  $int2 = preg_match_all("/<tr class=(?:tdshade2|tdshade1)>(.*?)<\/tr>/is", $page, $matches);

  if($int2 > 0 && is_array($matches[1])) {
      foreach($matches[1] as $match) {        
          $account = explode('</td><td>',$match);
          $account[0] = strip_tags(trim($account[0])); //domain
          $account[2] = strip_tags(trim($account[2])); //username
       
          // Base64 Encode Account UserID w/ Reseller Password
          $authstr = "$account[2]:$whmPass";
          $cPanelUidPass = base64_encode($authstr);
           
          // Kick off the cPanel Backup for the Account...
          $socket = fsockopen($account[0],2082,$errno,$errstr);
          if (!$socket) {
             echo "fsockopen Error: $errstr ($errno)\n";
          } else {
            echo "Processing client account: $account[0] \n";
            fputs($socket,"GET /frontend/$skin/backup/dofullbackup.html?dest=$dest&email=$email&server=$server&user=$user&pass=$pass\r\n");
            fputs($socket,"HTTPS/1.0\r\n");
            fputs($socket,"Host: $account[0] \r\n");
            fputs($socket,"Authorization: Basic $cPanelUidPass \r\n");
            fputs($socket,"Connection: close\r\n\r\n");
             
            while (!feof($socket)) {
              fgets($socket,4096);
            }
            fclose($socket);
          }
          sleep($sleep);
        }
  }
   
  // Now do yourself if desired...
  if($bkupReseller){
    // Kick off the cPanel Backup for the Reseller Account...
    $socket = fsockopen($whmDomain,2082,$errno,$errstr);
    if (!$socket) {
       echo "fsockopen Error: $errstr ($errno)\n";
    } else {
      echo "Processing reseller account: $whmDomain \n";
      fputs($socket,"GET /frontend/$skin/backup/dofullbackup.html?dest=$dest&email=$email&server=$server&user=$user&pass=$pass\r\n");
      fputs($socket,"HTTPS/1.0\r\n");
      fputs($socket,"Host: $whmDomain \r\n");
      fputs($socket,"Authorization: Basic $whmUidPass \r\n");
      fputs($socket,"Connection: close\r\n\r\n");
       
      while (!feof($socket)) {
        fgets($socket,4096);
      }
      fclose($socket);
    }
  }
  echo ">> Le Fini <<\n\n";
?>
Jeffrey Kane - TechSoEasyPrincipal Consultant
CERTIFIED EXPERT
Most Valuable Expert 2016
Top Expert 2014

Commented:
mpaert,

What you are asking for is available in CPanel's WebHostManager.  You state, "I've got my own setup with ssh access", but do you have access to WHM??  Because it's quite easy to configure this through WHM.

Whoever your hosting provider is would just need to designate you as a reseller in order to provide WHM access to your accounts.

Jeff
TechSoEasy
Jeffrey Kane - TechSoEasyPrincipal Consultant
CERTIFIED EXPERT
Most Valuable Expert 2016
Top Expert 2014

Commented:
Sorry, but that script is not necessary.  It requires that he has access to WHM, which if he does, then all he needs to do is configure the ftp backup within WHM.

Jeff
TechSoEasy
Commented:
PAQed with no points refunded (of 500)

GranMod
Community Support Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.