15 practical Linux/Unix tape management commands for the system administrator

Source: Internet
Author: User
Tags rewind

15 practical Linux/Unix tape management commands for the system administrator

Tape equipment should only be used for regular file archiving or data is transferred from one server to another. Generally, tape devices are connected to Unix machines and controlled by mt or MTCs. We strongly recommend that you back up all data to disks (maybe in the cloud) and tape devices at the same time. In this tutorial, you will learn:

  • Tape device name
  • Basic commands for managing tape drives
  • Basic backup and recovery commands


 

Why backup?

A backup plan is necessary for regular backup of files. If you prefer not to back up files, the risk of losing important data is greatly increased. With backup, you can recover from a disk failure. Backup can also help you resist:

  • Accidental File Deletion
  • File or file system corruption
  • Full server destruction, including destruction of the same disk backup due to fire or other problems
  • Hard disk or SSD crash
  • Virus or ransomware destroys or deletes files

You can use tape archive to back up the entire server and store it offline.

 

Understanding tape file tags and block sizes


Figure 01: tape file tag

Each tape device can store multiple backup files. The tape backup file is created using commands such as cpio, tar, and dd. At the same time, tape devices can be opened, written, and closed by a variety of programs. You can store several backups (Tape files) to a physical tape. There is a "tape file tag" between each tape file ". This indicates the end of a tape file on a physical tape and the start of another file. You need to use the mt command to locate the tape (fast forward, rewind, and tag ).

 

How is data stored on tape?


Figure 02: how the data on the tape is stored

