#!/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.
# Usage:
#   Change $filename and $dir to the base filename (ie without .tar) and
#   directory of the tar files.
#   Then, use tar:
#   tar -cvvM -L4194200 -F /path/to/this/script.pl -f /path/to/tar/file.tar /dir/to/tar
#   tar will create 'file.tar', then when it gets to just under 4G, will call
#   this script, which will move file.tar to file.00.tar, and start again on
#   file.tar.
#   Upon completion of the tar command, move file.tar to be the last in the
#   series (ie file.01.tar (if the only other file is file.00.tar)).
#
# 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;
use File::Copy;

my $filename = "src";
my $dir = "/tmp/tartest";

my $latest = `ls -1 $dir/$filename.*.tar 2>/dev/null| sort | tail -1`;

my $newname;
print "Latest: $latest\n";
if ( $latest ) {
	my ( $number ) = $latest =~ m{.*\.(\d+)\.tar};
	$number++;
	print "Number: $number\n";
	$newname = sprintf( "%s/%s.%02d.tar", $dir, $filename, $number );
	( -f $newname ) && die "File $newname exists - script screwed!";
} else {
	$newname = "$dir/$filename.00.tar";
}


move( "$dir/$filename.tar", $newname );
( -f $newname ) and exit 0;

