views:

649

answers:

3

Hi !

Here's my goal : I have a Windows XP PC with all the source code in it and a development database. Let's call it "pc.dev.XP". I have a destination computer that runs Linux. Let's call it "pc.demo.Linux". Here's what I've done on "pc.dev.XP" (just so you get the context) :

  • installed all cygwin stuff
  • created a valid rsa key and put it on the dest backup computer so that ssh doesn't ask for a password
  • rsync works pretty well this way

If i try to do this on "pc.dev.XP" via a command line :

cd \cygwin\bin
ssh [email protected] "cd /var/www && ls -al"

this works perfectly without asking a password Now here's what I want to do on the "pc.dev.XP":

  • launch a php script that extract the dev. database into a sql file
  • zip this file
  • transfer it via ftp to the "pc.demo.Linux"
  • log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"

if I run on "pc.dev.XP" manually :

putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql

this works perfectly. Same for :

cd \cygwin\bin
ssh Fred@dest "cd /var/www && ls -al"

If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.

Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")

Any idea what I could do / check / correct ?

A: 

You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?

If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.

"if I run on the development PC manually this works perfectly.". Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.

Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.

carl
I wanted to "go to http://myWebserver.com/crazyScript.php" and all this happens... but you made me think... creating a batch CMD / DOS file that would call "php.exe myscript.php" then call "ssh.exe [with good arguments]""use php to connect to the local ssh thereby using alternate credentials" That's maybe what I should do... but technically, how ?
Olivier Pons
+1  A: 

I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:

php extractFromDb.php | ssh [email protected] "mysql remoteDatabaseName"

This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.

If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:

php extractFromDb.php | gzip -9 | ssh [email protected] "gunzip | mysql remoteDatabaseName"
Wim
This seems to be a very good idea I'll check it out Monday thanks
Olivier Pons
A: 

Here's what I did : a batch file that :

  1. Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
  2. Calls rsync to synchronize the source folder
  3. Calls putty -l user -pw password -m file_with_ssh_commands_to_execute

It works like a charm.

Olivier Pons