#!/usr/bin/perl # rsync_backup Copyright Peter R. Wood, 2006 # Website: http://prwdot.org/ # You may use this code as long as my name remains # attached to it or any derivative works using it. my $config_file = $ARGV[0]; die "No config file specified!\n" unless $config_file; my $configuration = fetch_config($config_file); my $rsync_command = '/usr/bin/rsync ' . join(' ',$configuration->{rsync_options},$configuration->{rsync_source},$configuration->{rsync_target}) . ' >> ' . $configuration->{logfile}; my $starttime = time(); log_message("=" x 80); log_message("Backing up " . $configuration->{description}); # This will prevent rsync from running if the source does not exist # i.e., a disk is not currently mounted. if ( $configuration->{rsync_source} =~ /^\// && !-e $configuration->{rsync_source} ) { log_message("Error: " . $configuration->{rsync_source} . " does not exist!"); die; } # This will prevent rsync from running if the target does not exist # i.e., a disk is not currently mounted. if ( $configuration->{rsync_target} =~ /^\// && !-e $configuration->{rsync_target} ) { log_message("Error: " . $configuration->{rsync_target} . " does not exist!"); die; } log_message("Running command: $rsync_command."); my $rsync_output = `$rsync_command`; log_message("rsync output:"); log_message($rsync_output); log_message("Backup complete."); my $endtime = time(); my $elapsed_time = $endtime - $starttime; log_message("Elapsed Time: $elapsed_time seconds."); sub fetch_config { my ($file) = @_; my %configuration = (); open(CONFIG,"$config_file") || die "Couldn't open $config_file for reading: $!\n"; while () { chomp; my ($key,$value) = split(/\|/); $configuration{lc($key)} = $value; } close(CONFIG); return(\%configuration); } sub log_message { my ($message) = @_; open(LOG,">>".$configuration->{logfile}."") || die "Couldn't open logfile " . $configuration->{logfile} . " for writing: $!\n"; print LOG scalar(localtime(time())) . " $message\n"; close(LOG); }