cPanel: Auto Backup and Remote Transfer using API + PHP

The good thing about cPanel is you can generate your own backup automatically using cPanel API. In this case, we will use PHP to run on schedule to automatically generate backup and transfer via FTP or SCP to another server. This implementation can be done on user level without need to login into cPanel login page.

To integrate with cPanel API, we need a file xmlapi.php which we can get from cPanel GitHub repository at https://github.com/CpanelInc/xmlapi-php. This post is about creating full backup and transfer the backup to a remote location using FTP automatically with cPanel under user privileges, not root privileges. Variable as below:

cPanel: WHM 11.30.6 (build 6)
cPanel user: mycp123
cPanel password: Pas$r12cP
Domain: mycp123.org
Home directory: /home/mycp123

1. Download the PHP API at here https://github.com/CpanelInc/xmlapi-php/downloads. For me I will download the zip format into my local desktop. Unzip it. Inside the folder got several files and folders. We just need to upload xmlapi.php into public_html folder using FTP client.

2. I am login into cPanel to create PHP script by using File Manager. Go to cPanel > File Manager > Web Root > Go > New File > File Name: cpbackup.php > Create New File.

3. Open back the file in text editor mode by right click on the file (cpbackup.php) and select Code Edit > Edit. It should open cPanel code editor. Copy following lines and save:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
// Must include cPanel API
include "xmlapi.php";
 
// Credentials for cPanel account
$source_server_ip = ""; // Server IP or domain name eg: 212.122.3.77 or cpanel.domain.tld
$cpanel_account = ""; // cPanel username
$cpanel_password = ""; // cPanel password
 
// Credentials for FTP remote site
$ftphost = ""; // FTP host IP or domain name
$ftpacct = ""; // FTP account
$ftppass = ""; // FTP password
$email_notify = ''; // Email address for backup notification
 
$xmlapi = new xmlapi($source_server_ip);
$xmlapi->password_auth($cpanel_account,$cpanel_password);
$xmlapi->set_port('2083');
 
// Delete any other backup before create new backup
$conn_id = ftp_connect($ftphost);
$login_result = ftp_login($conn_id, $ftpacct, $ftppass);
$logs_dir = "/";
 
ftp_chdir($conn_id, $logs_dir);
$files = ftp_nlist($conn_id, ".");
foreach ($files as $file){
    ftp_delete($conn_id, $file);
}
 
ftp_close($conn_id);
 
$api_args = array(
                           'passiveftp',
                           $ftphost,
                           $ftpacct,
                           $ftppass,
                           $email_notify,
                            21,
                            '/'
                         );
 
$xmlapi->set_output('json');
print $xmlapi->api1_query($cpanel_account,'Fileman','fullbackup',$api_args);
 
?>

4. Update the credentials details between line 5 to 15 in the script and save it.

Notes:
The script will check whether any backup exists in destination server and will delete all of them (line 21 to 31). Then, using cPanel API we can create an argument as refer to here http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api1/ApiFileman#Fileman::fullbackup to use passive FTP as transfer mode at line 34 when the backup is ready.

5. We can execute this task manually by accessing the PHP file via browser at http://www.mycp123.org/cpbackup.php. You should see JSON output will be return as below if successful:

{"apiversion":"1","type":"event","module":"Fileman","func":"fullbackup","source":"module","data":{"result":""},"event":{"result":1}}

6. To automate this task, we can simply create cron job to run it weekly. Go to cPanel > Advanced > Cron jobs and use following command:

php -q /home/mycp123/public_html/cpbackup.php

Screen shot as below:

Done! You can create many scripts using cPanel API to automate your repeated task.

8 thoughts on “cPanel: Auto Backup and Remote Transfer using API + PHP

  1. Thanks for sharing this helpful advice. However, shouldn’t the FTP connection be established to the FTP remote site instead of the cPanel account site? If so, $source_server_ip on line 21 should be replaced with $ftphost…?

    Reply

    1. Yes you are right Jason. Pardon me for the mistake in that script. I have made correction into it. Thanks man!

      Reply

  2. When it says “full backup”, is it talking about a cPanel “cpmove” file like the “generate and download a full cPanel backup” would do, or is it one of the other, less-awesome automated backups that cPanel runs?

    I’m actively seeking a way to automatically generate full cPanel backups that can be easily restored at the command line if needed, and this might fit the bill. Just wanted to check first before spending the time setting everything up.

    Thanks for your help and contribution!

    Reply

  3. Thank you for the article. I had been looking for a way to do this in WHM/Cpanel.

    Reply

  4. I try using you script with the latest version of the cpanel xmlapi and got the below error. Any idea what might be the problem?

    Warning: ftp_login() expects parameter 1 to be resource, boolean given in /home/thanhd/fullbackup.php on line 30

    Warning: ftp_chdir() expects parameter 1 to be resource, boolean given in /home/thanhd/fullbackup.php on line 32

    Warning: ftp_nlist() expects parameter 1 to be resource, boolean given in /home/thanhd/fullbackup.php on line 33

    Warning: Invalid argument supplied for foreach() in /home/thanhd/fullbackup.php on line 34

    Warning: ftp_close() expects parameter 1 to be resource, boolean given in /home/thanhd/fullbackup.php on line 47
    {“apiversion”:”1″,”type”:”event”,”module”:”Fileman”,”func”:”fullbackup”,”source”:”module”,”data”:{“result”:””},”event”:{“result”:1}}

    Reply

    1. Your ftp host may be miswritten, please ensure that its on this format: example.com or xxx.xxx.xxx.xxx (<- insert ip here)
      Note the absence of 'http://&#039; and 'www.'

      Good luck!

      Reply

  5. Hello, i have successfully backed up the files, can anyone suggest me how to perform restore with same files to the respective accounts.

    thanks in advance.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *