Small Backup script

This script has a few requirments.

  1. It is ideal to have a cron job run this script
  2. The backup/remote server must have your public ssh key stored in authorized_keys2 so that we can ssh without a password.
  3. The remote server must have PHP and the script filesize.php placed somewhere.
  4. Very important: What ever path to the remote file you use in the $a_files array, you must create by hand a directory called archive within that remote directory. For example, if your path is /path/to/my/file.txt on the remote server, you would have to create the directory on the remote server, /path/to/my/archive.

File: backup_to_remote.php

<?php
$a_files = array(
        "/path/to/file1/on/local"=>"/backup/path/to/file1/on/remote",
        "/path/to/file2/on/local"=>"/backup/path/to/file2/on/remote"
        );

$timestamp = date("YmdHis");

foreach($a_files as $local_file=>$remote_file)
{
        $a_local_file = pathinfo($local_file);
        $a_remote_file = pathinfo($remote_file);

        $local_file_name = $a_local_file['basename'];
        $local_file_path = $a_local_file['dirname'];
        $remote_file_name = $a_remote_file['basename'];
        $remote_file_path = $a_remote_file['dirname'];

        $local_filesize = filesize($local_file);

        exec("ssh bob@remote-server 'php /path/to/filesize.php {$remote_file}'",$a_remote_filesize);
        $remote_filesize = $a_remote_filesize[0];

        if($local_filesize != $remote_filesize)
        {
                exec("ssh bob@remote-server 'mv {$remote_file} {$remote_file_path}/archive/{$remote_file_name}.{$timestamp}'");
                exec("scp {$local_file} bob@remote-server:{$remote_file_path}/");
        }

        unset($a_remote_filesize);
}
?>

File: filesize.php

<?php
$filename = $_SERVER['argv'][1];

print(filesize($filename));
?>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.