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 remote FTP backup script?

Discussion in 'cPanel Developers' started by frostino, Mar 8, 2011.

  1. frostino

    frostino Member

    Joined:
    Mar 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    I'm in a shared hosting environment that runs cPanel Pro 1.0 (RC1). I have been trying to find a way to run a daily backup. I understand how to set up a cron job but I can't get the script to work. I found a few scripts online and none work when I plug in my values. Can someone help me fix this? I want to backup and FTP the backup file to another server. I can do this successfully in the backup wizard but that means I have to do it manually.

    When I schedule this script to run, it appears to successfully log in to the second server but it doesn't write the file.

    Any ideas on what I may have done wrong?

    Code:
    ###
    # cPanel Backup via Cron v1.2.1
    ###
    
    ###
    #  Instructions
    ###
    # Make sure CP_SCP_BACKUP_ROOT directory exists. It is "web_backup" by default.
    # Within this directory you'll need a directory for each cPanel account you're
    # backing up.
    #
    # I.e. if you're backing up "example.com" and "foobar.com" and you want them
    # to be backed up in the default location of "web_backup", you'll need to
    # execute the following commands at the recieving end:
    # mkdir ~/web_backup
    # mkdir ~/web_backup/example.com
    # mkdir ~/web_backup/foobar.com
    ###
    
    
    ###
    # Config begins
    ### 
    
    #cPanel info -- host that is being backed up
    CP_USERNAME=-snip-
    CP_PASSWORD=-snip-
    CP_DOMAIN=-snip-
    CP_SKIN=x3 # plain "x" is probably the most common one
    
    #SCP/FTP info -- host to receive the backup file
    CP_SCP_USER=myusername
    CP_SCP_PASS=*********
    CP_SCP_HOST=**********.******.***
    CP_SCP_PORT=5005
    
    # see cPanel backup page for alternatives of transfer mode
    CP_SCP_MODE=ftp
    
    # email address for sending notification after backup is completed
    CP_EMAIL=**********@gmail.com
    
    # target directory on remote host; do not include domain names in this one
    CP_SCP_BACKUP_ROOT=myfoldername%2F  # url encoded, e.g. "/" equals "%2F"
    
    ###
    # Config ends
    ### 
    
    
    
    # creates the target path (directory and filename)
    # if you make changes, remember to url encode slashes, i.e. "/" equals "%2F"
    # if --year option is used, script creates a yearly filename, otherwise a monthly one
    if [ "$1" = '--year' ]; then
    	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$$CP_USERNAME-year-`date +%Y`.tar.gz
    else
    	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$$CP_USERNAME-month-`date +%m`.tar.gz
    fi
    
    # initiate backup (the following command should be on one line)
    curl --silent --insecure --user $CP_USERNAME:$CP_PASSWORD -d "dest=$CP_SCP_MODE&email=$CP_EMAIL&server=$CP_SCP_HOST&user=$CP_SCP_USER&pass=$CP_SCP_PASS&port=$CP_SCP_PORT&rdir=$CP_SCP_RDIR" https://$CP_DOMAIN:2083/frontend/$CP_SKIN/backup/dofullbackup.html > /dev/null
    
    # gets curl return code
    CURL_EXIT=$?
    
    # curl encountered an error
    if [ $CURL_EXIT -gt 0 ]; then
    	echo curl returned exit status $CURL_EXIT - see curl manual pages for more details
    	exit 69
    fi
    
    # everything ok
    exit 0
    
     
    #1 frostino, Mar 8, 2011
    Last edited by a moderator: Aug 1, 2012
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi frostino,

    Have you tried using the API. Specifically, Fileman::fullbackup, which is what that html page uses (If I remember correctly). It would essentially be the same as what you have in your example. One difference is the html layer: the API doesn't have that so it's one less variable in the problem. Additionally, we support the API but do not directly support programming against the UI.

    As a side note, cPanel Pro version is not something that should affect this, nor is it an adequate gauge for the version of cPanel you're running. If you have a standard X3 theme from your host, you can expand the statsbar on the left side of you cPanel interface: in there you should have the cPanel Version listed.

    If it were me, I'd schedule a cron task that uses the XML-API PHP client class. It will take care of all the cURL headers and the URL creation.

    Here's a sample script:
    PHP:
    # Add a cron task like "php -r /home/dave/scripts/mybackupscript.php"

    # mybackupscript.php
    <?php
    include "/home/dave/scripts/xmlapi.php";

    $ip "XXX.XXX.XXX.XXX";
    $cp_user 'USER';
    $cp_pass 'PASSWORD!';
    $confirm_email_addy 'user@mydomain.tld';

    $remote_server 'XXX.XXX.XXX.XXX';
    $remote_user 'USER';
    $remote_pass 'PASSWORD';
    $remote_port 21;
    $remote_dir '/';

    // Instantiate object
    $xmlapi = new xmlapi($ip);

    // Set to use secure cPanel port
    //  you will have to alter this to 2082 if your PHP doesn't have SSL support enabled
    $xmlapi->set_port('2083');

    // Set cPanel credentials
    $xmlapi->password_auth($cp_user$cp_pass);

    // Uncommentfor debugging
    //$xmlapi->set_debug(1);

    // Setup an ordinal array with arguments in proper order
    $args = array(
        
    'ftp',                  // Destination type
        
    $remote_server,         // Remote destination address/domain
        
    $remote_user,           // Destination's FTP user login
        
    $remote_pass,           // Destination's FTP password login
        
    $confirm_email_addy,    // Email address to send notification
        
    $remote_port,           // Listening FTP port on remote destination
        
    $remote_dir,            // Directory to store backup on remote destination
    );

    // Make API1 query
    $results $xmlapi->api1_query($cp_user'Fileman''fullbackup'$args);

    // You can do something with $result, but most likely the result status will be
    // inadequate to determine if the backup and the transfer happened.  The
    // email should help with that ;)
    ?>
    That will make a cURL query with the following url and postdata:
    Code:
    URL: https://10.1.1.1:2083/xml-api/cpanel
    DATA: cpanel_xmlapi_user=dave&cpanel_xmlapi_module=Fileman&cpanel_xmlapi_func=fullbackup&cpanel_xmlapi_apiversion=1&arg-0=ftp&arg-1=10.1.1.2&arg-2=USER&arg-3=PASSWORD&arg-4=dave%40mydomain.tld&arg-5=21&arg-6=%2F
    

    But if you don't want to use the XML-API PHP client class, that's understandable. You should be able to refashion you script to use API1 Fileman::fullbackup, simply make your cURL URL similar to the one I posted.

    Best Regards,
    -DavidN
     
    #2 cPanelDavidN, Mar 9, 2011
    Last edited by a moderator: Aug 1, 2012
  3. frostino

    frostino Member

    Joined:
    Mar 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    I'll try to use what you posted, thanks.

    My version of cPanel is 11.28.83.

    Do I just download and extract that file linked above the code you posted and include the xmlapi.php file and then plug in the values for the variables?

    I'll try that first and check back here later.

    Thanks!
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Yep, it should be as easy as that. You need the base PHP class ("xmlapi.php") and the sample script ("mybackupscript.php"). In "mybackupscript.php" change the path value for the PHP class include function, and then the values for those top nine variables.

    Cheers,
    -DavidN
     
  5. frostino

    frostino Member

    Joined:
    Mar 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    So I created the backup.php file and included the xmlapi.php. Plugged in my values. Then I went to the Cron Jobs feature in cPanel and in the command line, entered:

    php -r /home/xxxxxxxx/backup.php

    This morning I received an email with the message below. I only used -r because that's what was commented in the script.

     
  6. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Frostino,

    My apologies, the flag you want is "-f" for file (not "-r", which is used for inline CLI use). It was a typo on my part.

    Regards,
    -DavidN
     
  7. frostino

    frostino Member

    Joined:
    Mar 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Thanks, it works. I thought it wasn't working for most of the day but turns out the confirmation email was very delayed and by the time I got it, about 10 backups were uploaded (i set the cron job to run every half hour so I could make changes through the day and not have to wait forever for it to run).

    I'm a happy camper, now.
     
  8. doobiedar

    doobiedar Registered

    Joined:
    Aug 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi Guys,

    I'm trying to set up this script but i'm getting the following:
    # apiBackup.php (the name of my backup script)
    <br />
    <b>Fatal error</b>: Uncaught exception 'Exception' with message 'non integer or negative integer passed to set_port' in /home/USERNAME/scripts/xmlapi.php:298
    Stack trace:
    #0 /home/USERNAME/scripts/apiBackup.php(24): xmlapi-&gt;set_port('2082')
    #1 {main}
    thrown in <b>/home/USERNAME/scripts/xmlapi.php</b> on line <b>298</b><br />

    I had to download the xmlapi.php from here http://sdk.cpanel.net/utils/xmlapi_php_v1.0.tar.gz because the link originally posted isn't available.

    Here's the code in question from xmlapi.php

    /**
    * Set the port to connect to
    *
    * This will allow a user to define which port needs to be connected to.
    * The default port set within the class is 2087 (WHM-SSL) however other ports are optional
    * this function will automatically set the protocol to http if the port is equal to:
    * - 2082
    * - 2086
    * - 2095
    * - 80
    * @param int $port the port to connect to
    * @see set_protocol()
    * @see get_port()
    */
    public function set_port( $port ) {
    if ( !is_int( $port ) || $port < 1 ) {
    throw new Exception('non integer or negative integer passed to set_port');
    }

    // Account for ports that are non-ssl
    if ( $port == '2086' || $port == '2082' || $port == '80' || $port == '2095' ) {
    $this->set_protocol('http');
    }

    $this->port = $port;
    }


    Can anyone provide any assistance?
     
  9. doobiedar

    doobiedar Registered

    Joined:
    Aug 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi Guys,

    I just found that:
    $xmlapi->set_port('2083');
    should be
    $xmlapi->set_port(2083);

    and the xmlapi package is here (link above is dead): http://sdk.cpanel.net/utils/xmlapi_php_v1.0.tar.gz

    My new question is how do you set a retention for backups? As in, I only want to keep say 3 backups on my FTP server.

    Also, is there a way of getting some sort of log in the email?
     
    #9 doobiedar, Aug 7, 2011
    Last edited: Aug 7, 2011
  10. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  11. doobiedar

    doobiedar Registered

    Joined:
    Aug 7, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Does anyone know how to delete files on the remote server? Such as, the next time the backup runs if it finds anything in the remote directory it deletes it?
     
Loading...

Share This Page