<?php
// PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
// This script contains passwords. KEEP ACCESS TO THIS FILE SECURE! (place it in your home dir, not /www/)

// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********

// Info required for cPanel access
$cpuser = "cpaneluser"; // Username used to login to CPanel
$cppass = "cpanelpw"; // Password used to login to CPanel
$domain = "mycpanel.website.com:port"; // Domain name where CPanel is run and the port. I had to put the port here to get it to work.
$skin = "x3"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme (I was running x3)

// Info required for FTP host
$ftpuser = "username"; // Username for FTP account
$ftppass = "password"; // Password for FTP account
$ftphost = "website.com"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
$ftpport = "21"; // FTP Port (21 default)
$ftprdir = "backupdir"; // Backup Directory

// Notification information
$notifyemail = "notifyme@website.com"; // Email address to send results

// Secure or non-secure mode
$secure = 1; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********

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=$ftprdir&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 don't do anything with it.
while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}

fclose($socket);
?>

The above PHP script, works, but it is not writing to the backup directory ?

Recommended Answers

All 13 Replies

Is it 100% writable? Is there enough space? Does the user you're defining have access to that dir? Does that user have remote access at all?

Yes to all of the above, it is still not writting, arghh.

What do you mean by "The script works"?

That is the message I put into the script, when CronJobs executes. Although it is not backing up as it should in the directory that I have placed into the script.

No other suggestions or help ?

Hi,

Check if your remote server target directory is writable. On your PHP.ini file, make sure these values are increase to some acceptable values that will allow the script to finish its job.

Run a php info script and look for these values both of your servers

max_execution_time
post_max_size
max_input_time
upload_max_filesize

You check those values and let us know.

Can't you find a better script besides what you have posted above? why not just use a PHP ftp as simple as this ... Warning! NOT TESTED... check my reference here

function connect_ftp($remoteServerHost, $username, $password, $filesToBeUploaded, $directory_and_file_name_InRemoteServer){

$init_connect = ftp_connect($server);
$validate_login = ftp_login($init_connect, $username, $password);

if($init_connect && $validate_login){
    $init_upload = ftp_put($init_connect,$directoryInRemoteServer,$filesToBeUploaded,FTP_ASCII);
}
else{
    die('Unable to initialize connection to remoter server');
}

if(!$init_upload){
    echo 'Upload Attempt was a complete failure, check your directory permission. Check if the file size is not extremely large';
}

ftp_close($init_connect);
}

Example of implementation..

connect_ftp('ftp.remoteServer.com', 'username', 'password', 'backupTar.tar', 'backup/backupTar.tar');

more tips for CHMOD and other things.. Try searching on the the mode option, and change FTP_ASCII to FTP_BINARY..

You can also experiment with FTP_AUTO .. I am not sure, but there should be some function to set it to auto mode.

I changed the CHMOD to 777, I have to wait 12 hours between to see the results of CronJobs. I hope it works, otherwise, step two, I'll update on my progress. :)

Thank You

The script is suppose to save the entire server to a folder in a compressed file, that isn't occuring. I changed the CHMOD to 777, which I should have done in the first place, my fault.

There isn't an option for FTP_ASCII or FTP_Binary in the script ? Arghhh this was suppose to work, very frustrating. The CronJob and the script run fine, it is the script that isn't doing what it's suppose to.

veedoo

  1. max_execution_time = 60 / 60
  2. post_max_size = 100M / 100M
  3. max_input_time = 60 / 60
  4. upload_max_filesize = 64M / 64M

Sorry, should I run the script you posted; on my server ?

  function connect_ftp($remoteServerHost, $username, $password, $filesToBeUploaded, $directory_and_file_name_InRemoteServer){
    $init_connect = ftp_connect($server);
    $validate_login = ftp_login($init_connect, $username, $password);
    if($init_connect && $validate_login){
    $init_upload = ftp_put($init_connect,$directoryInRemoteServer,$filesToBeUploaded,FTP_ASCII);
    }
    else{
    die('Unable to initialize connection to remoter server');
    }
    if(!$init_upload){
    echo 'Upload Attempt was a complete failure, check your directory permission. Check if the file size is not extremely large';
    }
    ftp_close($init_connect);
    }

Hi,

The php.ini values are not enough.. it will just time out on you.. on the average of 60MB to 100MB file transfer the recommended settings are about

max_input_time = 1000
max_execution_time = 1000
upload_max_filesize = 200M
register_argc_argv = On
post_max_size = 200M

Don't run the script until both of the php.ini files on both servers are properly set.

You let me know when your done, and I will write a simple script than can transfer up to 1gb server to server without the use of FTP or any of those crap they overly hyped. 1gb in less than 5 minutes server to server. This is the script I used to move server to server. I don't really use the cpanel transfers.. just open the script on the browser, and the files are on its way..

I want to back up only one server, to a directory on the same server all directories, files etc backed up ! Is there any other change, besides I assume changing the PHP values ?

Veedeoo - I want to get something cleared up. If those PHP configuration values are increased, the script will execute as it should ? That is the only reason why the script is failing !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.