Skip to content

Instantly share code, notes, and snippets.

@wei
Created June 2, 2015 22:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save wei/1fc1884aac74dfb7905b to your computer and use it in GitHub Desktop.
Save wei/1fc1884aac74dfb7905b to your computer and use it in GitHub Desktop.
CPanel Full Backup Cron Script
<?php
// PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
// Cron Job Command: /usr/local/bin/php /home/<cpanel-username>/fullbackup.php
// This script contains passwords. It is important to keep access to this file secure (we would ask you to place it in your home directory, not public_html)
// You need create 'backups' folder in your home directory ( or any other folder that you would like to store your backups in ).
// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
// Information required for cPanel access
$cpuser = ""; // Username used to login to cPanel
$cppass = '@y'; // Password used to login to cPanel. NB! you could face some issues with the "$#&/" chars in the password, so if script does not work, please try to change the password.
$domain = ""; // Your main domain name
$skin = "gl_paper_lantern"; // Set to cPanel skin you use (script will not work if it does not match). Most people run the default "x" theme or "x3" theme
// Information required for FTP host
$ftpuser = ""; // Username for FTP account
$ftppass = ''; // Password for FTP account NB! you could face some issues with the "$#&/" chars in the password, so if script does not work, please try to change the password.
$ftphost = ""; // IP address of your hosting account
$ftpmode = "passiveftp"; // FTP mode
// Notification information $notifyemail = "any@example.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
$secure = 1;
// Set to 1 to have web page result appear in your cron log $debug = 0;
$debug = 1;
// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
$ftpport = "21";
$ftpdir = "/backup_dir/"; // Directory where backups stored (make it in your /home/ directory). Or you can change 'backups' to the name of any other folder created for the backups;
// Delete Old Files
$daysOld = '40'; //Enter the age of files in days, if a file should be deleted that's older than 2 days enter 2
$filesToSkip = array(
'.',
'..',
'.ftpquota'
); //Contains the files that the script needs to skip if it comes accross them
$notificationEmail = 'notifyt@mydomain.com'; //The email address to send a notification when file fails to delete
//FTP session starting
$ftpconnection = ftp_connect($ftphost);
$login = ftp_login($ftpconnection, $ftpuser, $ftppass);
if (!$ftpconnection || !$login) {
die('Connection attempt failed!');
}
//Switching to passive mode
ftp_pasv($ftpconnection, TRUE);
//Calcuting the datetime of todays day minus the amount of days entered
$dateToCompare = date('Y-m-d', strtotime('-' . $daysOld . ' days', time()));
//Looping through the contents of the provided directory
$files = ftp_nlist($ftpconnection, $ftpdir); //ftp_rawlist — Returns a detailed list of files in the given directory
foreach ($files as $file) {
//Check if the file is in the list of files to skip, if it is we continue the loop
if (in_array($file, $filesToSkip)) {
continue;
}
$modTime = ftp_mdtm($ftpconnection, $file);
if (strtotime($dateToCompare) >= $modTime) {
if (!ftp_delete($ftpconnection, $file)) { //Deleting the file that needs to be deleted
//If the file fails to delete we send a mail to the administrator
//mail($notificationEmail, 'FAILED TO DELETE FILE', 'FAILED TO DELETE FILE: '.$file);
echo "Failed to delete " . $file;
} else {
echo "Deleted " . $file;
}
}
}
// Proceed with backup request
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=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftpdir&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 do not do anything with it.
while (!feof($socket)) {
$response = fgets($socket, 4096);
if ($debug)
echo $response;
}
fclose($socket);
?>
@fatihates
Copy link

Hello, I want to use this process directly for the backup taken to the WHM (Home) directory.

To put it more clearly, the server does not back up all packages because there is no WHM license. I'm manually logging into ssh root and backing up with the code for i in $(ls /var/cpanel/users); do /scripts/pkgacct $i;done I want to bind this to a scheduled task.

Is it possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment