eval { Carp::confess("init") }; $SIG{__DIE__ } = sub { Carp::confess }; #### =head1 NAME backup.pl - Yet Another Script for making backups =head1 SYNOPSIS backup.pl --bakdir=s --backup=s [--ignorefile=s] [--debug=i] Options: --bakdir - where to look for and store backup files --backup - what directory to backup --ignorefile - file which lists what subdirs not to backup --debug - print debug information =cut use strict; use warnings; use English; use Getopt::Long; use Pod::Usage; use Archive::Tar; use File::Path; use File::Find; use File::Glob ':glob'; use Carp ( ); eval { Carp::confess("init") }; $SIG{__DIE__ } = sub { Carp::confess }; my $debug = 0; my $bakdir = ''; my $backup = ''; my $ignorefile = ''; GetOptions( 'debug=i' => \$debug, 'bakdir=s' => \$bakdir, 'backup=s' => \$backup, 'ignorefile=s' => \$ignorefile, ); eval {mkpath($bakdir)}; if ($@) { warn "Unable to find or create directory $bakdir\n$@\n"; pod2usage(1); exit; } # process the ignore file list my @ignorelist; if ($ignorefile ne '') { open(IGN, $ignorefile) || die "Unable to open $ignorefile for reading: $!\n"; while () { chomp; my @globlist = bsd_glob($_, GLOB_TILDE | GLOB_ERR); if (GLOB_ERROR) { warn "ignorefile entry '$_' produced and error!\n"; } foreach (@globlist) { push @ignorelist, $_; } } close(IGN); } # create a tar.gz archive with a unique filename my @t = reverse((localtime)[0..5]); $t[0]+=1900; $t[1]++; my $newbackup = $bakdir.'/'.sprintf("%4u-%02u-%02u-%02u-%02u-%02u",@t).'.tar.gz'; my $tar = Archive::Tar->new(); find({ wanted => \&add_to_archive, follow=>1, follow_skip=>2, no_chdir=>1}, $backup); $tar->write($newbackup, 9); sub add_to_archive { my $ignore = 0; foreach my $ignoreentry (@ignorelist) { if ($File::Find::name eq $ignoreentry) { print "IGNORED == match!!! " if ($debug >= 1); $ignore++; } if ($File::Find::name =~ /^$ignoreentry\//) { print "IGNORED dir match!!! " if ($debug >= 1); $ignore++; $File::Find::prune = 1; } } print "Added " if (!$ignore && ($debug >= 1)); print "$File::Find::name\n" if ($debug >= 1); return if $ignore; $tar->add_files($File::Find::name); }