Home>Computers>How to read and write data from and to tapes in Solaris / Unix / Linux

How to read and write data from and to tapes in Solaris / Unix / Linux

I did not write this myself, This lovely howto was originally located at: http://www.mpifr-bonn.mpg.de/staff/hvoss/TapeHowto.html .. However since it was wiped clean off their website I decided to re-post everything in its entirety here. Thanks Hauke for writing such a wonderful how-to probably a very long time ago. To everyone else, enjoy 🙂

This How-To describes the commands to be used to write data on a tape and how to retrieve the data later. Additionally, some general remarks on how tapes work are given.

 

Short overview

How What
mt -f <Tape-device> rewind or
mt -f <Tape-device> asf <number> or
mt -f <Tape-device> eom
Wind tape to the start or
to tape-archive <number> or
to the end of written data
tar -cvf <Tape-device> <directory> or
tar -xvf <Tape-device>
Write directory to tape (MAY OVERWRITE DATA!) or
read one tape archive
mt -f <Tape-device> offline Unmount tape

Detailed description

How data is stored on a tape

Unlike harddiscs or CD/DVDs, tapes don’t have a directory structure you can view with the ls command. Data is stored subsequently in tape archives (tar). The first tape archive will start on the beginning of the tape, and where it ends, the second will follow and so on (see Fig. 1). So be aware: If you want to add data to the tape without overwriting existing one, the tape has to be winded to the end of the last tape archive stored on the tape.

Fig. 1: Scheme of tape archives stored on a tape (but see remark on numbering below!)

 

 


How to store data on a tape

All steps described below usually take some time – tapes are not the fastest type of storage. Better no’t be in a hurry 😉

  • Remove the write-protection from the tape.
    Most tapes have a slider or similar to physically write protect the tape. Slide this into write-enable position. For details have a look on the paper in the box of the tape – the write protection should be described there.
  • Put the tape into the drive. Find out the drive’s device-id and the name of the PC it is connected to.
    Typical device-id’s are /dev/dat or /dev/dlt. Solaris will be /dev/rmt/
  • Log in to the PC the drive is connected to.
    You may log in from a remote PC.
  • Wind the tape to the correct position
    Type the following command (where <device> is the device-id mentioned above) to…

    • …start writing at the beginning of the tape (OVERWRITING all data):

      mt -f <device> rewind

    • …start writing after the last tar (keeping all data written so far):

      mt -f <device> eom

    • …start writing after tar number X, where the first tar is number 0 (OVERWRITING all data following tar number X – but see remark on numbering below!):

      mt -f <device> asf <number>

      where <number> is the tar number of the first tar that will be overwritten (should be X + 1).

     

  • Write the data
    To write directory <directory> and all subdirectories to the tape, type

    tar -cvf <device> <directory>

    Repeat this step for each directory that you want to write. No need to wind the tape after each tar command, the tape will stay at the end of the last written tar, continuing there with the next issued tar command. For more tips on the tar command refer to the according man-pages.

  • Unmount the tape
    After writing all data you wanted to write, type

    mt -f <device> offline

    The tape will be rewinded and unloaded.


 

How to retrieve data from a tape

Again: All steps described below usually take some time.

  • Write-protect you tape!!!
    You should always write-protect the tape if you only want to read data to avoid any accidental data loss. This might happen if you simply type the wrong letter with the tar command (c instead of x), which happens faster as one should think. Or someone else writes to the tape by accident while you’re walking back to your desk.Most tapes have a slider or similar to physically write protect the tape. Slide this into write-protect position. For details have a look on the paper in the box of the tape – the write protection should be described there.
  • Put the tape into the drive. Find out the drive’s device-id and the name of the PC it is connected to.
    Typical device-id’s are /dev/dat or /dev/dlt.
  • Log in to the PC the drive is connected to.
    You may log in from a remote PC.
  • Wind the tape to the correct position
    Type the following command (where <device> is the device-id mentioned above) to…

    • …start reading at the beginning of the tape:

      mt -f <device> rewind

    • …start reading tar number X, where the first tar is number 0 (but see remark on numbering below!):

      mt -f <device> asf <number>

      where <number> is the tar number of the first tar that will be read.

     

  • Read the data
    Type

    tar -xvf <device>

    The contents of the tape archive the tape was winded to will be read and copied to the directory where you issued the tar command. The subdirectory structure will be kept. If you want to read subsequent tape archives, repeat this step for each tape archive. If the tar’s are not subsequent, use the previous step to wind the tape to the correct position.

  • Unmount the tape
    After reading all wanted data, type

    mt -f <device> offline

    The tape will be rewinded and unloaded.


 

Some remarks, tips and tricks

  • There is some confusion if the first tape archive on the tape is number 0 or number 1. So if you want to be sure how your tape handles ist, test it out. Or, if you can’t test ist, replace the command

    mt -f <device> asf <number>

    by the following two:

    mt -f <device> rewind
    mt -f <device> fsf <number>

    This will rewind the tape and then go forward by <number> tape archives.

  • As no ls command is possible to have a quick look what’s on your tape, one may write the list of all archived files into some textfile. There one later can look up quickly what was stored. To build the listfile, modify the tar command for writing your files as follows:

    tar -tvf <device> <directory> > <Listfile.txt>

    where <Listfile.txt> is the file that will contain the list of files stored in the tape archive. Generate a seperate listfile for each tape archive you build.

  • If you want to keep track of the amount of data you wrote to the tape, use the command

    du -h <directory>

    which will tell you the size of the directory. Perhaps it’s a good idea to write this into the listfile generated above.

  • Have a look on the mt command man pages for useful options.
  • If you want to do regular backups on tape, have a look here.

Good luck, and tell me if you’ve some more tips! Hauke Voß

5/5 - (1 vote)