Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running this command on shell CLI and it works

sudo -u apache /usr/bin/mysqldump -u root -p"password" --opt dbname | gzip -c | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/path/to/.ssh/known_hosts -l deploy -i /path/to/.ssh/id_rsa user@somehost "/bin/cat > /path/to/backup_db/backup_08-20-2013.gz"

But this command does not work if I run from PHP.

I run this command using system function of PHP system($command, $retval);

But in the $retval the status is 255 and I get this error in the output of system

ssh: connect to host myhostname port 22: Permission denied

The command works even with apache user when I put it directly in shell and it does not ask for password that means the command is able to read the RSA key and all. But if I run it from PHP it does not work. PHP script is also running with 'apache' user.

Please point me right direction.

Thanks

share|improve this question
    
Check php.ini file and see if exec function is disabled under disable_functions. –  PratapSingh Aug 20 '13 at 13:59
    
Check the audit log /var/log/audit/audit.log. –  Michael Hampton Aug 20 '13 at 14:32
    
audit.log is last modified 2 month ago, so nothing there relevant –  Sun Aug 21 '13 at 5:25
    
oh man, add more details about it.. –  stderr Sep 9 '13 at 8:58

2 Answers 2

You might have an easier time if you used phpseclib, a pure PHP SSH2 implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('sudo -u apache /usr/bin/mysqldump -u root -p"password" --opt dbname | gzip -c > /path/to/backup_db/backup_08-20-2013.gz');
?>
share|improve this answer

The ssh command you are running is not run as user apache. Try this:

sudo -u apache --shell /bin/bash <<<$'/usr/bin/mysqldump -u root \
     -p"password" --opt dbname | gzip -c | \
     ssh -o StrictHostKeyChecking=no -o \
     UserKnownHostsFile=/path/to/.ssh/known_hosts -l deploy -i \
     /path/to/.ssh/id_rsa user@somehost "/bin/cat > \
     /path/to/backup_db/backup_08-20-2013.gz"'

Please ask if something is not clear about this line.

share|improve this answer
    
Please explain what is not clear about this line. –  Michael Hampton Jul 5 at 14:06
    
I beg your pardon? I don't understand your question. Do you want me to explain why the OP's line is not good, or do you want me to explain my line? –  moebius_eye Jul 5 at 19:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.