Php – Copy remote file with rsync in php

linuxPHPrsyncssh

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 remoteserveruser@remoteserver.com:/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]

Best Answer

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...