Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to keep a log of files that I am writing to tape using dd or tar for each file. Then at the end of the run, prepend that log to the beginning of the tape using dd. If it works, I can see the contents of any tape by reading the first few blocks, and then use "mt fsf" to position the tape at the specified file. The log would look something like:

1 file1.gz
2 file2.gz
3 ...

And maybe I would include a helpful example:

to restore the second file from the this list: mt -f /dev/nst0 fsf 2; tar xf /dev/nst0

I start the run by writing a blank space to the tape:

## prepare a placeholder at the beginning of the tape    
dd if=/dev/zero bs=32k count=1024 of=/dev/nst0

## loop through the files in the directory, writing them to tape and a log
for file in $(ls test_dir); do 
    tar cf /dev/nst0 $file &&
    echo $file >>process.log

## rewind the tape and prepend the process.log to the tape
mt -f /dev/nst0 rewi
(dd if=process.log bs=32k; dd if=/dev/zero bs=32k count=1024)|dd of=/dev/nst0 bs=32k conv=noerror,sync,block count=1024

## see if worked
mt -f /dev/nst0 rewi
dd if=/dev/nst0 of=process.log bs=32k
mt -f /dev/nst0 fsf 1
tar tf /dev/nst0

Unfortunately while I can read the log off the tape, I can't seem to pull any further data, producing the output below. If I don't try overwriting the first file with the process log, it produces the name of a file in the test directory.

tar: This does not look like a tar archive
tar: Error exit delayed from previous errors

Is there really no way around this? I know I can stat all the files and then put together a list of files that should fit onto the tape (based on known/estimated tape capacity), then just write that first; that's not nearly as clever.

share|improve this question
Who cares about clever? All I care about is recoverability. If clever makes it more unreliable, it's useless to me – Magellan Oct 7 '12 at 4:27
Have you tried putting an eof before the tar? – Zoredache Oct 7 '12 at 4:55
I agree, its not clever if its "more unreliable". I do think it's clever and more "reliable" if you can keep a log of files you actually put on tape, not just ones you hope to fit on tape. – worfly Oct 7 '12 at 14:40
okay, impossible (thanks to automatically written eod): – worfly Oct 7 '12 at 19:18

I think you can do it, but you can't stick a file at the beginning of a TAR archive just like that; they do have headers. I suspect that if you were to move the tar archive's current contents forward on disk and write another file (header and then data blocks) into the space thus freed, you could do it. Unless you have a way to move the beginning of the file without actually moving data, such an operation would be very expensive (equivalent to rewriting the entire tape each time).

Here is the GNU description of a TAR header.

Otherwise, forget about using TAR to write the files, and use DD, while managing the file allocations yourself somehow.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.