Tuesday, February 14, 2012

RedHat /CentOS Tapes MT command

Working with "mt" Commands: reading and writing to tape.

    The following assumes the tape device is "/dev/st0"

    STEP 1 ( rewind the tape)

         # mt -f /dev/nst0 rewind

    STEP 2 (check to see if you are at block 0)

         # mt -f /dev/nst0 tell
           At block 0.

    STEP 3 (Backup "tar compress"  directories "today"  and "etc")

         # tar -czf /dev/nst0 today  etc

    STEP 4 (Check to see what block you are at)

          # mt -f /dev/nst0 tell

      You should get something like block 2 at this point.

    STEP 5 (Rewind the tape)

          # mt -f /dev/nst0 rewind

    STEP 6 (List the files)

          # tar -tzf /dev/nst0
             today/
             etc/
             
    STEP 7 (Restore directory "one"  into directory "junk").  Note, you
         have to first rewind the tape, since the last operation moved
         ahead 2 blocks. Check this with "mt -f /dev/nst0".

          # cd junk
          # mt -f /dev/nst0 rewind
          # mt -f /dev/nst0 tell
             At block 0.
          # tar -xzf /dev/nst0 today

    STEP 8 (Next, take a look to see what block the tape is at)

          # mt -f /dev/nst0 tell
             At block 2.

    STEP 9 (Now backup directories three  and four)

          # tar -czf /dev/nst0 kd6w2 kd6mtf

      After backing up the files, the tape should be past block 2.
      Check this.

          # mt -f /dev/nst0 tell
            At block 4.

         Currently the following exist:

               At block 1:
                    today/
                   etc/
                   kd6w2/

               At block 2:
                   kd5mtf
 
                 At block 4:
                   (* This is empty *)

    A few notes. You can set the blocking factor and a label
    with tar. 

     $ tar --label="temp label" --create  --blocking-factor=128 --file=/dev/nst0 Notes

    But note if you try to read it with the default, incorrect blocking
    factor, then, you will get the following error:

       $ tar -t   --file=/dev/nst0
       tar: /dev/nst0: Cannot read: Cannot allocate memory
       tar: At beginning of tape, quitting now
       tar: Error is not recoverable: exiting now

    However this is easily fixed with the correct blocking factor

        $ mt -f /dev/nst0 rewind
        $ tar -t --blocking-factor=128 --file=/dev/nst0
        workingdir testarea
        conf.txt

    Take advantage of the label command.

        $ MYCOMMENTS="tape"
        $ tar --label="$(date +%F)"+"${MYCOMMENTS}"

    Writing to tape on a remote 192.168.56.5 computer

        $ tar cvzf - ./tmp | ssh -l rajat 192.168.56.5 '(mt -f /dev/nst0 rewind; dd of=/dev/st0 )'

    Restoring the contents from tape on a remote computer

        $ ssh -l rajat 192.168.56.5 '(mt -f /dev/nst0 rewind; dd if=/dev/st0  )'|tar xzf -

    Getting data off of tape with dd command with odd blocking factor. Just set ibs very high

        $ mt -f /dev/nst0 rewind
        $ tar --label="Contenets of Notes" --create  --blocking-factor=128 --file=/dev/nst0 Notes
        $ mt -f /dev/nst0 rewind
        $ dd ibs=1048576 if=/dev/st0 of=notes.tar

    The above will probably work with ibs=64k as well

4 comments:

Roger Pack said...

how to partition? :)

Unknown said...

If you see "MT" man page you can find under.

setpartition
(SCSI tapes) Switch to the partition determined by count. The default data partition of the tape is numbered zero. Switching partition is available only if enabled for the device, the device supports multiple partitions, and the tape is formatted with multiple partitions.
partseek
(SCSI tapes) The tape position is set to block count in the partition given by the argument after count. The default partition is zero.
mkpartition
(SCSI tapes) Format the tape with one (count is zero) or two partitions (count gives the size of the second partition in megabytes). The tape drive must be able to format partitioned tapes with initiator-specified partition size and partition support must be enabled for the drive.

Unknown said...

Hello there. Your posts and share are great. I got something in my head. st0 while the command line was why nst0. Thanks

Unknown said...

Hello there. Your posts and share are great. I got something in my head. st0 while the command line was why nst0. Thanks