cute, quick hack I did to backup some directories on my server. There's probly a nicer way to do this, but this one works :)
#!/usr/bin/perl $CONF_FILE="backup.conf"; $MOUNT_BACKUP_HD=""; # PLEASE set this HD up in your fstab if you use +it if($MOUNT_BACKUP_HD) { system("mount $MOUNT_BACKUP_HD"); } open(CONF, "< $CONF_FILE") || die "no config file found!\n"; $i=0; while(<CONF>) { # Parse out comments. if(substr($_, 0,1) eq '#') { next; } my @tmp = split(/\#/,$_); $_ = $tmp[0]; chomp; # Alright, ready to split. @line = split / /; $tarfile = shift @line; $array_tar[$i] = $tarfile; $i++; $dir = shift @line; $lref = \@line; backup($tarfile,$dir,$lref); } $tref = \@array_tar; gzip($tref); if($MOUNT_BACKUP_HD) { system("umount $MOUNT_BACKUP_HD"); } sub backup { $tarfile = $_[0]; $dir = $_[1]; $exc = $_[2]; print "****: ",$tarfile, " ", $dir; foreach $file (@$exc) { print " ",$file; } print "\n"; $checkfile = $dir . '.tar'; if(-e $tarfile && -e $tarfile) { open(LIST, "ls -1 $dir|"); while(<LIST>){ chomp; split / /; $_ = $_[0]; $skip = 0; foreach $file (@$exc) { if($_ eq $file) { $skip = 'yes'; } } if(!$skip) { $tarit = "tar -rf $tarfile " . $dir . $_; print $tarit . "\n"; system($tarit); } } } elsif(-e $checkfile){ $tarthis = "tar -cf $tarfile $checkfile"; print $tarthis,"\n"; system $tarthis; backup($tarfile, $dir, $exc); } } sub gzip { $dir = $_[0]; $i=0; foreach $tar (@$dir) { $skip=0; foreach $check (@blah) { if( $tar eq $check ) { $skip = 1;} } if(!$skip) { $blah[$i] = $tar; $i++;} } foreach $tarfile (@blah) { $gzipit = 'gzip -f ' . $tarfile . '.gz ' . $tarfile; print $gzipit,"\n"; system($gzipit); } } #End backup.pl Example conf file. ############################################# # Config file example: backup.conf # # /tar/file.tar /directory/path/ excluded_file excluded_directory # # You can have as many files/dir excluded as you wish.. /tmp/web.tar /www/htdocs/ spark realms noc doc /tmp/web.tar /www/htdocs/spark/ noc /tmp/web.tar /www/htdocs/realms/ /tmp/trilex.tar /home/trilex/code/ /tmp/talker.tar /home/trilex/public_html/

Replies are listed 'Best First'.
RE: backup.pl
by jjhorner (Hermit) on Jun 20, 2000 at 19:01 UTC

    You would get -- from me (if I gave them out for anything less than catastrophic errors) for a few reasons:

    • 'use strict'
    • 'use warnings' or '-w'
    • error checking
    • If merlyn sees this, he is sure to warn you about the potential infinite loops because of links.
    • File::Find is your friend.

    You should start using them now, or you will be in trouble later.

    Start looking into CPAN for the modules available to save you time and energy.

    All in all, halfway decent code, and will be pretty good once you get a little more experience. We aren't all merlyn or vroom.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
RE: backup.pl
by le (Friar) on Jun 20, 2000 at 11:37 UTC
    I think weeding out comments is faster if you do:
    next if /^#/;
      Yes, but what if you accidently had a whitespace or somthing before that comment? I accidently do that all the time, this is why it would even be better to use
      next if /^\s*\#/;
      -cleen