Login   Register  
PHP Classes
elePHPant
Icontem

Automating Backup Tasks Remotely with PHP and PECL SSH2 extension - PHP Classes blog

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Automating Backup Tas...   Post a comment Post a comment   See comments See comments (5)   Trackbacks (0)  
<< Previous: 10 steps to migrate W...>> Next: Quickly importing dat...

Author: Jaimie Sirovich

Posted on:

Categories: PHP Tutorials

This article teaches how to setup automated backup PHP scripts using the PECL SSH2 extension to copy files to a remote server.




Picture of Jaimie Sirovich
By Jaimie Sirovich United States
seoegghead.com
<email contact>
Contents

* Introduction

* PECL SSH2 Installation

* SSH2 wrapper library

* About SSH2.php


* Introduction

We sleep better at night when scripts are faithfully taking care of our backups and automating various other annoyances that us humans tend to forget about at the worst possible times.

But this can be frustrating to set up for every web site we have, especially when they are on different servers at various web hosts. It is also difficult to monitor.

What if one of the scripts were to fail unpredictably for whatever reason on a remote server, and you do not even notice?

Enter automation with a few PHP scripts and the SSH2 extension. This lets us automate everything from one server with the power of the PHP scripting language.


* PECL SSH2 Installation

First, lets install PECL SSH2:

For Unix and Linux:

Official instructions can be found in the PHP site:

http://www.php.net/ssh2.installation

With newer versions of PECL SSH, this command line seems to work well:

pecl install ssh2-beta

But if it does not work, Kevin van Zonneveld has a great write up here:

http://kevin.vanzonneveld.net/techblog/articl/make_ssh_conne ...

For Windows:

Installing on Windows is very easy. Either follow the directions on the PHP site (less easy), or install WAMP (piece of cake) and enable SSH2 under:

PHP > PHP extensions > php_ssh2

This code fragment is needed if you do not want it enabled in php.ini, and was also necessary for the installation of WAMP I tested with, regardless of what was set in php.ini.

If you get the message "ERROR: PECL ssh2 must be installed" despite enabling php_ssh2 in WAMP, you will need to use this code:

if (!extension_loaded('ssh2')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_ssh2.dll');
} else {
dl('ssh2.so');
}
}


* SSH2 wrapper library

Next, lets load up my library located at:

http://www.seoegghead.com/software/ssh2-php-wrappers.seo

require_once('SSH2.php');

Proceed to login using password authentication. This is OK if we have the backup machine secured, but if you prefer take some time to implement public key authentication:

$s = new SSH2('MYSERVER');
$s->loginWithPassword('MYLOGIN', 'MYPASSWORD');
$_buf = $s->execCommandBlocking('tar cvf - /www/foo_com/');
file_put_contents('foo_com_backup_' . date('Ymd'), $_buf);

Done!

Of course this little setup is not limited to backups. It is perfectly suited to work in a UNIX-expect-ish way for the most mundane or complex tasks.

For example, we might want to periodically log in to check for a file:

require_once('SSH2.php');
$s = new SSH2('SOMEOTHERSERVER');
$s->loginWithPassword('MYLOGIN', 'MYPASSWORD');
$buf = $s->execCommandBlocking('ls -l /some_directory *.core');

if ($buf) { ... }

You may also read prompts and make decisions with waitPrompt function.

$s->waitPrompt('? ', $_buffer, 10);

if (strstr($_buffer, 'delete all')) {
$s->writePrompt('N');
} else {
$s->writePrompt('Y');
}

That means "wait for a question mark prompt for 10 seconds and give me the output in $_buffer variable. Without waiting 10 seconds it would block forever if there was some unexpected output.

Then writePrompt call sends a 'Y' or an 'N' with a new line character to respond depending on some program logic.

Fairly complex tasks can be automated like this in PHP. And while PHP has expect bindings, I find it a whole lot easier to just use PHP.

I may fashion it to be submitted for PEAR eventually. It will be submitting it to PHPClasses too. For now Please do let me know if you find any bugs!


* About SSH2.php

Of course you may use the SSH2 extension functions yourselves instead of using this wrapper class, but lets discuss some caveats:

1. Blocking timeouts and select() are entirely unsupported by PECL SSH2. That is OK. We can live without it.

I worked around it in this class with non-blocking polling until timeout to simulate the same. Except it also seems that non-blocking I/O also does not work reliably in older versions of the PECL extension. The script will warn you when you set a timeout, if this is the case.

2. I did some sensible stream redirection behind the scenes via the shell to eliminate the need to read from stderr separately. Doing so via PHP without calling select() and limited non-blocking support would be difficult and it makes reading and parsing even more difficult.

Look at the _generateCommand function to see what was done. Please let me know if you see any holes in its logic or bugs.

Lastly, I do not see a straightforward way to ensure that a series of commands complete successfully without echoing a sentinel value and blocking for its output (or any output, really). Use the function execCommandBlockNoOutput to do that.

Examples on many sites use sleep(), but that is not a good idea, since you are assuming the command is executed in that time. Eventually you will be wrong!

For example:

$s->execCommandBlockNoOutput('step1');
$do_something = // ... some logic
if ($do_something) {
$s->execCommandBlockNoOutput('step2_that_depends_on_step1');
}

It is not perfect, but it does make automation pretty easy.


That is all for now. Thank you for reading. Check out our blog if you get a chance. I will be releasing some other good stuff in the next few months.

http://www.seoegghead.com/blog/

You need to be a registered user or login to post a comment

1,282,072 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

Facebook ConnectGmail or other Google Account
Hotmail or Microsoft Windows LiveStackOverflow
GitHubYahoo


Comments:

3. Vars possible hole - Thomas (2009-02-26 00:26)
login details in define fields... - 0 replies
Read the whole comment and replies

2. pure-PHP ssh2 implementation - Thomas McKinzey (2009-02-20 17:01)
should be easier to use than PECL's ssh2 extension... - 1 reply
Read the whole comment and replies

1. Great job - Hasanur Rahaman (2009-02-20 17:01)
It would be a dream come true if it works ..... - 1 reply
Read the whole comment and replies


<< Previous: 10 steps to migrate W...>> Next: Quickly importing dat...

  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Automating Backup Tas...   Post a comment Post a comment   See comments See comments (5)   Trackbacks (0)