Child pages
  • How to recover data from a corrupted .tar.bz2 file?

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

\

#

\

!/usr/bin/perl

\

-w


use

strict;

#

  1. 99.9%
  1. of
  1. all
  1. credits
  1. for
  1. this
  1. script
  1. go
#
  1. to
  1. Tore
  1. Skjellnes
  1. <torsk@elkraft.ntnu.no>
#
  1. who
  1. is
  1. the
  1. originator.

my

$tarfile;


my

$c;


my

$hit;


my

$header;

#

  1. if
  1. you
  1. don't
  1. get
  1. any
  1. results,
  1. outcomment
  1. the
  1. line
  1. below
  1. and
#
  1. decomment
  1. the
  1. line
  1. below
  1. the
  1. it
  1. and
  1. retry

  1. my
  1. @src
  1. =
  1. (ord('u'),ord('s'),ord('t'),ord('a'),ord('r'),ord("
  1. "),
  1. ord("
  1. "),0);
\

  1. #my
  1. @src
  1. =
  1. (ord('u'),ord('s'),ord('t'),ord('a'),ord('r'),0,ord('0'),ord('0'));

die

"No

tar

file

given

on

command

line"

if

$#ARGV

\

!=

0;

Panel
titlefindtarheader.pl
Wiki Markup
Wiki Markup
$tarfile = $ARGV\[0\];

open(IN,$tarfile)

or

die

"Could

not

open

`$tarfile':

$

\

!";

$hit

=

0;


$

\

|

=

1;


seek(IN,257,0)

or

die

"Could

not

seek

forward

257

characters

in

`$tarfile':

$

\

!";


while

(read(IN,$c,1)

==

1)

{

{

Wiki Markup
($hit = 0, next) unless (ord($c) == $src\[$hit\]);     $hit = $hit + 1;


( print "hit: $hit", next ) unless $hit > $#src;       # we have a probable header at (pos - 265)\!


my $pos = tell(IN) - 265;


seek(IN,$pos,0) 	or (warn "Could not seek to position $pos in `$tarfile': $\!", next);


(read(IN,$header,512) == 512) 	or (warn "Could not read 512 byte header at position $pos in `$tarfile': $\!", seek(IN,$pos+265,0),next);


my ($name, $mode, $uid, $gid, $size, $mtime, $chksum, $typeflag, 	$linkname, $magic, $version, $uname, $gname, 	$devmajor, $devminor, $prefix) 	= unpack ("Z100a8a8a8Z12a12a8a1a100a6a2a32a32a8a8Z155", $header);


$size = int $size;


printf("%s:%s:%s:%s\n",$tarfile,($pos+1),$name,$size);


$hit = 0;


}

close(IN)

or

warn

"Error

closing

`$tarfile':

$

\

!";

Yeah, copy/paste and save it. chmod +x on it.
Now, to find the first clean tar header on recovery2_failing.tar, do the following:

...

Code Block
./findtarheader.pl recovery2_failing.tar \| head \-n 1

to get only the first occurence of a tar header.
You get something like :
recovery2_failing.tar:17185:naked_girls/pamela.jpg:157106

...