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.

Automatic cPanel backup (domain & MySQL) with cron & PHP

Discussion in 'Data Protection' started by tastaram, Mar 19, 2009.

  1. tastaram

    tastaram Registered

    Joined:
    Mar 19, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    OK, so I posed the question, and I found the answer. (Of course I had to find it myself, I just don't have people who regularly read my posts and do research for me!)

    The situation is this: I use cPanel on my web hosting server. I use the cPanel backup tool to regularly backup my home directory (includes my web files, mail, etc), and my MySQL databases. I love the fact that you can use it to backup to a remote FTP server, and I do that on a fairly regular basis.

    The major drawback is that you have to remember/schedule to perform this backup manually. This becomes especially difficult if you have multiple cPanel accounts. It would be amazing if you could just schedule the cPanel backup to run at regular intervals, perhaps with cron. Well, although there's no option for that in cPanel, the script below will allow you to do exactly that!

    This backup script includes SSL support. This isn't necessary if you run the script on the server for which you're generating the backup; but the SSL support is very important if you're running the script somewhere else to connect to your cPanel hosting account.
    <?php

    // PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
    // This script contains passwords. KEEP ACCESS TO THIS FILE SECURE! (place it in your home dir, not /www/)

    // ********* 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 = "example.com"; // Domain name where CPanel is run
    $skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme

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

    // Notification information
    $notifyemail = "you@example.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 *********

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

    ?>
    source:http://deepdictionary.com/
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    If you're looking to improve this script, you can connect to the XML API using the cPanel user credentials over port 2082 or port 2083. Then call the following API1 function:

    HTML:
    <cpanel Fileman="fullbackup($FORM{'dest'},$FORM{'server'},$FORM{'user'},$FORM{'pass'},$FORM{'email'},$FORM{'port'},$FORM{'rdir'})">
    
    If you need help with this API function, let me know. The XML API itself is documented at:

    http://www.cPanel.net/plugins/xmlapi
     
  3. mst3kroqs

    mst3kroqs Registered

    Joined:
    Apr 2, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
  4. Dellow

    Dellow Member

    Joined:
    Feb 27, 2003
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1

    i really need it to save the files on remote server in same files name. (fixed name) or delete yesterday's file.

    any idea?

    Regards,
     
  5. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Setting a fixed name is not possible at this time.

    You may find it easier to automatically delete old files via FTP than using our APIs.
     
  6. jrianto

    jrianto Active Member

    Joined:
    Jun 9, 2008
    Messages:
    44
    Likes Received:
    0
    Trophy Points:
    6
    You can delete yesterday's file through a script that connects to your FTP server. So add a function on the backup script and run it on cron, when it detects there is two backup files residing on the FTP server, simply remove the older one.
     
  7. tyankee

    tyankee Registered

    Joined:
    Apr 9, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    i've looked everywhere for this answer but can't find it.. btw i love this script.. i've been using it for about a year i think and it works great backing up to another server BUT i can't seem to get it to work backing up a domain to it's own server.. the job runs fine, completes with no errors, even lists the file name that it created but i can't find that filename anywhere on the server – not in the home or www directory – not anywhere.. i've tried this now on 4 different websites and i get the same results.. any ideas? or does it just not work if you try to backup on the same server.
     
  8. SunShellNET

    SunShellNET Active Member

    Joined:
    Feb 24, 2009
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    6
    Hi
    You can use cpSync Backup Manager ( free ) for automatic backup everyday if your host supports it.
     
    #8 SunShellNET, Apr 15, 2010
    Last edited: Apr 15, 2010
  9. 3awh

    3awh Member

    Joined:
    Sep 1, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    WWW
    Anyone know if there is another option for auto backups to the same server
    link above is dead
     
  10. gwmbox

    gwmbox Member

    Joined:
    Apr 5, 2003
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Australia
    Can I ask, how can I set it to backup to a specific directory on the FTP site?

    Thanks

    GW
     
  11. MikeHigginbotto

    Joined:
    Jun 13, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    It turns out a few things have changed since the initial script was written to solve this problem. I've written up a fixed version of the script together with a couple of changes to make it easier to use it to backup multiple sites through cPanel and a brief synopsis of how to get the backups to feed through into a local FTP server. The write up is /http://mikehigginbottom.com/content/automatic-cpanel-backups.
     
  12. rahuldas14

    rahuldas14 Member

    Joined:
    Nov 24, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Implemented this updated version. Whats the 2nd site for? will it back up two websites??
     
  13. MikeHigginbotto

    Joined:
    Jun 13, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Correct. The code as written will backup two sites. You can add more sites by adding extra entries to the array like so:

    $sites['2']['domaintobackup'] = "example.com";
    $sites['2']['cpaneluser'] = "user2";
    $sites['2']['cpanelpass'] = "pass2";
    $sites['2']['cpanelskin'] = "x3Bronze";

    and so on.
     
  14. rahuldas14

    rahuldas14 Member

    Joined:
    Nov 24, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    doesnt work...I got a mail which said:

    Could not open input file: /home/youraccount/fullbackup.php

    Can u help me?
     
  15. tomdchi

    tomdchi Well-Known Member

    Joined:
    Feb 24, 2008
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Atlanta, GA
    cPanel Access Level:
    DataCenter Provider
    This is the script I use to backup accounts to another server.
    Requires xmlapi-php available at https://github.com/CpanelInc/xmlapi-php
    This will:
    Backup ALL accounts on the server.
    Use API to get list of accounts.

    Note that the argument in the code below "\\" is the destination ftp directory.


    PHP:
    <?php
    require ('xmlapi.php');

    class 
    cpbackup
    {
        public function 
    backup($source_server_ip$server_password$ftp_domain$ftp_acct$ftp_pass$notify_email)
        {
            
    $xmlapi = new xmlapi($source_server_ip);
            
    $xmlapi->password_auth("root"$server_password);
            
    $xmlapi->set_port('2087');
            
    //$xmlapi->set_debug(1);//this setting will put output into the error log in the directory that you are calling script from 
            
    $xmlapi->set_output('array');
            
    $userlist $xmlapi->listaccts();
            foreach (
    $userlist as $v => $field)
            {
                foreach (
    $field as $cpuser)
                {
                    if (!empty(
    $cpuser['user']))
                    {
                        
    $cpanel_account $cpuser['user'];
                        
    $api_args = array('ftp'$ftp_domain$ftp_acct$ftp_pass$notify_email21"\\");
                        
    $response $xmlapi->api1_query($cpanel_account'Fileman''fullbackup'$api_args);
                        foreach (
    $response as $v2)
                        {
                            
    $result $v2['result'];
                        }
                        if (
    $result == 1)
                        {
                            
    sleep(60);
                        }
                    }
                }
            }
            return 
    true;
        }
    }

    $backup = new cpbackup();
    $result $backup->backup('server ip''server root password''ftp domain''ftp acct''ftp pass''notify email address');


    ?>

    Tom
     
  16. garrettp

    garrettp Well-Known Member
    PartnerNOC

    Joined:
    Jun 18, 2004
    Messages:
    312
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    DataCenter Provider
    Not sure why this script is needed when cPanel has a fully-functional full backup via FTP right in the WHM interface?
     
  17. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
    I do understand the purpose of the original thread, since the original poster only had cPanel access to the machine where automatic backups aren't possible for the end-user to perform. Not certain for the newer script that uses the API and backs up all the accounts on the machine, though.
     
  18. tomdchi

    tomdchi Well-Known Member

    Joined:
    Feb 24, 2008
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Atlanta, GA
    cPanel Access Level:
    DataCenter Provider
    You cannot use the incremental backup option in conjunction with ftp backups through WHM. That is the point of the script-you can have normal incremental backups in WHM and use the script to backup all accounts to another server.
    Also in our setup we want to have backups local and remote.
     
    #18 tomdchi, Jun 24, 2011
    Last edited: Jun 24, 2011
  19. garrettp

    garrettp Well-Known Member
    PartnerNOC

    Joined:
    Jun 18, 2004
    Messages:
    312
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    DataCenter Provider
    I suppose the script would make sense for incremental and full backs in conjunction, but for local and remote it would be significantly more efficient to just have a postcpbackup hook to copy a local backup via FTP/SCP to the remote storage point.
     
  20. FlexiHost

    FlexiHost Member

    Joined:
    Aug 10, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Christchurch, New Zealand
    cPanel Access Level:
    Root Administrator
    One thing that would be nice, is for hosts that use whmcs for their billing systems, to be able to offer some kind of backup solution through there, I have been hunting high and low for one.

    Quite a few users are not that technically up to being able to log into cPanel and create a full backup from there.

    Any thoughts as to how that could be done maybe? Some kind of simple click here to generate Backup and email them a download link to get the files perhaps?
     
Loading...

Share This Page