All data is stored continuously in tar continuous tape storage format. The first tape archive starts from the physical start of the tape (tar #0 ). The following is tar #1, and so on.

 

Unix tape device name
  1. /Dev/rmt/0 or/dev/rmt/1 or/dev/rmt/[0-127]: name of a regular tape device on Unix. The tape is automatically reversed.
  2. /Dev/rmt/0n: it is characterized by no reverse playback. In other words, after the tape is used, it stays in the current state and waits for the next command.
  3. /Dev/rmt/0b: use the tape interface, that is, the behavior of BSD. Various types of operating systems such as AIX, Windows, Linux, and FreeBSD are more readable.
  4. /Dev/rmt/0l: Set the density to low.
  5. /Dev/rmt/0 m: Set the density to medium.
  6. /Dev/rmt/0u: Set the density to high.
  7. /Dev/rmt/0c: Set the density to compression.
  8. /Dev/st [0-9]: Linux-specific SCSI tape device name.
  9. /Dev/sa [0-9]: FreeBSD-specific SCSI tape device name.
  10. /Dev/esa0: the name of the SCSI tape device specified by FreeBSD. It is displayed when it is disabled (if possible ).

 

Tape device name example
  • /Dev/rmt/1cn indicates that unity 1 is being used, and the compression density is not reversed.
  • /Dev/rmt/0hb indicates that unity 0, high density, and BSD are being used.
  • On Linux, the device name of the SCSI tape is/dev/st0.
  • Linux SCSI tape device name:/dev/nst0
  • The name of the SCSI tape device on FreeBSD is/dev/sa0.
  • No reverse SCSI tape device name on FreeBSD:/dev/NSAID 0

 

How do I List Installed scsi tape devices?

Enter the following command:

###Linux (see man for more information)###
Lsscsi
Lsscsi -g
### IBM AIX ###
lsdev -Cc tape
lsdev -Cc adsm
lscfg -vl rmt*
### Solaris Unix ###
Cfgadm – a
Cfgadm -al
Luxadm probe
Iostat -En
### HP-UX Unix ###
Ioscan Cf
ioscan -funC tape
ioscan -fnC tape
ioscan -kfC tape

Output example from my Linux Server:


Figure 03: tape devices installed on Linux servers

 

Mt command example

On Linux and Unix-like systems, the mt command is used to control operations on the tape drive, such as viewing the status or searching for files on the tape or writing the tape control flag. Most of the following commands must be executed as root users. Syntax:

mt -f /tape/device/name operation

 

Set Environment

You can set the TAPE shell variable. This is the path name of the tape drive. On FreeBSD, the default value (if the variable is not set, rather than null) is/dev/NSAID 0. You can use the-f parameter of the mt command to pass the variable to overwrite it, as described below.

###Add to your shell profile###
TAPE=/dev/st1 #Linux
TAPE=/dev/rmt/2 #Unix
TAPE=/dev/nsa3 #FreeBSD
Export TAPE

 

1: Display tape/drive status
mt status  ### Use default
mt -f /dev/rmt/0  status ### Unix
mt -f /dev/st0 status ### Linux
mt -f /dev/nsa0 status ### FreeBSD
MT - F / dev / RMT / 1 Status - UNIX unit 1, i.e. tape device No. 1

You can use a shell loop statement to traverse a system and locate all its tape drives as follows:

for d in 0 1 2 3 4 5
do
 mt -f "/dev/rmt/${d}" status
done

 

2: rewind
mt rew
mt rewind
mt -f /dev/mt/0 rewind
mt -f /dev/st0 rewind

 

3: pop-up tape
mt off
mt offline
mt eject
mt -f /dev/mt/0 off
mt -f /dev/st0 eject

 

4: erase the tape (rewind, unmount the tape if supported)
mt erase
mt -f /dev/st0 erase  #Linux
mt -f /dev/rmt/0 erase #Unix

 

5: tensioning the tape cartridge

If an error occurs while reading the tape, re-tensioning the tape, clean the tape drive, and try again like below:

mt retension
mt -f /dev/rmt/1 retension #Unix
mt -f /dev/st0 retension #Linux

 

6: Write the EOF tag to the current location of the tape
mt eof
mt weof
mt -f /dev/st0 eof

 

7: Move the tape forward to the specified number of file tags, that is, skipping the specified EOF tag

The tape is located at the first block of the next file, that is, the tape is located at the first block of the next area (see Figure 01 ):

mt fsf
mt -f /dev/rmt/0 fsf
mt -f /dev/rmt/1 fsf 1 #go 1 forward file/tape (see fig.01)

 

 

8: removes the specified number of file tags from the tape, that is, the specified EOF tag is reversed.

The tape is located at the first block of the next file, that is, the tape is located after the EOF tag (see Figure 01 ):

mt bsf
mt -f /dev/rmt/1 bsf
mt -f /dev/rmt/1 bsf 1 #go 1 backward file/tape (see fig.01)

Here is the list of tape location commands:

FSF advances the specified number of file tags. The tape is positioned in the first block of the next file.
Fsfm advances the specified number of file tags. The tape is positioned on the last block of the previous file.
BSF backs up the specified number of file tags. The tape is positioned on the last block of the previous file.
BSfm backs up the specified number of file tags. The tape is positioned in the first block of the next file.
The ASF tape is positioned at the beginning of the specified number of file tags. Positioning is achieved by rewinding first and then advancing the specified number of file tags.
FSR advances the specified number of records.
BSR backs up the specified number of records.
FSS (SCSI tables) advances the specified setmarks.
BSS (SCSI tables) backs up the specified setmarks.

 

Basic BACKUP command

Let's take a look at the backup and recovery commands.

 

9: Backup Directory (in tar format)
tar cvf /dev/rmt/0n /etc
tar cvf /dev/st0 /etc

 

10: Restore directory (in tar format)
tar xvf /dev/rmt/0n -C /path/to/restore
tar xvf /dev/st0 -C /tmp

 

11: list or check the tape content in tar format)
mt -f /dev/st0 rewind; dd if=/dev/st0 of=-

### tar format ###
tar tvf {DEVICE} {Directory-FileName}
tar tvf /dev/st0
tar tvf /dev/st0 desktop
tar tvf /dev/rmt/0 foo > list.txt

 

12: use dump or ufsdump to back up partitions
###UNIX backup c0t0d0s2 partition###
ufsdump 0uf /dev/rmt/0  /dev/rdsk/c0t0d0s2
###Linux backup / home partition###
dump 0uf /dev/nst0 /dev/sda5
dump 0uf /dev/nst0 /home
###FreeBSD backup / usr partition###
dump -0aL -b64 -f /dev/nsa0 /usr

 

12: Use ufsrestore or restore to restore a partition
It's Unix.
ufsrestore xf /dev/rmt/0
###UNIX interactive recovery###
ufsrestore if /dev/rmt/0
### Linux ###
restore rf /dev/nst0
###Interactive recovery from 6th backup on tape media###
restore isf 6 /dev/nst0
###FreeBSD recovering ufsdump format###
restore -i -f /dev/nsa0

 

13: write from the beginning of the tape (see Figure 02)
###This overwrites all data on the tape###
mt -f /dev/st1 rewind
###Back up home###
tar cvf /dev/st1 /home
###Offline and unmount tape###
mt -f /dev/st0 offline

Start from the beginning of the tape:

mt -f /dev/st0 rewind
tar xvf /dev/st0
mt -f /dev/st0 offline

 

