#!/usr/bin/perl -w =head1 NAME backup.pl - Yet Another Script for making backups =head1 SYNOPSIS backup.pl --bakdir=s --backup=s [--ignorefile=s] Options: --bakdir - where to look for and store backup files --backup - what directory to backup --ignorefile - file which lists what subdirs not to backup =cut use strict; use warnings; use English; use Getopt::Long; use Pod::Usage; use File::Path; my $bakdir = ''; my $backup = ''; my $ignorefile = ''; GetOptions( 'bakdir=s' => \$bakdir, 'backup=s' => \$backup, 'ignorefile=s' => \$ignorefile, ); $bakdir ||= "."; $backup ||= "."; if ( $bakdir eq $backup ) { warn "We should not create a backup of $backup in $bakdir\n"; pod2usage(1); exit; } eval {mkpath($bakdir)}; if ($@) { warn "Unable to find or create directory $bakdir\n$@\n"; pod2usage(1); exit; } # create a tar.gz archive with a unique filename my @t = reverse((localtime)[0..5]); $t[0]+=1900; $t[1]++; my $t = sprintf("%4u-%02u-%02u-%02u-%02u-%02u",@t); my $newbackup = "$bakdir/$t.tar.gz"; my @cmd = qw/tar cz/; push @cmd, '-X', $ignorefile if ( length( $ignorefile ) and -f $ignorefile and -r _ ); push @cmd, '-f', $newbackup, $backup; exec @cmd;