#!/usr/bin/perl -w # # Script used to back up files to another # partition. # # Code by Brad Lhotsky <brad@divisionbyzero.net> # $|++; use strict; ########################### # Configs: # my $MOUNT = "yes"; my $PARTITION = "/dev/hdd1"; my $MOUNTPOINT = "/mnt/tmp"; my $BACKUPDIR = $MOUNTPOINT; my $CONFIG = "/root/backup/backup.conf"; my $ERRORFILE = "/root/backup/backup.err"; die "config file $CONFIG couldn't be found: $!\n" unless -e $CONFIG; my %TARS=(); unlink($ERRORFILE) if -e $ERRORFILE; if($MOUNT) { system("mount $PARTITION $MOUNTPOINT"); } open(CFG, "<$CONFIG") or die "couldn't open $CONFIG: $!\n"; while(local $_ = <CFG>) { chomp; s/^\s+//; s/\s+$//; next if /^#/; next if !$_; (local $_, my @JUNK) = split /#/; s/^\s+//; s/\s+$//; my ($tarfile,$directory,@EXCLUDES) = split; push @EXCLUDES,'..'; push @EXCLUDES,'.'; my $ok = &backup($tarfile,$directory,\@EXCLUDES); if($ok) { print "Tarred $tarfile successfully! ($directory)\n"; $TARS{$tarfile}=1; } else { print "Tar of $directory to $tarfile failed!\n"; } } foreach my $tarfile (keys %TARS) { &compress($tarfile); } if($MOUNT) { system("umount $PARTITION"); } sub backup { my ($tarfile,$dir,$exc) = @_; return 0 unless $tarfile && $dir && $exc; if($dir !~ /\/$/) { $dir .= '/'; } if($tarfile !~ /^\//) { $tarfile = $BACKUPDIR . "/$tarfile"; } my $check = "$dir/.tar"; return 0 unless -e $check; if(-e $tarfile) { return 0 unless -d $dir; opendir(LS, $dir) or die "couldn't open $dir for listi +ng: $!\n"; while(local $_ = readdir(LS)) { #chomp; my $skip=0; foreach my $file (@$exc) { $skip=1, last if ($_ eq $file); } next if $skip; my $file = $dir . $_; my $append = "tar -rf $tarfile $file 2>> $ERRO +RFILE"; system($append); print "\t$file\n"; } print "$tarfile appended to!\n"; } else { my $create = "tar -cf $tarfile $check 2>> $ERRORFILE"; print "$tarfile created!\n"; system($create); &backup($tarfile,$dir,$exc); } } sub compress { my $tarfile = shift; if($tarfile !~ /^\//) { $tarfile = $BACKUPDIR . "/$tarfile"; } my $gzipit = 'gzip -f ' . $tarfile . '.gz ' . $tarfile . " 2>> + $ERRORFILE"; print "GZipping $tarfile .... "; system($gzipit); print "done!\n"; }
In reply to Evolution of a Perl Programmer (new backup script) by reyjrar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |