Published 2013-03-04 00:00:00

 Backups, yes we do backup occasionally, and I've been looking for a better solutions than my historical office<->hosting replication, which although cheap, always made me wonder if I was still making copies. So after our recent office move, and minor server upgrade, I thought better double check on it all.. As usual, the thing had failed due to various reasons, and needed replacing. 

So since I've been thinking about a new solution for our clients, I decided to go ahead and try it out for our data. I've been googling affordable cloud based storage for a while and found a company onlinestoragesolutions.com, the pricing is very reasonable US$45 for 2 years currently, with unlimited everything. There are a few review sites that seem to throw some cold water over the offer, but I had one client sign up without any major issues (for another reason), so at that price I though let's give it a go....

One of the key features they advertise is rsync, which since we run all linux machines would be ideal. However after paying, and getting access, I realized it's not quite a simple as pointing your rsync at their server. You need to set up an ssh tunnel to route rsync through.

Can't be that difficult I thought.... Turns out that setting up a password based ssh tunnel, automatically on a cron job, is no small task, there are questions all over stackexchange and various forums, none that I saw managed to find a solution. Most of the suggestions are based around the 'expect' program, a usefull unix tool which can be used to script ssh access. The problem in this case was that setting up the tunnel, then doing the rsync, and then closing the tunnel is not something that a bash, expect or any other method I found could do easily if at all.

So almost at the point of giving up, I started looking around at php's popen (which would not work either), and fell over the pecl expect extension. 

Below is the result of a few minutes coding, which does exactly what is needed, and can be run directly from cron. Feel free to escape from overpriced backup solutions..


#!/usr/bin/php
<?php
 
$username = 'xxxx';
$password = 'xxxx';
$backup_what = '/home';
$target = 'myhome'

ini_set("expect.loguser", "Off");

// forward remote port on the ols server:873 to localhost:9873

$stream = fopen("expect://ssh {$username}@fm.ols18.com -L9873:localhost:873", "r");

$cases = array (
    array ("password:",  'password'),
    array( "yes/no)?", 'yes'),
    array( "Terminal", 'terminal'),
);
while (true) {
    $x = expect_expectl ($stream, $cases);
    switch ($x) {
        case 'terminal': 
            break;

        case 'password':
            fwrite ($stream, $password."\n");
            break;

        case 'yes':
            fwrite ($stream, "yes\n");
            break;
            
        default:
            echo $x; // usuall -1 as line was not matched..
            die ("Error was occurred while connecting to the remote host!\n");

    }
    if ($x == 'terminal') {
        break;
    }
    //echo "break while";    

}
 

putenv('RSYNC_PASSWORD='.$password);
passthru("/usr/bin/rsync -a {$backup_what} rsync://{$username}@localhost:9873/{$username}/{$target}');

fclose ($stream);

Comments

Wrong service provider URL
I think you meant to link to http://onlinestoragesolution.com (without the 's')
#0 - Pies ( Link) on 2013-03-06 21:35:27 Delete Comment
extension
Interesting extension. I'll have to test it.
#1 - hyh ( Link) on 2013-03-13 16:36:54 Delete Comment

Add Your Comment