Small Backup script
This script has a few requirments.
- It is ideal to have a cron job run this script
- The backup/remote server must have your public ssh key stored in
authorized_keys2so that we can ssh without a password. - The remote server must have PHP and the script
filesize.phpplaced somewhere. - Very important: What ever path to the remote file you use in the
$a_filesarray, you must create by hand a directory calledarchivewithin that remote directory. For example, if your path is/path/to/my/file.txton 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);
}
?>
$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));
?>
$filename = $_SERVER['argv'][1];
print(filesize($filename));
?>
[Click to add or edit comments])
Please prepend comments below including a date