I wrote this script to tell me what files seemed likely to need
backing up.  It doesn't work nearly as well as I'd like, mostly
because dpkg doesn't manage all the files I wish it did.

Here's the rationale:

To make my laptop easy to back up, I will segregate things that need
backing up and things that don't; the rules for determining will be
as follows, in order of priority:
- any RCS (*,v) files need to be backed up, because I edit them on that system.
- anything in or under a directory named 'pkgs' doesn't need to be
  backed up, because it's a publicly available package I can copy from
  somewhere else; but backing it up might be a useful thing to do to
  save time redownloading.
- likewise for 'rcvs', but it's not publicly available --- it's a foreign
  CVS repository.
- anything dpkg -S knows about doesn't need to be backed up, because
  it came from a Debian package (which is presumably either backed up or
  publicly available)
- anything in or under a directory named 'tmp' doesn't need to be backed
  up because it's a temporary file, and absolutely shouldn't, because
  it can get arbitrarily large
- anything else needs to be backed up

#!/usr/bin/perl -w
use strict;
# produce a list of files to back up.  
use vars '$name', '&find';

require "find.pl";

open DPKGPIPE, "dpkg -S '/*'|" or die "Can't fork: $!\n";
my @dpkgfiles = <DPKGPIPE>;
close DPKGPIPE or die "running dpkg failed: $!";

chomp @dpkgfiles;
s/.*?: // for @dpkgfiles;  # remove package names, leaving filenames

my %dpkgfiles;
@dpkgfiles{@dpkgfiles} = ();

my $rootdev;

sub backupable {
  my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
  return 0 if not defined $dev;  # lstat fails
  $rootdev = $dev if not defined $rootdev;
  return 0 if $rootdev ne $dev;  # something other than the root filesystem
  return 1 if /,v$/;
  return 0 if ($name =~ m|/pkgs/| or $name =~ m|/rcvs/| or 
               exists $dpkgfiles{$name} or $name =~ m|/tmp/|);
  return 1;
}

sub wanted {
  my $dobackup = backupable();
  print "$name\n" if $dobackup;
  return $dobackup;
}

&find('/');

__END__


Reply via email to