Automatic cPanel backups with PHP

As of late, I’ve gotten more and more into practicing redundancy when it comes to my web applications. This little bit of code can be extremely helpful if you’ve got your website on one server and wish to back it up to another via FTP.


We’re going to make a .PHP file in the home of our hosting account. e.g. [home/yourusername/backup.php]


This script does support SSL which I strongly recommend if you’re going to be using it as I do for FTP backups to a remote server.
$cpaneluser = 'user'; // cPanel username
$cpaneluserpass = 'pass'; // cPanel password
$theme = 'paper_lantern'; // Must match current selected cPanel theme ('paper_lantern' in the majority of cases, 'x3' is possible as well)
$ftp = true; // If it's false the backup will be stored in user's home directory, otherwise it will be uploaded via FTP to some custom location
$ftpserver = ''; // Must be localhost for current server or custom hostname for remote FTP upload
$ftpusername = 'user'; // cPanel/SFTP username. Should be the same as cPanel username for local upload or custom for remote upload
$ftppassword = 'pass'; // cPanel/SFTP password. Should be the same as cPanel password for local upload or custom for remote upload
$ftpport = '21098'; // SFTP port. Should be 21 in most cases.
$ftpdirectory = '/home/user/backups'; // Directory on FTP server to store backups. MUST EXIST BEFORE BACKUP OR BACKUP PROCESS WILL FAIL
// Do not edit below this line
$domain = 'localhost';
$secure = true;
$auth = base64_encode($cpaneluser . ":" . $cpaneluserpass);
if ($secure) {
$url = "ssl://" . $domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
$socket = fsockopen('localhost', 2082);
if (!$socket) {
exit("Failed to open socket connection.");
if ($ftp) {
$params = "dest=scp&server=$ftpserver&user=$ftpusername&pass=$ftppassword&port=$ftpport&rdir=$ftpdirectory&submit=Generate Backup";
} else {
$params = "submit=Generate Backup";
fputs($socket, "POST /frontend/" . $theme . "/backup/dofullbackup.html?" . $params . " HTTP/1.0\r\n");
fputs($socket, "Host: $domain\r\n");
fputs($socket, "Authorization: Basic $auth\r\n");
fputs($socket, "Connection: Close\r\n");
fputs($socket, "\r\n");
while (!feof($socket)) {
$response = fgets($socket, 4096);
// echo $response; //uncomment this line for debugging
The information in the above script you are to edit should be simple enough to come by.
The next step you are to take is creating a CRON Job to automate an hourly/weekly/monthly backup.
00 4 * * 1 /usr/local/bin/php /home/youraccount/fullbackup.php
This will run your script every Sunday at 4 AM.
You can easily edit your job to specify a particular time you’d like your backup script to run, I set mine this way as it is a time when I’m not likely to be making any changes with any content or material.


I’ve found this script to be exceptionally helpful and hopefully, you will too!

Just a man from the North East trying to make it in the world.

Leave a reply:

Your email address will not be published.