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
GREAT BLOG
The hilarious moments are very few because life is the name of seriousness. The comical or funny people try their best to make the moments so bright. This work is not easy because you have to utilize your all senses.
#2 - susanrichard ( Link) on 2017-11-10 15:16:58 Delete Comment
Top Universities that Welcome International Students for Creative Writing
In the present day, the number of international students is increasing every year. Students love to study in a foreign country and many students dream of studying abroad when they are very young. It is possible for students who are dreaming of studying abroad since more and more universities, colleges and schools across the world welcomes international students. If you look at the classrooms of universities, colleges and schools across the world you will be able to find out a number of students who are from out of country. Diversity is not only seen in public life but also in the classroom and it is always a great thing to have diversity in classroom as it enhances learning.
#3 - buy essays ( Link) on 2018-03-27 14:29:01 Delete Comment
health information
Gracia Clinic is one of the largest clinics in Jakarta today. With complete and modern medical equipment, Gracia Clinic is ready to compete with other clones and hospitals inside and outside Jakarta. http://klinikutama.com/4-gejala-sifilis-dan-cara-mengatasinya-102328.html
#4 - gracia ( Link) on 2018-04-11 15:52:02 Delete Comment

Add Your Comment

Follow us on