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.

Full backup with Cpanel and Cron

Discussion in 'General Discussion' started by godyn, Feb 22, 2007.

  1. godyn

    godyn Member

    Joined:
    Apr 25, 2005
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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. doni49

    doni49 Well-Known Member

    Joined:
    Oct 4, 2006
    Messages:
    74
    Likes Received:
    0
    Trophy Points:
    6
    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:
    <?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. godyn

    godyn Member

    Joined:
    Apr 25, 2005
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    that's great
    thanks so much !
     
Loading...

Share This Page