#!/usr/bin/perl # Filename: upchanged # Author: David Ljung Madison # See License: http://MarginalHacks.com/License/ # Description: Only upload changed files (based on a touched file) use strict; ################################################## # Setup the variables ################################################## my $PROGNAME = $0; $PROGNAME =~ s|.*/||; my $DESTMACHINE = $ENV{WEB_SERVER}; $DESTMACHINE = "$ENV{WEB_USER}\@$DESTMACHINE" if $ENV{WEB_USER} && $DESTMACHINE; die("No destination") unless $DESTMACHINE; my $TOUCHFILE = ".$PROGNAME.date"; ################################################## # Usage ################################################## sub fatal { foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR: $msg\n"; } exit(-1); } sub usage { foreach my $msg (@_) { print STDERR "ERROR: $msg\n"; } print STDERR "\n"; print STDERR "Usage:\t$PROGNAME [-d] \n"; print STDERR "\tDoes something to the given file\n"; print STDERR "\t-d\tSet debug mode\n"; print STDERR "\n"; exit -1; } sub parse_args { my ($from,$to); while (my $arg=shift(@ARGV)) { if ($arg =~ /^-h$/) { usage(); } if ($arg =~ /^-d$/) { $MAIN::DEBUG=1; next; } if ($arg =~ /^-/) { usage("Unknown option: $arg"); } usage("Too many locations specified [$arg, $from, $to]") if $to; $from ? ($to=$arg) : ($from=$arg); } usage("No 'from' or 'to' location defined") unless $from; $to = $from unless $to; #usage("No 'to' location defined") unless $to; ($from,$to); } sub debug { return unless $MAIN::DEBUG; foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; } } ################################################## # Main code ################################################## sub main { my ($from,$to) = parse_args(); chdir($from) || die("Couldn't cd $from\n"); # Get the files my @files; if (-f $TOUCHFILE) { @files = `find . -newer $TOUCHFILE -type f`; } else { print STDERR "First time [no touchfile], uploading everything\n"; @files = `find . -type f`; } # Any found? unless (@files) { print STDERR "No files changed since last $PROGNAME\n"; exit; } chomp @files; print join("\n",@files),"\n"; # Tar and ssh system("tar cf - @files | ssh -a -x $DESTMACHINE cd $to \\; tar xf -"); # For next update system("touch $TOUCHFILE") unless $?; } main();