sams.folk
By:
sams.folk

rsync: How to Use in a PHP File

July 24, 2015 11.7k views
PHP Linux Commands Ubuntu

I'm trying to send an image file from one server to another using rsync. The sending server's public key has been placed into the receiving server's /root/.ssh/authorized_keys . (I'll not use root later on. Just trying to keep things as simple as possible for now.)

I can get an image copied successfully to the receiving server from the command line.

rsync -va /var/www/html/nameofimage.jpg root@111.222.333.444:/images/

However, I do not know how to write this into a PHP file. I thought this would work.

<?php 
exec('rsync -va /var/www/html/nameofimage.jpg root@111.222.333.444:/images/');
?>

But when this file is invoked from the command line, I receive the message ...
rsync: link_stat "/home/myusername/ssh" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.0]

Thanks for any direction with this.

6 comments
  • I found the PHP code did work. I just needed to change permissions to execute, to go along with the exec( ) directive.

    Invoking the .php file from the command line is now successful. Currently, though, I'm stuck on invoking the file from a webpage. I've tried changing the .ssh, idrsa, and idrsa.pub to owner www-data, as learned when scr'n, but that didn't help this time. I'm receiving the following error. Don't know why it's wanting to create a directory. These messages are not too succinct.

    Could not create directory '/var/www/.ssh'.
    Host key verification failed.
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]

  • Because ssh always validate host key and then create known_hosts file under ~/.ssh directory.
    You can manually create the .ssh directory and chown to www-data.
    If not just tell ssh to skip host key checking.

    rsync -e "ssh -o StrictHostKeyChecking=no,UserKnownHostsFile=/dev/null" --other-options-here
    

    Ref: https://www.digitalocean.com/community/tutorials/how-to-copy-files-with-rsync-over-ssh

    by Bulat Khamitov
    In this tutorial we'll use Rsync to copy files over SSH.
  • Thanks for replying Tony.

    I'm still having problems. I modified the comma part and inserted the ssh information into the directive.

    <?php 
    exec('rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" /var/www/html/nameofimage.jpg root@111.222.333.444:/images/');
    ?>
    

    I then received this error.

    Could not create directory '/var/www/.ssh'.
    Failed to add the host to the list of known hosts (/var/www/.ssh/known_hosts).
    Permission denied, please try again.
    Permission denied, please try again.
    Permission denied (publickey,password).
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]

    So I made a /var/www/.ssh directory. I've tried owners root, myusername, and www-data, but still get this error.

    Warning: Permanently added '45.55.136.175' (ECDSA) to the list of known hosts.
    Permission denied, please try again.
    Permission denied, please try again.
    Permission denied (publickey,password).
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]

  • Late reply, no notification for comment reply.

    Seems you are missing the identity file (private key), you have three methods to specify the identity file

    1. Copy your key to /var/www/.ssh/id_rsa (id.dsa or id_ecdsa) & chmod 0600 /var/www/.ssh/id_rsa.
      Then let ssh search for it.

    2. Specify it with -i option:

      rsync -e "ssh -o StrictHostKeyChecking=no,UserKnownHostsFile=/dev/null -i /path/to/your/private/key" --other-options-here
      
    3. Specify in config file /var/www/.ssh/config (chown www-data & chmod 0600 may be required)
      Edit it with following content:

      Host name_of_your_choice
      Hostname 45.55.136.175
      Port 22
      User your_user_name_at_remote_machine
      PreferredAuthentications publickey
      IdentityFile /path/to/your/private/key
      
  • Edit my comment

    For method 3, you should use name_of_your_choice instead of username@hostname.com

    e.g.

    rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" /var/www/html/nameofimage.jpg name_of_your_choice:/images
    

    Hope this help.

  • That solved it.

    I went with the first suggestion, and the image file made it to the other server. Seems I had the wrong Key in the file. It was replaced and things worked.

    Your explanation was clear and elaborate. Thanks for the assistance!

1 Answer

This question was answered by @TonyTsang:

Late reply, no notification for comment reply.

Seems you are missing the identity file (private key), you have three methods to specify the identity file

  1. Copy your key to /var/www/.ssh/id_rsa (id.dsa or id_ecdsa) & chmod 0600 /var/www/.ssh/id_rsa.
    Then let ssh search for it.

  2. Specify it with -i option:

    rsync -e "ssh -o StrictHostKeyChecking=no,UserKnownHostsFile=/dev/null -i /path/to/your/private/key" --other-options-here
    
  3. Specify in config file /var/www/.ssh/config (chown www-data & chmod 0600 may be required)
    Edit it with following content:

    Host name_of_your_choice
    Hostname 45.55.136.175
    Port 22
    User your_user_name_at_remote_machine
    PreferredAuthentications publickey
    IdentityFile /path/to/your/private/key
    

View the original comment

Have another answer? Share your knowledge.