

Listing 2: do_rsync

#!/usr/local/bin/perl
#
############################################################################
#
#  do_rsync
#
#  Author:  Lisa Hamet Bernard, IT consultant, 8/6/2004
#
#  The purpose of this script is to provide a web interface to allow the
#  Webmaster to selectively deploy files from the development website 
#  to the production websites.  The backend tool used is rsync.
#
############################################################################

#user is login for ssh destination server, $dest
# Don't forget trailing slash in source dir for rsync 
$user = "webmaster";
$dest = "\@web1.agency.org:";
$devdir = "/app/website/";
$proddir = "/app/website"; 
$basedir = "/tmp/";  # base directory to put rsync output
$filename = "rsynclist.txt";  #name of rsync output file
             
#Create temporary output file
system "/usr/bin/touch","$basedir$filename";

# Run rsync in dry-run mode to generate list of potential files to be copied
# (Do it via sudo as user webmaster, which can access the remote server via ssh 
# with preshared keys. Then no password needs to be supplied.)

`/usr/local/bin/sudo -u webmaster /usr/local/bin/rsync --recursive --rsh=/usr/local/bin/ssh --dry-run              
--archive --update --exclude-from=/usr/local/apache/cgi-bin/exclude_list.txt $devdir $user$dest$proddir > 
$basedir$filename`;

# Put all of the output from rsync in a form consisting of a table with check 
# boxes and submit and reset buttons

    &open_file("FILE1","","$basedir$filename");
    $counter = 0;

    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<BODY BGCOLOR=#FFFFFF TEXT=#000000 LINK=#0000FF VLINK=#800040 
ALINK=#800040>\n";
    print "<TITLE>Production file deployment list</TITLE>\n";
    print "<CENTER>\n";
    print "<form id=rsyncfiles action=/cgi-bin/move2prod method=post name=rsyncfiles 
enctype=Default>\n";
    print "<TABLE width=521 border=1 cellpadding=0 cellspacing=2>\n";
    print "<TH Colspan=2 title=Production file deployment list</TH>\n";

     print "<tr>\n";
     print "<td align=center><b>Select files to be moved</b></td>\n";
     print "<td align=center><b>Candidate files</b></td>\n";
     print "</tr>\n";

    while ($line = &read_file("FILE1")) {
        if (($line =~ /^building\b/) || ($line =~ /^wrote\b/) || ($line =~ /^total\b/) || ($line =~ /^$/) || ($line =~ 
/^Sun\bMicro/)) {
        # ignore informational output from rsync and OpenSSH
}
        else {
     print "<tr>\n";
     print "<td align=center><input type=checkbox name=file$counter value=$counter></td>\n";
     print "<td nowrap>$line</td>\n";
     print "</TR>\n";
        }
     $counter++;
    }
    close(FILE1);
    if ($counter == 0) {
       print "<B> Development and production sites are identical.<eB>\n";
    }

    print "</TABLE>\n";
    print ("<p><input type=submit name=submitButton value=\"Move selected files\"><spacer size=32 
type=horizontal><input type=reset value=Reset></p>\n");
    print "</form>\n";
    print "</CENTER>\n";
    print "</BODY></HTML>\n";

sub open_file {

  local ($filevar, $filemode, $filename) = @_;
  
  open ($filevar,$filemode . $filename) ||
     die ("Can't open $filename");
}            

sub read_file {

  local ($filevar) = @_;

  <$filevar>;  
}
