Community Forums
Connect with us on LinkedIn
Community Notice
+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 28
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

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

    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. #2
    Technical Product Specialist cPanelDavidG's Avatar
    Join Date
    Nov 2006
    Location
    Houston, TX
    Posts
    10,720
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    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 Code:
    <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. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Default Attribution

    I thought it would also be nice to post the likely actual source for this information:

    http://www.justin-cook.com/wp/2006/1...with-cron-php/

  4. #4
    Member
    Join Date
    Feb 2003
    Posts
    5

    Default

    Quote Originally Posted by cPanelDavidG View Post
    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 Code:
    <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:

    XML and JSON APIs

    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. #5
    Technical Product Specialist cPanelDavidG's Avatar
    Join Date
    Nov 2006
    Location
    Houston, TX
    Posts
    10,720
    cPanel/Enkompass Access Level

    Root Administrator

    Default

    Quote Originally Posted by Dellow View Post
    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,
    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. #6
    Member
    Join Date
    Jun 2008
    Posts
    32

    Default

    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. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Default

    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. #8
    Member
    Join Date
    Feb 2009
    Posts
    33

    Default

    Hi
    You can use cpSync Backup Manager ( free ) for automatic backup everyday if your host supports it.
    Last edited by SunShellNET; 04-15-2010 at 10:16 AM.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    WWW
    Posts
    1

    Default

    Quote Originally Posted by SunShellNET View Post
    Hi
    You can use cpSync Backup Manager ( free ) for automatic backup everyday if your host supports it.
    Anyone know if there is another option for auto backups to the same server
    link above is dead

  10. #10
    Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    7

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    Can I ask, how can I set it to backup to a specific directory on the FTP site?

    Thanks

    GW

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    6

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    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. #12
    Member
    Join Date
    Nov 2009
    Posts
    7

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    Implemented this updated version. Whats the 2nd site for? will it back up two websites??

  13. #13
    Member
    Join Date
    Jun 2011
    Posts
    6

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    Quote Originally Posted by rahuldas14 View Post
    Implemented this updated version. Whats the 2nd site for? will it back up two websites??
    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. #14
    Member
    Join Date
    Nov 2009
    Posts
    7

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    doesnt work...I got a mail which said:

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

    Can u help me?

  15. #15
    Member
    Join Date
    Feb 2008
    Location
    Atlanta, GA
    Posts
    74
    cPanel/Enkompass Access Level

    DataCenter Provider

    Default Re: Automatic cPanel backup (domain & MySQL) with cron & PHP

    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 Code:
    <?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

Similar Threads & Tags
Similar threads

  1. Exim & PHP & Domains & Ip's & /etc/mailips
    By NemoXP in forum E-mail Discussions
    Replies: 2
    Last Post: 05-13-2010, 01:37 AM
  2. Exim & PHP & Domains & Ip's & /etc/mailips
    By NemoXP in forum New User Questions
    Replies: 0
    Last Post: 05-12-2010, 09:41 AM
  3. Replies: 2
    Last Post: 07-04-2007, 09:12 AM
  4. Replies: 12
    Last Post: 08-16-2006, 11:56 PM
  5. Mysql 4 & php & horde problem
    By vincento in forum cPanel and WHM Discussions
    Replies: 0
    Last Post: 06-10-2003, 08:52 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube