Sync local and remote folders using rsync from php script without entering password

How to synchronize local and remote folders using rsync from inside php script without asking for password?

I have already set up a public key to automate login on a remote server for my user.

So this works without any problems from cli:

rsync -r -a -v -e "ssh -l user" --delete ~/local/file 111.111.11.111:~/remote/;

But when I try to run the same with a PHP script (on a web page on my local server):

$c='rsync -r -a -v -e "ssh -l user" --delete ~/local/file 111.111.11.111:~/remote/';
//exec($c,$data);
passthru($c,$data);
print_r($data);

This is what I get:

255

And not a single file is downloaded from the local to the remote server.

Searching the net, I found this key :

" BASh Expect shell code, , root. nobody apache ( Apache). , phpSuExec, Suhosin suPHP, , ."

, , " " PHP, "apache". , script , - , ... !

UPDATE: - :

passthru('ssh user@111.111.11.111 | ls',$data);

foder, , . rsync, PHP script.

  • chmod -R 0777 . .

UPDATE:

, " ssh $HOME/.ssh/, PHP Apache, $HOME; $HOME/.ssh/id_dsa. , , , ."

rsync, :

if($con=ssh2_connect('111.111.11.111',22)) echo 'ok!';
if(ssh2_auth_password($con,'apache','xxxxxx')) echo ' ok!';
if(ssh2_scp_send($con,'localfile','/remotefolder',0755)) echo ' ok!';

: 0644 : 0775

, php BASh ...

@Yzmir Ramirez : " , " -, apache "- . script , .ssh .

, . - , , , .

+5
3

, 255 , ; . , , , , , 255 .

, , . , , , rsync , . , rsync . su apache , script ( script cwd), , ; rsync, -. , , - , verbosity .

, , . , rsync'ing , , , .

try {
    if ('' != $config['publishSsh']['to']['remote']):
    //we're syncing with a remote server
        $rsyncToRemote = escapeshellarg($config['publishSsh']['to']['remote']) . ':';
        $rsyncTo = $rsyncToRemote . escapeshellarg($config['publishThemes']['to']['path']);
        $keyFile = $config['publishSsh']['to']['keyfile'];
        $rsyncSshCommand = "-e \"ssh -o 'BatchMode yes' -o 'StrictHostKeyChecking no' -q -i '$keyFile' -c arcfour\"";
    else:
    //we're syncing with the local machine
        $rsyncTo = escapeshellarg($config['publishThemes']['to']['path']);
        $rsyncSshCommand = '';
    endif;

    $log->Message("Apache running as user: " . trim(`whoami`), GLCLogger::level_debug);
    $deployCommand = "
        cd /my/themes/folder/; \
        rsync \
        --verbose \
        --delete \
        --recursive \
        --exclude '.DS_Store' \
        --exclude '.svn/' \
        --log-format='Action: %o %n' \
        --stats \
        $rsyncSshCommand \
        ./ \
        $rsyncTo \
         2>&1
    "; //deployCommand
    $log->Message("Deploying with command: \n" . $deployCommand, GLCLogger::level_debug);
    exec($deployCommand, $rsyncOutputLines, $returnValue);
    $log->Message("rsync status code: $returnValue", GLCLogger::level_debug);
    $log->Message("rsync output: \n" . implode("\n", $rsyncOutputLines), GLCLogger::level_debug);
    if (0 != $returnValue):
        $log->Message("Encountered error while publishing themes: <<<$returnValue>>>");
        throw new Exception('rsync error');
    endif;

    /* ... process output ... */
} catch (Exception $e) {
    /* ... handle errors ... */
}

, :

  • exec(), . , , , .

  • rsync . .

  • , , Apache, , rsync rsync. , , , , .

  • rsync, .

. , , :

  • Apache , -; , Apache, . .

  • , , ssh , , - , .

  • suEXEC script , Apache, . , -, Apache, .

, .

+5

- , ( "" "apache" ) /.ssh, "/home/you/.ssh/keyfile" , .

ssh, -i:

ssh -i /home/you/.ssh/keyfile

0

, , .

rsync php, , . . php script 0 1.

, , rsync script, . " " 0, rsync, 1, rsync, 0 rsync;" "

.

0

All Articles