Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- cPanel Backup Script
- This script will auto create backup file, transfer to FTP server
- By Michael Phan @ sondaika.com
- */
- // Change to your information
- $cpanel_user = "cpaneluserexample"; // change to your cpanel user
- $cpanel_password = "cpaneluser_password"; // change to your cpanel password
- $domain = "exampledomain.com"; // your domain
- $email_to_report = "[email protected]"; // Your email to receive notification when the script finish backup. Leave it blank if you dont want to receive notification.
- $ftp = true; // transfer to FTP server or not
- $ftpserver = "202.150.xxx.xxx"; // your ftp server address
- $ftpusername = "ftp_example_username"; // your ftp username
- $ftppassword = "ftp_example_password"; // your ftp password
- $ftpdirectory = "/example_path/"; // your backup directory
- $daytostore = 3; // how many days do you want to store the backup file, the files which older than this number of day will be deleted
- //This part is option, the bellow information is work for almost user
- $ftpport = "21"; // default port for FTP connection of most hosting providers now
- $theme = "paper_lantern"; // change to your cpanel theme name, default is most popular theme now named paper_lantern
- $secure = true; // use https or not
- // =================================================================================
- // ========= do not change anything bellow unless you are a master of PHP ==========
- // =================================================================================
- // Backup
- $auth = base64_encode("$cpanel_user" . ":" . "$cpanel_password");
- if ($secure) {
- $url = "ssl://" . $domain;
- $port = 2083;
- } else {
- $url = $domain;
- $port = 2082;
- }
- $socket = fsockopen($url, $port);
- if (!$socket) {
- exit("Failed to open socket connection.");
- }
- if ($ftp) {
- $params = "dest=ftp&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;
- }
- fclose($socket);
- // delete old file from ftp if FTP set to true
- if ($ftp) {
- // set up basic connection
- $conn_id = ftp_connect($ftpserver);
- // login with username and password
- $login_result = ftp_login($conn_id, $ftpusername, $ftppassword);
- if ($login_result) {
- $filelistname = ftp_nlist($conn_id, $ftpdirectory);
- foreach ($filelistname as $key => $value) {
- $file = $ftpdirectory . $value;
- $mdftime = ftp_mdtm($conn_id, $file);
- if (($value!==".") && ($value !=="..") && ((time() - $mdftime) > $daytostore * 86400)) {
- // try to delete $file
- if (ftp_delete($conn_id, $file)) {
- echo "$file deleted successful\n";
- } else {
- echo "could not delete $file\n";
- }
- }
- }
- // close the connection
- ftp_close($conn_id);
- }
- else {
- echo "FTP connection Failed";
- }
- }
- // send report email
- if ($email_to_report !== "") {
- $attime = date('d-m-Y H:i:s');
- $to = $email_to_report;
- $subject = 'Auto backup for ' . $domain . ' at ' . $attime . ' successful.';
- $message = 'Auto backup for ' . $domain . ' at ' . $attime . ' successful.';
- $headers = 'From: autobackupreport@' . $domain . "\r\n" .
- 'Reply-To: autobackupreport@' . $domain . "\r\n" .
- 'X-Mailer: PHP/' . phpversion();
- mail($to, $subject, $message, $headers);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment