BackupPC-users

[BackupPC-users] Routine for local copying of individual backups

2009-11-02 14:22:38
Subject: [BackupPC-users] Routine for local copying of individual backups
From: "Jeffrey J. Kosowsky" <backuppc AT kosowsky DOT org>
To: General list for user discussion <backuppc-users AT lists.sourceforge DOT net>
Date: Mon, 02 Nov 2009 14:19:25 -0500
The following routine can be used to make local (same filesystem)
copies of individual BackupPC backups.

This is useful if you want to save a specific backup number but still
allow regular expiry of the original version. Just make a copy of the
backup that you want to saved to a different location or name on the
same filesystem and then it won't be expired and the original will
expire away as desired.

The program preserves hard links while copying files in the original
backup that are not linked (mostly zero length files anyway. As a
result, such local copying doesn't take much additional space and is
quite fast.

-----------------------------------------------------------------------

#!/usr/bin/perl
# Copy over backup locally - preserving hard links
# Usage: BackupPC_cplocal.pl <source dir> <target dir>
# The routine copies the source dir *into* the target dir by:
# - Creating directories (same permissions)
# - Hard linking to files with nlinks >=1 
# - Copying over files with nlinks=1 preserving all attributes

use Cwd;
use File::Find;

my ($source, $target, $dir, $base);
die "Error: Source '$source' is not a directory...\n" unless -d ($source = 
$ARGV[0]);
die "Error: Target '$target' is not a directory...\n" unless -d ($target = 
$ARGV[1]);

`df $source` =~ m|(^/dev[^ \t\n]*)|m;
my $sourcedev=$1;
`df $target` =~ m|(^/dev[^ \t\n]*)|m;
my $targetdev=$1;
die "Error: Source and target aren't on same device...\n" unless $sourcedev eq 
$targetdev;

$source =~ s|/*$||;
$source = getcwd() . "/" . $source unless $source =~ m|^/|;
$target =~ s|/*$|/|;
$target = getcwd() . "/" . $target unless $target =~ m|^/|;

($base = $source) =~ s|^.*/||;
die "Source '$base' already exists in '$target'\n" if -e "$target/$base";

chdir $source . "/..";
find({wanted => \&copy, no_chdir => 1}, ($base));

sub copy
{
        if(-d $_) {
                warn "Error: Couldn't create directory: $target$_\n" unless
                        mkdir("$target$_", 0750); #Make directory tree
        }
        else {
                my $numlinks = (stat($_))[3];
                if($numlinks > 1) { #Link if already linked
                warn "Error: Couldn't create link: $target$_\n" unless
                        link($_, $target . $_ );
                }
                else { #Otherwise copy
                #Note: Don't use perl File::Copy because doesn't preserve 
attributes
                        system('cp', '-cdpP', $_, $target . $_);
                }
        }
}

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
BackupPC-users mailing list
BackupPC-users AT lists.sourceforge DOT net
List:    https://lists.sourceforge.net/lists/listinfo/backuppc-users
Wiki:    http://backuppc.wiki.sourceforge.net
Project: http://backuppc.sourceforge.net/

<Prev in Thread] Current Thread [Next in Thread>
  • [BackupPC-users] Routine for local copying of individual backups, Jeffrey J. Kosowsky <=