Forum Moderators: coopster

Message Too Old, No Replies

Cron / PHP Script / FTP

         

cyberdyne

11:03 pm on Nov 24, 2011 (gmt 0)

10+ Year Member



I'm trying to set up a cron script which will backup a home directory (shared hosting) and ftp it to a backup server (private home-based server). All connections are confirmed as good and the script runs but when it comes to ftp-ing the file the output email returns the below error.
Can anyone please tell me what might be causing it?
Many thanks in advance


Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput line 29.

enigma1

10:23 am on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This error is too general. Could be the domain, ip, port that isn't setup right. Enable display of php errors/warnings, run the script manually and see if the php complains about non-initialized variables.

cyberdyne

12:34 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Thank you for your reply.
I ran it manually and the script appears to run fine until it has to connect to my IP. I know my IP is correct and I know the FTP details are correct as I've checked a test account using an online FTP connectivity checker. I also see the backup file being created in the root directory via FTP but the file simply isn't sent. I cannot find any error messages for the script but do believe error reporting is turned on as I've found them elsewhere for other scripts in the past.
The problem appears to be in the FTP process initiating. It seems as though cPanel doesn't want to send the file via FTP and so simply deletes it.
I've included the script below.

<?php
// ********* Configuration *********

// Info required for cPanel access
$cpuser = "cPanel_username"; // Username used to login to CPanel
$cppass = "cPanel_password"; // Password used to login to CPanel
$domain = "mydomain.co.uk"; // Domain name where CPanel is run
$skin = "x3"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP host
$ftpuser = "ftpuser"; // Username for FTP account
$ftppass = "ftppass"; // Password for FTP account
$ftphost = "188.220.***.***"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
$ftpport = "21"; // Port (default = 21)
$rdir = "/"; // Remote dir (defaut = / )

// Notification information
$notifyemail = "my@email.fu"; // 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

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

// *********** Don't Touch! *********

//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&rdir=$rdir&email=$notifyemail&server=$ftphost&port=$port&user=$ftpuser&pass=$ftppass&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);

?>

enigma1

1:36 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually scratch that you're connecting on 2082. Check if both ends don't block the ftp ports and try passive ftp

cyberdyne

2:18 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Passive ftp made no difference I'm afraid. I still received a 'Full Backup Complete' email with the same error message at the bottom and the backup file was delete.

Thanks for your help though.

Incidentally, I can still connect to my site via normal FTP means and I can still connect to my home FTP via test sites.

enigma1

3:30 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have a way of connecting to your ftp server from inside the cpanel and sending the file (manually I mean)? To rule out the php script.

They change stuff with the different cpanel versions so this may not be php related.

PS: And also you should have a log on your ftp server. See what it shows. Was there a connection attempt and what were the parameters/fields

cyberdyne

3:42 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Do you have a way of connecting to your ftp server from inside the cpanel and sending the file (manually I mean)? To rule out the php script.

They change stuff with the different cpanel versions so this may not be php related.
Checking now to see if I can do this.
My cPanel version is: 11.30.5 (build 2)

PS: And also you should have a log on your ftp server. See what it shows. Was there a connection attempt and what were the parameters/fields
No there were no connection attempts whatsoever from my site's IP and never have been on any attempt to run the script. However, the successful connections from the test site are all logged and as expected.

Thanks

enigma1

4:08 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you cannot figure it out from the cpanel, there are other ways, like using the php ftp support. You could run a quick test just use the php example see if the file is send
[php.net...]

If it does, the backup of the cpanel should store the file in a folder you specify. Use the ftp commands and send it. If that doesn't work it could be your firewall on the ftp server. Not sure if you have a log to see the connections there.

cyberdyne

4:53 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Thank you for the above suggestion. I managed to run the script ad it worked perfectly. That is, after it told me that 'uploading' was not allowed. This was a little confusing as I assumed I was downloading but of course, from the remote server's point of view, it is uploading.

Anyhow, I tried my original script again with both uploading and downloading allowed in my FTP server software but I got the same result unfortunately.

So, I'm assuming this indicates that the cPanel FTP works fine and the issue lies with the script.

enigma1

5:27 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or with the version of cpanel and the parameters it accepts. Because you do initiate the backup so that part is working because you get the email. Every code sample I checked is like yours so I doubt the script has errors.

If you cannot find a solution you could always initiate the backup without the remote send parameters, so the cpanel simply stores the backup on the server then you transfer the file using the php ftp commands and delete it after the transfer. As far I can tell it can be fully automated so at least you have another way.

cyberdyne

5:47 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Yes, I already do so and store them on the server but unfortunately my disc space is often low and I don't always get chance to log in and move them off the server so this was the perfect solution. I will look at automating that though.
I'm not going to give up but do thank you for you help.

cyberdyne

6:57 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Found the problem:

$params = "dest=$ftpmode&rdir=$rdir&email=$notifyemail&server=$ftphost&port=$port&user=$ftpuser&pass=$ftppass&submit=Generate Backup";


should have read:

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftpdir&submit=Generate Backup";


The order in which the params are executed is essential

Many thanks for all your help.

enigma1

7:11 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am glad you sorted it but that's really weird. Maybe you should submit a bug report on the cpanel for this.

Unless the actual values for the server, user, directory etc contain some strange characters cpanel doesn't decode properly if you order them in a specific way?

cyberdyne

7:20 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Unsure if they'll accept a bug report from an end-user, I recall trying once before but I'll have a look.
Thanks

I've just seen that there's also an error in my initial code:

port=$port

should read:
port=$ftpport

enigma1

7:27 pm on Nov 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes the port could be a problem although you tried with passive ftp and so it should get a port from your server. Not sure if you had the upload block in effect at that time and it didn't work because of it.

cyberdyne

1:22 pm on Nov 26, 2011 (gmt 0)

10+ Year Member



It appears the issue had already been covered on the cPanel forums, my searches just didn't find it.

[forums.cpanel.net...]