Exemple #1
0
function walk_dirs($files, $backup_dirs, $path_prefix = '')
{
    foreach ($backup_dirs as $bd) {
        if ($bd == '.' || $bd == '..') {
            continue;
        }
        $path_prefix = fix_path_end($path_prefix);
        $bd = "{$path_prefix}{$bd}";
        if (is_file($bd)) {
            array_push($files, $bd);
            continue;
        }
        if (!is_dir($bd)) {
            continue;
        }
        $sres = scandir($bd);
        if ($sres === false) {
            echo "Cannot scan {$sres}\n";
        }
        $files = walk_dirs($files, $sres, $bd);
    }
    return $files;
}
}
$targz = "{$archive_prefix}.tar.gz";
echo "Creating {$targz}\n";
exec('tar --ignore-failed-read --numeric-owner -czpf ' . escapeshellarg($targz) . ' --files-from=' . escapeshellarg(BACKUP_LIST), $output, $return_var);
if ($return_var == 0) {
    echo "Files packed\n";
} else {
    die("tar command failed: {$return_var}\n");
}
$targzgpg = "{$targz}.gpg";
exec('gpg --passphrase-file secret.txt --symmetric --cipher-algo AES256 -o ' . escapeshellarg($targzgpg) . ' ' . escapeshellarg($targz));
unlink($targz);
echo "Connecting to " . REMOTE_HOST . "\n";
$connection = ssh2_connect(REMOTE_HOST, 22);
if (!$connection) {
    die("Could not connect to " . REMOTE_HOST);
}
echo "Authenticate as user " . REMOTE_USER . "\n";
if (!ssh2_auth_password($connection, REMOTE_USER, REMOTE_PASSWORD)) {
    die("Could not authenticate as user " . REMOTE_USER);
}
$sftp = ssh2_sftp($connection);
$remote_path = fix_path_end(REMOTE_BASE_PATH) . $hostname;
echo "Ensure remote {$remote_path} exists\n";
ssh2_sftp_mkdir($sftp, $remote_path, 0770, true);
$remote_file = $remote_path . '/' . $targzgpg;
if (!ssh2_scp_send($connection, $targzgpg, $remote_file)) {
    die("Could not upload {$targzgpg}\n");
}
unlink($targzgpg);
echo "Statistic:\n\nNew files: {$new_files}\nChanged files: {$changed_files}\nNot modifed files: {$unchanged_files}\n";