The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Upgraded from reseller to VPS and my backup script stopped working

Discussion in 'Data Protection' started by Tim Sanders, Aug 20, 2015.

  1. Tim Sanders

    Tim Sanders Registered

    Joined:
    Aug 20, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Asheville, NC, USA
    cPanel Access Level:
    Root Administrator
    Using the same hosting company, I upgraded from a reseller plan (no WHM root access) to a VPS plan (with WHM root access.) I had been using two scripts running from cron to perform nightly backups from each of my accounts. The first script I copied very heavily from https://www.namecheap.com/support/knowledgebase/article.aspx/915 . This script creates a full backup and places it in the Backups directory off of Home. This script stopped creating the full backups after I upgraded to a VPS server.

    The second script takes the backup file, renames it to the current day of the month, and ftp's it to a pc in my office. It deletes last months backup of the same day. It renames the first backup of the month with the month name. This way I have a backup from every other day for the past 30 days and a monthly backup for the last year. This script still works if there is a backup file located in the backup directory.

    I can use the same login info etc, and manually go into cpanel and create the backup. I've added several Echo commands through the script and the resultant email shows each echo command. My hosting company's tech support doesn't know why it doesn't work.

    I'm also trying to use the backup in WHM, but I'm getting inconsistent results with it. Over the last few days of trying it, not all of my accounts are backed up and sent to my ftp server. Some are, but not all. And each night, it is a different set that manages to make it through.

    So what would make this not work on a VPS server when it did work on a reseller server?

    Here is the first Cron command and the first script that is not doing it's job.

    Code:
    0 1 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * * php /home/username/fullbackup.php
    (set to run at 1am every odd numbered day)
    fullbackup.php:
    <?php
    echo "start of script";
    ini_set('max_execution_time', 3600);
    
    // PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
    
    // This script contains passwords. It is important to keep access to this file secure (we would ask you to place it in your home directory, not public_html)
    
    // You need create 'backups' folder in your home directory ( or any other folder that you would like to store your backups in ).
    
    // ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
    
    // Information required for cPanel access
    
    $cpuser = "username"; // Username used to login to cPanel
    
    $cppass = "userpassword"; // Password used to login to cPanel. NB! you could face some issues with the "$#&/" chars in the password, so if script does not work, please try to change the password.
    
    $domain = "domain.com";// Your main domain name
    
    $skin = "x3"; // Set to cPanel skin you use (script will not work if it does not match). Most people run the default "x" theme or "x3" theme
    
    // Information required for FTP host
    
    $ftpuser = "username"; // Username for FTP account
    
    $ftppass = "userpassword"; // Password for FTP account NB! you could face some issues with the "$#&/" chars in the password, so if script does not work, please try to change the password.
    
    $ftphost = "ipaddress"; // IP address of your hosting account
    
    $ftpmode = "passiveftp"; // FTP mode
    
    // Notification information
    
    $notifyemail = "myemail@mydomain.com"; // Email address to send results
    
    Echo "stings are set";
    
    // 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;
    
    // *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
    
    $ftpport = "21";
    
    $ftpdir = "/backups"; // Directory where backups stored (make it in your /home/ directory). Or you can change 'backups' to the name of any other folder created for the backups;
    
    // if ($secure) {
    
    // $url = "ssl://".$domain;
    
    // $port = 2083;
    
    // } else {
    
    $url = $domain;
    
    $port = 2082;
    
    // }
    
    echo "check for secure connection";
    
    $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&port=$ftpport&rdir=$ftpdir&submit=Generate Backup";
    
    echo "Set paramaters";
    
    // 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 do not do anything with it.
    
    while (!feof($socket)) {
    
    $response = fgets($socket,4096); if ($debug) echo $response;
    echo "send info to cpanel";
    }
    
    fclose($socket);
    echo "end of script";
    
    ?>
    
    
    Here is the second cron command and script that does work.

    Code:
    0 2 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * * php /home/aimsport/ftpbackup.php
    (Set to run at 2am every odd numbered day)
    
    ftpbackup.php
    <?php
    //get file names of dir backup without dot and dot,dot
    $ws = "directory_under_backups_on_ftp_server";
    $dir = "backups/";
    $a = array_diff(scandir($dir), array('..', '.'));
    
    //Go through each filename and delete all but the first filename that begins with backup
    //Send the first file that begins with backup to the ftpserver
    
    $s=0;
    foreach($a as $fn)
    {
    if ($s==1 or substr($fn, 0, 6)!=="backup")
      {
      unlink("backups/".$fn);
      }
      else
      {
      //Get today's date and create file name
      $d = date("d");
      if ($d=="01")
       {
       $d = date("M");
       }
    
      // connect and login to FTP server
      $ftp_server = "myftpserver.no-ip.org";
      $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
      $login = ftp_login($ftp_conn, "username", "password");
    
      //Delete file from ftp site if there
    
      $fsize = ftp_size($ftp_conn, "backups/".$ws."/".$d.".tar.gz");
      if ($fsize != -1)
       {
       ftp_delete($ftp_conn, "backups/".$ws."/".$d.".tar.gz");
       }
    
      // upload file
      ftp_put($ftp_conn, "backups/".$ws."/".$d.".tar.gz", "backups/".$fn,     FTP_BINARY);
    
      // close connection
      ftp_close($ftp_conn);
    
      //Delete file from site
      unlink("backups/".$fn);
      
      //set flag to 1 indicate file has been sent to ftp
      $s=1;
    }
    }
    ?>
    
    
     
  2. Tim Sanders

    Tim Sanders Registered

    Joined:
    Aug 20, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Asheville, NC, USA
    cPanel Access Level:
    Root Administrator
    Ok, it's amazing how writing something out and going back over it in printed form will make something jump out at you. After writing this all up and posting it, I went back through the scripts as they were posted. It then hit me that on my new VPS server, there is a setting that forces me to use a SSL connection for WHM, CPanel, and Webmail. (Chrome has been fussing at me because I'm using a self-signed certificate on the server.) I went into the first script and uncommented some lines I had earlier commented (5 lines starting with "// if ($secure) {" and ending with "// }") and added a line to set $secure = 1;. This made it work.

    The difference was the requirement to use SSL for Cpanel access on my new server that was not a requirement on my old server.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,762
    Likes Received:
    662
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page