How To Take Full Backup From cPanel By PHP Script

Hello Friends, as a developer i am daily using cPanel and its features and in that features cPanel provide one feature to take full backup or to take backup of any folder from the server but sometimes in some circumstances we have to take daily/weekly/monthly full backup of files by PHP script by running cron job. Yesterday i was searching  for it to do in one of my project and i found this script which are very useful.So below is the script which take full backup from cPanel and make it .zip folder in root folder of your cPanel.

Make one php file and give it to name whatever you want, for this instance i am giving it name details_cp.php, this file is just for details of your cPanel and FTP and other things. So the content of details_cp.php would be given below (Please change all details in  this file with your details):

<?php
$cpuser = “cpanel username”; //cPanel Username
$cppass = “cpanel Password”; //cPanel Username
$domain = “yourdomain.com”; // Domain name you want to take backup
$skin = “x2″; // no need to change

$ftpuser = “FTP username”; // FTP username
$ftppass = “FTP password”; // FTP username
$ftphost = “FTP host”; // FTP Host
$ftpmode = “passiveftp”;
$ftpdir = “/subdir”;

$notifyemail = “webmaster@mydomain.com”; // Email which will use in case of any error.

$delbackup = 1;

$secure = 0; // If you are using secure ssl connection

$debug = 0;
?>

After that you have to create second file, which is then main backup script, which will take backup from cPanel by using  details given in details_cp.php. So for this instance i am giving this file name backup_cp.php and content of backup_cp.php would be :

Note: in backup_cp.php we have included details_cp.php on second line so take care to change that name in backup_cp.php file if you are changing file names.

<?php
include(‘details_cp.php’);
if ($secure) {
$url = “ssl://”.$domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
}

if ($delbackup) {
$conn_id = ftp_connect($ftphost);
if ($conn_id) {
$ok = ftp_login($conn_id, $ftpuser, $ftppass);
if ($ok) {
if (!empty($ftpdir)) {
$ok = ftp_chdir($conn_id,$ftpdir);
}
if ($ok) {
$dirlist = ftp_nlist($conn_id, “backup*”);
if (!empty($dirlist[0])) @ftp_delete($conn_id,$dirlist[0]);
}
}
ftp_close($conn_id);
}
}

$socket = fsockopen($url,$port);
if (!$socket) { echo “Cannot connect to $url\n”; exit; }
$authstr = $cpuser.”:”.$cppass;
$pass = base64_encode($authstr);
$request_data = “dest=”.urlencode($ftpmode).”&email=”.urlencode($notifyemail);
$request_data .= “&server=”.urlencode($ftphost).”&user=”.urlencode($ftpuser);
$request_data .= “&pass=”.urlencode($ftppass);
if (!empty($ftpdir)) $request_data .= “&rdir=”.urlencode($ftpdir);
$request_data .= “&submit=”.urlencode(‘Generate Backup’);

fputs($socket,”POST /frontend/”.$skin.”/backup/dofullbackup.html HTTP/1.1\r\n”);
fputs($socket,”Host: $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($request_data).”\r\n\r\n”.$request_data);

while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}
fclose($socket);
?>

After make this file simply run backup_cp.php in your browser, It will run process for sometime and then it will make .zip folder of backup in root folder of given domain.
Hope this post helps you. If i am missing anything then let me know.

  • Ra2uk

    The script is missing the FTP port value.
    details_cp.php’
    Add
    $ftpport = “21”;

    and add the:
    $request_data .= “&port=”.urlencode($ftpport);

Subscribe to PHP Freelancer

Enter your email address: