#!/usr/bin/perl -Tw use strict; $ENV{'PATH'}='/usr/bin:/usr/local/bin:/bin'; my(%backdirs); my($EmailAddr,$EmailFile); my($DOW, $DOM, $YMD, $FDATE); my($TarDir, $BackupFile); my($SizeBefore, $SizeAfter, $Size, $Compression); $EmailAddr = 'user@host.com'; $TarDir = '/home/backup'; # name- daily/weekly/monthly - Compress (yes,no) - Directory $backdirs{"etc"}{"daily"}{"yes"} = "/etc"; $backdirs{"home"}{"weekly"}{"yes"} = "/home"; $backdirs{"home2"}{"weekly"}{"yes"} = "/home2"; $backdirs{"bin"}{"monthly"}{"yes"} = "/usr/local/bin"; ############################################################### ############################################################### ##End User Configuration # ############################################################### ############################################################### $EmailFile = "/tmp/email.$$"; open(EMF,">>$EmailFile"); sub UnTaint($) { return(($_[0] =~ /^(.*)$/)[0]); } $DOW = `date +%w`; $DOM = `date +%d`; $YMD = `date +%Y%m%d`; chomp($DOW); chomp($DOM); chomp($YMD); open(CMD, "ps ax | grep -v grep | grep -c saveit.pl |"); if ( > 1 ) { die("Backup Process Already Running"); } close CMD; foreach my $name (keys %backdirs) { foreach my $timeframe (keys %{$backdirs{"$name"}}) { foreach my $compression (keys %{$backdirs{"$name"}{"$timeframe"}}) { $FDATE = `date`; chomp($FDATE); my $dir = $backdirs{"$name"}{"$timeframe"}{"$compression"}; if($timeframe =~ /daily/ || ($timeframe =~ /weekly/ && $DOW =~/^1$/) || ($timeframe =~ /monthly/ && $DOM =~/^1$/)) { my $LogFile = UnTaint("$TarDir/$timeframe/$name-$YMD.log"); if("$compression" eq "yes") { $BackupFile = UnTaint("$TarDir/$timeframe/$name-$YMD.tar.bz2"); } else { $BackupFile = UnTaint("$TarDir/$timeframe/$name-$YMD.tar"); } print EMF "\n"; print EMF "Running $name as $timeframe\n"; print EMF "Backing Up $dir\n"; print EMF "Log file is $LogFile\n"; print EMF "Backup to $BackupFile of $name started at $FDATE\n"; if("$compression" eq "yes") { system("/bin/tar -cjvvf $BackupFile $dir > $LogFile 2>>$EmailFile"); open(SIZEBEFORE, "du -sm $dir | cut -f1 |"); $SizeBefore = ; chomp($SizeBefore); open(SIZEAFTER, "du -sm $BackupFile | cut -f1 |"); $SizeAfter = ; chomp($SizeAfter); print EMF "\t $SizeBefore MB (before compression)\n"; print EMF "\t $SizeAfter MB (after compression)\n"; close SIZEBEFORE; close SIZEAFTER; $Compression = (($SizeBefore - $SizeAfter) / $SizeBefore)*100; printf EMF "Compression Ratio: %2.2f%%\n", $Compression; } else #Compression Not Desired { system("/bin/tar -cvvf $BackupFile $dir > $LogFile 2>$EmailFile"); open(SIZE, "du -sm $BackupFile | cut -f1 |"); $Size= ; chomp($Size); print EMF "\t $Size MB (no compression desired)\n"; close SIZE; } } } } } if(-s $EmailFile) { print EMF "\n\n"; print EMF `df -h`; print EMF "\n\n\tThe Backup Script\n"; system(UnTaint("cat $EmailFile | mail -s \"Backup for $YMD\" $EmailAddr")); } close EMF; system("rm -f $EmailFile");