0

I'm trying to execute with PHP a command (rsync) to copy folders and files from a remote server to a local folder.

This is the code I wrote in php. Command WORKS in SSH (local Terminal and remote with putty.exe), copying correctly the folders and the files.

But it doesn't work in PHP. What can I do? Do you know a better(secure/optimal) way to do this?

exec("echo superuserpassword | sudo -S sshpass -p 'sshremoteserverpassword' rsync -rvogp --chmod=ugo=rwX --chown=ftpuser:ftpuser -e ssh [email protected]:/path/files/folder /opt/lampp/htdocs/dowloadedfiles/", $output, $exit_code);

EDIT: I had read this guide to create a link between my server and my local machine.

Now I can login with ssh in my remote machine without password.

I changed my command:

 rsync -crahvP --chmod=ugo=rwX --chown=ftpuser:ftpuser remote.com:/path/to/remote/files /path/to/local/files/

This command works too in terminal, but when I send it with exec php command, it fails again, but I got another different error: 127.

As MarcoS told in his answer, I checked the error_log.

The messages are this:

ssh: relocation error: ssh: symbol EVP_des_cbc, version OPENSSL_1.0.0 not defined in file libcrypto.so.1.0.0 with link time reference
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: remote command not found (code 127) at io.c(226) [Receiver=3.1.1]
2
  • 2
    What "doesn't work"? Do you get an error? Commented Sep 16, 2016 at 17:46
  • The command do not copy the files and folders. The 2 variables out like this: $output = 1 $exit_code = 1 Commented Sep 19, 2016 at 9:31

2 Answers 2

3

Well, after lot of try/error, I finished to cut the problem in the root:

I readed this guide (like the last one, but better explained) and I changed the php file that execute the rsync command to the remote server (where files are located) and run the rsync.php file there, and it worked perfectly.

To execute in the machine with the files (the files to copy and the rsync.php)

1.- ssh-keygen generates keys

ssh-keygen

Enter an empty passphrase and repeat empty passphrase again.

2.- ssh-copy-id copies public key to remote host

ssh-copy-id -i ~/.ssh/id_rsa.pub remoteserveraddressip(xxx.xxx.xxx.xxx)

The rsync.php file:

exec("rsync -crahvP /path/in/local/files/foldertocopy remoteuser@remoteserveraddress:/path/in/remote/destinationfolder/", $output, $exit_code);

After all of that, navigate to the rsync.php file and all must work. At least worked for me...

Sign up to request clarification or add additional context in comments.

Comments

2

I suppose you are experiencing identity problems... :-)

On a cli, you are running the command as the logged-in user.
On PHP, you are running the command as the user your web server runs as (for example, apache often runs as www-data, or apache user...).

One possible solution I see (if the above is the problem real cause), is to add your user to web-server group...

I'd also suggest you to check the web-server error logs, to be sure about the real cause of the problem... :-)

2 Comments

I got this error while checking error log: ssh: relocation error: ssh: symbol EVP_des_cbc, version OPENSSL_1.0.0 not defined in file libcrypto.so.1.0.0 with link time reference rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: remote command not found (code 127) at io.c(226) [Receiver=3.1.1]
A little late to the problem at hand, but that error means that rsync is not installed on the remote server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.