Published 2013-03-04 00:00:00
#!/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);