#!/usr/bin/perl -w use strict; # Don't allow any funny stuff. Perl is confusing enough without it. use Cwd; # bu2: incremental backup script # This script will create a list of files to incrementally back up, based on a backup list normally used for full # backups. Dates of each file found in the backup list is compared with the date of the last full backup, to # determine whether it should be included. The name of the incremental backup filelist can be specified, or a # default filename can be used. if ($ENV{HOME} eq "/home/Peter") { # I always back up from home directory, chdir("/home/Peter"); # so go there, because } # tar paths will be relative to home. my $base = cwd(); # $base will have current working directory. my $currentfile; # holds current file to examine my $bu2_mtime; # Process any command line parameters sub backuplist { # Subroutine called check files & directories my $currentfile = shift; # file or subdirectory to check, relative to $base my $tempdir = $base . "/" . $currentfile; # full path from root to current file or directory my $nextdir; # for next path level, if needed my $entry; # an entry from the list # here, check whether it's a directory or not. if (-d $tempdir) { # continue here only if looking at a directory chdir ($tempdir) || die "I can't get to $tempdir\n$!"; # report any error changing directory my @backuplist = <*>; # get all directory entries to an array foreach (@backuplist) { # go through the list and check for valid files $entry = $_; # get an entry in the list next if (-l $entry); # ignore links if (-d $entry) { # if the entry is a directory, $nextdir = $currentfile . "/" . $entry; # $nexdir will simulate current address off of $base &backuplist($nextdir); # go check this directory chdir ($tempdir) || die "I can't return to $tempdir\n"; # this gets me back to where I was. } else { # otherwise it's a file so check it. if ((-M $entry) < $bu2_mtime) { # if the file was modified after this script... print OUT $currentfile . "/" . $entry,"\n"; # put the file and relative path into backup list } } } } else { # end up here if it's a file, not a directory. if ((-M $tempdir) < $bu2_mtime) { # check age of file relative to last full backup print OUT $currentfile,"\n"; # put the file into backup list } } } # process any command line arguments when this script is called. my $infile = shift(@ARGV); # declare some variables and my $outfile = shift(@ARGV); # pick up any command line arguments my $tarfilename = shift(@ARGV); if (not defined($infile)) { # set defaults if none were provided $infile = "bin/backuplist"; # modify defaults for your own use } if (not defined($outfile)) { $outfile = "bin/incbackuplist"; } if (not defined($tarfilename)) { $tarfilename = "incbackup"; } if (not($infile =~ /^\//)) { # if path isn't absolute, make it absolute! $infile = $base . "/" . $infile; # prepend current working directory to input filename } if (not($outfile =~ /^\//)) { # if path isn't absolute, make it absolute! $outfile = $base . "/" . $outfile; # prepend current working directory to output filename } print "input file is ",$infile,"\n"; # confirm file names print "output file is ",$outfile,"\n"; # open(OUT, ">$outfile"); # Open the output file for writing open(IN, "<$infile"); # open the intput file for reading $bu2_mtime = `which bu2`; # Get path to this script chomp($bu2_mtime); # get rid of the newline... $bu2_mtime = (-M $bu2_mtime); # Get the date (mtime) of last full backup print "Last full backup was done ",$bu2_mtime," days ago.\n"; # Report number of days since this script last modified. while () { # While there are lines left to read, $currentfile = $_; # Read in lines from the backup file list chomp($currentfile); # lop off that newline &backuplist($currentfile); # go check the file or directory for backing up } close(IN) || die "Can't close input file!\n"; # close the file I read in. close(OUT) || die "Can't close output file!\n"; # close the file I created. chdir $base || die "I can't get to $base\n$!"; # make sure we end up at the current working directory. print "Review $outfile to see which files will be backed up.\n";# system "backmeup",$tarfilename,$outfile,"inc"; # invoke tar and create the backup.