14: write data from the last tar (see Figure 02)
###This preserves previously written data###
mt -f /dev/st1 eom
###Back up home###
tar cvf /dev/st1 /home
###Uninstall###
mt -f /dev/st0 offline

 

15: write after tar number 2 (see Figure 02)
###Write after tar number 2 (should be 2 + 1)###
mt -f /dev/st0 asf 3
tar cvf /dev/st0 /usr
###ASF is equivalent to FSF###
mt -f /dev/sf0 rewind
mt -f /dev/st0 fsf 2

Restore tar from tar number 2:

mt -f /dev/st0 asf 3
tar xvf /dev/st0
mt -f /dev/st0 offline

 

How do I verify the Backup Tape created using tar?

It is very important to regularly perform system-wide repair and service tests. This is the only way to determine the proper operation of the entire system. For more information, see our tutorial on verifying the tar command tape backup.

 

Example shell script
#!/bin/bash
# A UNIX / Linux shell script to backup dirs to tape device like /dev/st0 (linux)
# This script make both full and incremental backups.
# You need at two sets of five  tapes. Label each tape as Mon, Tue, Wed, Thu and Fri.
# You can run script at midnight or early morning each day using cronjons.
# The operator or sys admin can replace the tape every day after the script has done.
# Script must run as root or configure permission via sudo.
# -------------------------------------------------------------------------
# Copyright (c) 1999 Vivek Gite <vivek@nixcraft.com>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Last updated on : March-2003 - Added log file support.
# Last updated on : Feb-2007 - Added support for excluding files / dirs.
# -------------------------------------------------------------------------
LOGBASE=/root/backup/log

# Backup dirs; do not prefix /
BACKUP_ROOT_DIR="home sales"

# Get todays day like Mon, Tue and so on
NOW=$(date +"%a")

# Tape devie name
TAPE="/dev/st0"

# Exclude file
TAR_ARGS=""
EXCLUDE_CONF=/root/.backup.exclude.conf

# Backup Log file
LOGFIILE=$LOGBASE/$NOW.backup.log

# Path to binaries
TAR=/bin/tar
MT=/bin/mt
MKDIR=/bin/mkdir

# ------------------------------------------------------------------------
# Excluding files when using tar
# Create a file called $EXCLUDE_CONF using a text editor
# Add files matching patterns such as follows (regex allowed):
# home/vivek/iso
# home/vivek/*.cpp~
# ------------------------------------------------------------------------
[ -f $EXCLUDE_CONF ] && TAR_ARGS="-X $EXCLUDE_CONF"

#### Custom functions #####
# Make a full backup
full_backup(){
    local old=$(pwd)
    cd /
    $TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR
    $MT -f $TAPE rewind
    $MT -f $TAPE offline
    cd $old
}

# Make a  partial backup
partial_backup(){
    local old=$(pwd)
    cd /
    $TAR $TAR_ARGS -cvpf $TAPE -N "$(date -d '1 day ago')" $BACKUP_ROOT_DIR
    $MT -f $TAPE rewind
    $MT -f $TAPE offline
    cd $old
}

# Make sure all dirs exits
verify_backup_dirs(){
    local s=0
    for d in $BACKUP_ROOT_DIR
    do
        if [ ! -d /$d ];
        then
            echo "Error : /$d directory does not exits!"
            s=1
        fi
    done
    # if not; just die
    [ $s -eq 1 ] && exit 1
}

#### Main logic ####

# Make sure log dir exits
[ ! -d $LOGBASE ] && $MKDIR -p $LOGBASE

# Verify dirs
verify_backup_dirs

# Okay let us start backup procedure
# If it is Monday make a full backup;
# For Tue to Fri make a partial backup
# Weekend no backups
case $NOW in
    Mon)    full_backup;;
    Tue|Wed|Thu|Fri)    partial_backup;;
    *) ;;
esac > $LOGFIILE 2>&1

 

 

About third-party backup tools

Linux and Unix-like systems provide many third-party tools that can be used to schedule backups, including tape backups, such:

  • Amanda
  • Bacula
  • Rsync
  • Duplicity
  • Rsnapshot

See

  • Man pages-mt (1), CTX (1), tar (1), dump (8), restore (8)

Via: http://www.cyberciti.biz/hardware/unix-linux-basic-tape-management-commands/

Author: Vivek Gite Translator: alim0x Proofreader: Mr eye

This article was originally compiled by LCTT and launched with the honor of Linux in China

This article permanently updates the link address:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.