#!/usr/bin/perl
# Written by Michael Howe, in a desparate attempt to get tar to backup a 63GB
# partition onto a filesystem that has a maximum size of 4GB (FAT32, we hate
# you).
# Usage:
#   Symlink 'file.tar' to 'file.00.tar'.  Set $filename and $dir appropriately.
#   Run tar:
#   tar -tvM -F /path/to/script.pl -f ./file.tar # lists
#   tar -xvM -F /path/to/script.pl -f ./file.tar # extracts
#
#   NOTE: you should remove (or relink) file.tar after each operation, as it
#   will be left pointing to the last file in the archive.
#
# IMPORTANT:
#   This may well eat your data, and could also fry your hardware, or (worst
#   case) lead to an alien invasion.  I claim no responsibility - use at your
#   own risk (and remember to backup).

use strict;
use warnings;

my $filename = "src";
my $dir = "/tmp/tartest";
my ( $cur, $num, $next );

if ( -l "$dir/$filename.tar" ) {
    $cur = readlink( "$dir/$filename.tar" );
    ( $num ) = $cur =~ m{.*$filename\.(\d+).*};
    $num++;
    unlink( "$dir/$filename.tar" ) or die "Can't unlink $dir/$filename.tar: $!";
} else {
    $num = 0;
}
$next = sprintf( "%s/%s.%02d.tar", $dir, $filename, $num );
print "Continuing with archive $num\n";
symlink( $next, "$dir/$filename.tar" ) && exit 0;

