cerebral discharge
Messages
  • You are not using a Gecko-based web browser. That means that this site does not look as good to you. I recommend following the Firefox link on this page (if you can see it), and downloading a secure, standards-compliant browser. If you're using Internet Explorer, you run a higher risk of getting viruses and putting up with other nonsense. If you're using a good browser, and everything appears swell (including all the rounded corners), then shoot me an email so I can fix this message.
SCP Update Script

This is a console PHP script that takes a directory structure and uploads it, excluding specified files and unchanged files, using scp. Chances are, if you're reading this, you know what scp is. If not, it's a command line utility that can copy (cp) files via a remote ssh connection. To make things simpler, look up how to make ssh bypass passwords by generating a public and private key.

#!/usr/bin/php -q
<?php
// change these for your server
$remote_name 'username';
$remote_host 'server-name.com';
$remote_path 'relative/path/to/folder';
// add files that should be skipped (you must begin all with ./)
$_SKIP = array('.''..''./' basename(__FILE__), './examplefolder''./examplefile.txt');

// don't change anything below here
$_SEND = array();
function 
add_send($dir$filename) {
    global 
$_SEND;
    
$_SEND[$dir][] = $filename;
}

function 
should_skip($filename) {
    global 
$_SKIP;
    return 
in_array($filename$_SKIP);
}

function 
scan_dir($dirname$time) {
    
$dh opendir($dirname);
    while(
$filename readdir($dh)) {
        if(
$filename == '.' || $filename == '..') {
            continue;
        }

        
$fullname $dirname '/' $filename;
        if(
should_skip($fullname)) {
            continue;
        }


        if(
is_dir($fullname)) {
            
scan_dir($fullname$time);
            continue;
        }


        if(
filemtime($fullname) > $time) {
            
add_send($dirname$fullname);
        }
    }
    
closedir($dh);
}


$time filemtime("lastsend");

scan_dir('.'$time);
print_r($_SEND);
foreach(
$_SEND as $dir => $files) {
    if(
count($files) == 0) {
        continue;
    }
    
$cmd "scp " join(' '$_SEND[$dir]) . " $remote_name@$remote_host:$remote_path/$dir";
    echo 
$cmd "\n";
    
passthru($cmd);
}
touch('lastsend');

?>