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.

Why this script doesnt work on cPanel based servers ?

Discussion in 'cPanel Developers' started by serverack, Dec 29, 2011.

  1. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi,
    I have a script which makes a full backup tar.gz file of our cpanel account.
    Manually we can do this by going to backup option in cpanel.

    Problem is when I run this script on localhost on our office system, it connects to abcuser.com i.e. our cpanel domain, it works I mean it creates a backup file (.tar.gz) in /home/abcuser/

    But when I keep this file on our cpanel server under /home directory, code doesn't work, I mean it doesnt create .tar.gz file.

    Following is that code
    Code:

    Code:
    <?php
    
    $cpanel_username = "abcuser";
    $cpanel_password = "abcpass";
    $cpanel_skin = "x3";
    $cpanel_domain = "abcuser.com";
    $cpanel_notify_email = "abc@example.com";
    $cpanel_counter = 0;
    $cpanel_socket_counter = 0;
    $debug = 1;
    $secure = 0;
    
    //Perform authentication
        
        if ($secure) 
        {
            $url = "ssl://".$cpanel_domain;
            $port = 2083;
            }
        else 
        {
            $url = $cpanel_domain;
            $port = 2082;
            }
        // Open Socket Now
        $socket = fsockopen($url,$port);
        if (!$socket) 
        {
            $email_connection_to_domain = "<p><font color='#993300'>Cannot connect to $url</font></p>";
            echo $email_connection_to_domain;
            $cpanel_socket_counter++;
             }
        else
        {
            $email_connection_to_domain = "<p><font color='#347235'>Connected to $url</font></p>";
            echo $email_connection_to_domain;
            }
    
        // Combination base64
        $authstr = $cpanel_username.":".$cpanel_password;
        $pass = base64_encode($authstr);
        
        // $request_data = "dest=".urlencode("homedir")."&email=".urlencode($cpanel_notifyemail);
        //$request_data .= "&submit=".urlencode('Generate Backup');
        
        $params = "dest=homedir&email=$cpanel_notify_email&submit=Generate Backup"; 
    
        fputs($socket,"POST /frontend/".$cpanel_skin."/backup/dofullbackup.html?".$params."HTTP/1.1\r\n");
        fputs($socket,"Host: $cpanel_domain\r\n");
        fputs($socket,"Authorization: Basic $pass\r\n");
        fputs($socket,"Content-type: application/x-www-form-urlencoded\r\n");
        fputs($socket,"Content-length: ".strlen($params)."\r\n\r\n".$params);
        
        if (!feof($socket)) 
        {
            $response = fgets($socket,4096);
            if ($debug) 
            {
                echo $response;
                echo "check status";
            }
            
        fclose($socket);
    
    ?>
    I request people to run this script on your cpanel account and check whether it works or not.
    Replace username, pass, email, domain
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    672
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Is there any specific reason you are using a custom script to backup accounts, as opposed to using the script provided by cPanel? You can backup accounts via the following command:

    Code:
    # /scripts/pkgacct username
    Thank you.
     
  3. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    My script is for cpanel users and not for admins.
    users cannot login as root
     
  4. cPanelJared

    cPanelJared Technical Analyst
    Staff Member

    Joined:
    Feb 25, 2010
    Messages:
    1,842
    Likes Received:
    18
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Users can generate and back up their own accounts in cPanel, by clicking the Backups link, under the Files heading. The Backup Manager feature needs to be enabled in the feature list that applies to the account (i.e., the feature list that is applied to the package that is applied to the account).

    Users can use this option that is already built into the cPanel interface without the need for a custom script.
     
  5. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
  6. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,
    However following script works,

    Code:
    <?php 
    
    // PHP script to allow 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 = "abcuser"; // Username used to login to CPanel 
    $cppass = "abcpass"; // Password used to login to CPanel 
    $domain = "abcuser.com"; // Domain name where CPanel is run 
    $skin = "x3"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme 
    
    
    // Notification information 
    $notifyemail = "abcuser@abc.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=homedir&email=$notifyemail&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); 
    
    ?>
    
    In my code which I posted earlier, I just have changed variables
     
  7. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Is anybody here ? Developers ? Moderators ?
    I feel like I am nude running on freeway
     
  8. 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
    Would you like this moved to the cPanel Developers area? You didn't post this into the right area if you wanted developers to review your code.
     
  9. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi,
    Please do the needful.

    Thanks
     
  10. 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
    That above script is a bit ugly, there's many things that can cause it to break from future versions of cPanel to settings in current versions such as IP checking, cookie spoofing protection, CSRF protection etc.

    We do have an API 1 function call for generating full backups: ApiFileman < ApiDocs/Api1 < TWiki

    Also, we have a PHP class you can get via cPanel & WHM's SDK which will help you call this API 1 function from outside cPanel&WHM. Once you get things working, this will give you fewer headaches and work for much longer than the above code would.
     
  11. serverack

    serverack Member

    Joined:
    Aug 14, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    There exist very short cpanel dev documentation available.:eek:
    How can I go ahead with PHP ?
    I have made a script which transfers cpanel full backup to amazon s3 automatically, no human interference is required.:)

    In case of amazon, they have explained everything neatly with examples but I am not happy with cpanel's developers documents.:mad:

    BTW your front end example and back end example links were broken, looks like now they are fixed.
     
  12. 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 click on the link "PHP XML-API class" which goes to http://github.com/CpanelInc/xmlapi-php from page cPanel & WHM's SDK - you will see a neatly coded and well commented PHP class to assist you with your development. Our documentation is not specific to Perl, RoR, C#, PHP etc. Instead, since the API itself is in XML and JSON, the code samples tend to be in JSON and XML. You can then translate this information into the language or SDK you intend to use.
     
Loading...

Share This Page