3

Why this:

rsync -avz -e ssh /home/userneme/folder username@domain.com:/var/www/folder

works from cronjob and this:

exec("rsync -avz -e ssh /home/userneme/folder username@domain.com:/var/www/folder");

doesn't work.

I know exec is working because i have a few places in my appp that do convercion from pdf to jpg with ImageMagick (exec).

SOLVED

exec is working OK it was a permission issue on remote server. "Local" server is shared reseller account and remote server is my first VPS Ubuntu 10.10 LAMP box. If only I had a system administrator since i'm just a software developer forced to do this and i stink at it :) Thank You all!

Joel Coel
  • 12,910
  • 13
  • 61
  • 99
mojeime
  • 33
  • 1
  • 4
  • 1
    Do you use ssh keys with rsync? Your apache have access to this keys ? – Sacx Mar 18 '11 at 13:06
  • yes passwordless ssh public. They are working fine with cronjob. For apache i don't know because "local" server is shared (Hostgator) – mojeime Mar 18 '11 at 13:08

4 Answers4

4

This is probably PATH related. try execing rsync with a full path e.g.

exec("/usr/bin/rsync -avz -e ssh /home/userneme/folder username@domain.com:/var/www/folder");
user9517
  • 114,104
  • 20
  • 206
  • 289
4

Your ssh keys should be also on apache home folder. If not, the rsync will not work.

Regards

Sacx
  • 2,541
  • 15
  • 13
  • or specified with -i, iirc – 3molo Mar 18 '11 at 13:13
  • You mean apacheuser/.ssh folder. If this is true that i am stuck becouse "local" is hostgator reseller account – mojeime Mar 18 '11 at 13:20
  • Yes. Sorry. try to don't use sshkeys and pipe the password directly to rsync. – Sacx Mar 18 '11 at 13:26
  • After adding: -i /home/user/.ssh/id_rsa.pub output of exec changed to this: array(1) { [0]=> string(27) "building file list ... done" }, but file is not transfered. – mojeime Mar 18 '11 at 14:27
  • Do you have enough rights to that folders ? – Sacx Mar 18 '11 at 14:38
  • That was the problem. It worked as soon as i switched destination folder to /var/test even without -i switch. Strange thing (i know i shouldnot do that but...) i am autorizing as root@domain.com and my /var/www (755) is owned by www-data:www-data. Why can't i write to my document root (var/www) even if i am root? – mojeime Mar 18 '11 at 14:51
  • As root should work. Probably the problem is elsewhere. Try with /var/www/ and look on the destination server syslog/messages log files. Probably there will appear the reason why is not working. – Sacx Mar 18 '11 at 15:05
  • For some strange reason it works with /var/www. Never mind, let's say it was my typo before :) Thank you! – mojeime Mar 18 '11 at 15:17
2

Does your host have safe_mode on?

Run php -i |grep -i safe. If safe mode is on, then look at safe_mode_exec_dir. If rsync isn't in one of the listed folders, then PHP won't exec it.

You can ask your host to put a symlink to rsync in the exec_dir path, and then exec it from that location; but it depends on their policy as to whether they'll allow it.

SmallClanger
  • 8,947
  • 1
  • 31
  • 45
1

You should be able to do this by giving ssh a specific key file to use in the -e option to rsync. Since you're using a shared host, what you need to do is create the ssh key somewhere safe, then put the keys onto the host in some directory as safe as you can get (should only be readable by you and apache, but if anyone else knows about it they can use a PHP script or something else to have apache send your key to them. If your host uses suexec for scripts so they all run under your username, you can make it readable only by you.)

Then you would use

rsync -avz -e "ssh -i /some/id_?sa_key" /home/userneme/folder username@domain.com:/var/www/folder

Since this isn't very secure, you should set up the remote side's authorized keys file to force a command that will, at the very minimum, verify that the command being executed is "rsync".

More on automating rsync (including a script to verify the ssh command) here.

DerfK
  • 19,313
  • 2
  • 35
  • 51