http://www.perlmonks.org?node_id=1033648

Jackiejarvis has asked for the wisdom of the Perl Monks concerning the following question:

Like in my previous post I would ask you to give some feedback on my second script. This script will start a rsync process between to places. I would like to get some feedback on this script so I can improved it or maybe you see some things that can go wrong that I don't see.

Many thanks!

#!/usr/bin/perl # (c) Dieter Odeurs # This script starts rsync for a back-up job use strict; use warnings; use Getopt::Long; my $path = ""; my $destination; my $help; #Show usage sub showUsage{ print <<EOF; ---------------------------- -- Usage for rsync script -- ---------------------------- Required: --path : The source directory, this is the directory you want to back-u +p Example --path='/path/to/source' --destination This is the destination, where you want to put the back-up Example --destination='/path/to/destination' Optional:"; --exclude Use this option if you want to exclude some directories Without this option the script will exclude only .etc and lost ++found Example: --exclude='test' --exclude='demo' EOF exit; } #Without exclude options the script automatically exclude .etc and los +t+found my @exclude = ( ".etc", "lost+found" ); #You must specify a path, destination and if you want to exclude some +extra files it's possible with exclude showUsage() if (!GetOptions ( 'path=s' => \$path, 'exclude=s' => \@exclude, 'destination=s' => \$destination, 'help|?' => \$help, '<>' => \&showUsage ) or defined $help ); # Check if the path exist otherwise exit the program and give an error if(defined $path){ if (-d $path){ print $path . " is a correct path \n"; }else{ print $path . " is a incorrect path \n"; exit; } }else{ showUsage(); } # Check if a destination is defined unless (defined $destination) { showUsage (); } print "exclude : @exclude \n"; # Create the command to start rsync with all the excludes and options my $cmd = "rsync -av --iconv=iso88591,utf8 --stats"; foreach my $exclude (@exclude) { $cmd = $cmd . " --exclude '$exclude'"; } $cmd .= " $path $destination"; print $cmd . "\n"; # Run the program and show the output my @result; @result = `$cmd 2>&1`; foreach my $result (@result) { print $result; chomp $result; } #exit the program exit;