Listing 3: move2prod

#!/usr/local/bin/perl
##############################################################################
#
# move2prod
#
# Author:  Lisa Hamet Bernard, IT Consultant, 8/6/2004
#
# This script is the back end of the do_rsync script, which enables the
# Webmaster to selectively deploy files from the development website to the 
# production websites.  The do_rsync script passes this script the list of 
# indexes of the files to be moved, referencing /tmp/rsynclist.txt.  This
# script performs the rsync for those files, after making timestamped copies of 
# any named files that already exist on production.
#
#############################################################################
use CGI qw(:standard escapeHTML);
use CGI 'param';

#Redefine the user login and machine names of both production servers, the development directory,
# the production directory, the temporary file containing dry-run rsync output from do_rsync
# and the temporary file in which to write files to be copied.

$user = "webmaster";
$dest1 = "\@web1.agency.org:";
$dest2 = "\@web2.agency.org:";
$devdir = "/app/website/";
$proddir = "/app/website";
$basedir = "/tmp/";
$filename = "rsynclist.txt";
$cpfilename = "copylist.txt";

# get the file index list from the do_rsync form
my @fileindex = param();

&open_file("FILE1","","$basedir$filename");
# Read contents of rsync dry-run into an array
@filelist = <FILE1>;

# output the HTML
print header(), start_html("Development files deployment completion");

# If at least one filename parameter passed, open output file
if ($#fileindex > 0) {
   &open_file("FILE2",">","$basedir$cpfilename");
}

# For each parameter passed, get its value and use it as an index into 
# filelist, then write that filename to a second temp file containing the list of only those
# files that are to be deployed.  

 for ($i=0; $i<$#fileindex; $i++)  {
       $filenum = param($fileindex[$i]);
       $rsyncfile = $filelist[$filenum];
       print FILE2 $rsyncfile;
}

close(FILE1);
close(FILE2);
unlink($basedir.$filename);

# Get timestamp string (in mmddyyHHMM format) to use for rsync suffix.

$timestamp = `date '+%m%d%y%H%M'`;
chomp($timestamp);
$suffix = "." . $timestamp;

# Do the rsync from development to the production servers using /tmp/copylist as list of files
# to include.  Use sudo so rsync executes as user webmaster (on remote server), who has preshared 
# ssh keys between this server and the production servers. Use the archive option to preserve all file
# attributes.  Use the combination of the backup and suffix options to make copies of existing files 
# on the production server with the timestamp suffix before replacing.

`/usr/local/bin/sudo -u webmaster /usr/local/bin/rsync --rsh=/usr/local/bin/ssh --archive --update --relative   
--backup --suffix=$suffix --files-from=$basedir$cpfilename $devdir $user$dest1$proddir`;

# Repeat above for second production server

`/usr/local/bin/sudo -u webmaster /usr/local/bin/rsync --rsh=/usr/local/bin/ssh --archive --update --relative   
--backup --suffix=$suffix --files-from=$basedir$cpfilename $devdir $user$dest2$proddir`;

# Delete temp file
unlink($basedir.$cpfilename);
             
print p("rsync operation is complete.  ", $#fileindex, " files deployed to production.");

print end_html();

sub open_file {

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

