Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Member
    Join Date
    Apr 2005
    Posts
    7

    Lightbulb Full backup with Cpanel and Cron

    I've found a complete backup solution by search here.
    Only thing: I'm not a server administrator, I only have my WHM and Cpanel accounts.
    So I make file backups with the CRON now.

    Now,
    Is there a way to command line the Cpanel Backup script (wich is perfect), so that this creates a regular backup to an external server?
    What is the code for this?

  2. #2
    Member
    Join Date
    Oct 2006
    Posts
    72

    Default

    The majority of the following code is a script that my host provided me with. However I believe that his ultimate source was this forum but I do not know the author's name.

    I've extended it to copy the resulting tar file to an external FTP site. A couple caveats:

    * It only retains the most recent backup.
    * My added code isn't all that well documented as I didn't originally intend to post it for others.

    I plan to improve the commenting and add the ability to keep the last 5 or so backups but I'm too busy right now.

    PHP Code:
    <?php

    $live 
    false;
    // PHP script to allow periodic cPanel backups automatically.
    // Based on script posted by max.hedroom in cpanel.net forums
    //   This script contains passwords.  KEEP ACCESS TO THIS FILE SECURE!


    // ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********


    // Info required for cPanel access
    $cpuser "username"// Username used to login to CPanel
    $cppass "password"// Password used to login to CPanel
    $domain "domain.com"// Domain name where CPanel is run
    $skin "x"// Set to cPanel skin you use (script won't work if it doesn't match)


    // Info required for FTP host
    $ftpuser "ftpUser"// Username for FTP account
    $ftppass "ftpPass"// Password for FTP account
    $ftphost "ftp.ftpserver.com"// Full hostname or IP address for FTP host
    $ftpmode "ftp"// FTP mode ("ftp" for active, "passiveftp" for passive)


    // Notification information
    $notifyemail "username@domain.com"// 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 0;


    // *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
    function ftp_delfile($ftp_server,$ftp_user_name$ftp_user_pass$fileList){
        
    // set up basic connection
        
    $conn_id ftp_connect($ftp_server);

        
    // login with username and password
        
    $login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);
        foreach (
    $fileList as $file){
            
    // try to delete $file
            
    (ftp_delete($conn_id$file));
        }
        
    // close the connection
        
    ftp_close($conn_id);
    }

    function 
    ftp_getfilename($ftp_server,$ftp_user_name$ftp_user_pass){
    // set up basic connection
    $conn_id ftp_connect($ftp_server);

    // login with username and password
    $login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

    // get contents of the current directory
    if ($login_result){$contents ftp_nlist($conn_id".");}

    // output $contents
    //var_dump($contents);
        
    if ($contents){
            
    $j 0;
            for (
    $i 0$i count($contents); $i++) {
                if (
    eregi ("^backup-[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}_[0-9]{2}-[0-9]{2}-[0-9]{2}_username.tar.gz$"$contents[$i])) {
                    
    $fileList[$j] = $contents[$i];
                    
    $j += 1;
                }
            }
        return 
    $fileList;
        }else{
            echo 
    "False";
        }
    }
    function 
    getfilelist(){
      
    $j 0;
       if (
    $dh opendir("/home2/username/")) {
           while ((
    $file readdir($dh)) !== false) {
            if (
    eregi ("^backup-[0-9]{1,2}.[0-9]{1,2}.[0-9]{4}_[0-9]{2}-[0-9]{2}-[0-9]{2}_username.tar.gz$"$file)) {
                
    $fileList[$j] = "/home2/username/" .$file;
                
    $j += 1;
            }
           }
           
    closedir($dh);
       }
        if (
    $fileList){
            return 
    $fileList;
        }
    }
    $localfiles getfilelist();

    $files ftp_getfilename($ftphost,$ftpuser,$ftppass);

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


    $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&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);

    ftp_delfile($ftphost,$ftpuser,$ftppass,$files);
    foreach (
    $localfiles as $local){
    //    echo "Filename:  " . $local . "<br />";
        
    unlink ($local);
    }
    ?>

  3. #3
    Member
    Join Date
    Apr 2005
    Posts
    7

    Default

    that's great
    thanks so much !

Similar Threads & Tags
Similar threads

  1. Replies: 12
    Last Post: 12-21-2010, 03:47 PM
  2. Full backup cron
    By andrei1015 in forum Data Protection
    Replies: 2
    Last Post: 06-09-2010, 12:04 PM
  3. Generate Full backup to ftp Cron job
    By twyst3d in forum cPanel and WHM Discussions
    Replies: 5
    Last Post: 05-02-2006, 05:13 PM
  4. Cpanel-backup-full backup to home dir
    By Blue Steel in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 03-20-2005, 06:17 AM
  5. Replies: 0
    Last Post: 05-08-2004, 07:04 PM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube