15 Useful Linux and Unix Tape Managements Commands For Sysadmins

 

by VIVEK GITE on OCTOBER 18, 2015 last updated MARCH 28, 2016

in COMMAND LINE HACKSHARDWAREHOWTOSTORAGE

Tape devices should be used on a regular basis only for archiving files or for transferring data from one server to another. Usually, tape devices are all hooked up to Unix boxes, and controlled with mt or mtx. You must backup all data to both disks (may be in the cloud) and the tape device. In this tutorial you will learn about:

 

 

 

  • Tape device names
  • Basic commands to manage tape drive
  • Basic backup and restore commands

 

Why do you need backups?

A backup plan is necessary for you to backup your files on a regular basis. If you choose not to back up your own files, you risk losing important data. A backup allows you to:

  • Ability to recover from disk failure
  • Accidental file deletion
  • File or file system corruption
  • Complete server destruction, including the destruction of on-site backups due to fire or other problems
  • Hard drives and SSD do crash
  • Viruses and ransomware can corrupt or delete files

You can use tape-based archives to backup the whole server and move tapes off-site.

Understanding tape file marks and block size

 

Fig.01: Tape file marks

Fig.01: Tape file marks

Each tape device can store multiple tape backup files. Tape backup files are created using cpio, tar, dd, and so on. However, tape device can be opened, written data to, and closed by the various program. You can store several backups (tapes) on physical tape. Between each tape file is a “tape file mark”. This is used to indicate where one tape file ends and another begins on physical tape. You need to use mt command to positions the tape (winds forward and rewinds and marks).

 

How is data stored on a tape drive

 

Fig.02: How data is stored on a tape

Fig.02: How data is stored on a tape

All data is stored subsequently in sequential tape archive format using tar. The first tape archive will start on the physical beginning of the tape (tar #0). The next will be tar #1 and so on.

 

Tape device names on Unix

  1. /dev/rmt/0 or /dev/rmt/1 or /dev/rmt/[0-127] : Regular tape device name on Unix. The tape is rewound.
  2. /dev/rmt/0n : This is know as no rewind i.e. after using tape, leaves the tape in current status for next command.
  3. /dev/rmt/0b : Use magtape interface i.e. BSD behavior. More-readable by a variety of OS’s such as AIX, Windows, Linux, FreeBSD, and more.
  4. /dev/rmt/0l : Set density to low.
  5. /dev/rmt/0m : Set density to medium.
  6. /dev/rmt/0u : Set density to high.
  7. /dev/rmt/0c : Set density to compressed.
  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 : FreeBSD specific SCSI tape device name that eject on close (if capable).

Tape device name examples

  • The /dev/rmt/1cn indicate that I’m using unity 1, compressed density and no rewind.
  • The /dev/rmt/0hb indicate that I’m using unity 0, high density and BSD behavior.
  • The auto rewind SCSI tape device name on Linux : /dev/st0
  • The non-rewind SCSI tape device name on Linux : /dev/nst0
  • The auto rewind SCSI tape device name on FreeBSD: /dev/sa0
  • The non-rewind SCSI tape device name on FreeBSD: /dev/nsa0

How do I list installed SCSI tape devices?

Type the following commands:

## Linux (read man pages for more info) ##
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

Sample outputs from my Linux server:

Fig.03: Installed tape devices on Linux server

Fig.03: Installed tape devices on Linux server

 

mt command examples

In Linux and Unix-like system, the mt command is used to control operations of the tape drive, such as finding status or seeking through files on a tape or writing tape control marks to the tape. You must type the following command as root user. The syntax is:

mt -f /tape/device/name operation

Setting up environment

You can set TAPE shell variable. This is the pathname of the tape drive. The default (if the variable is unset, but not if it is null) is /dev/nsa0 on FreeBSD. It may be overridden with the -f option passed to the mt command as explained below.

 ## Add to your shell startup file ##
TAPE=/dev/st1 #Linux
TAPE=/dev/rmt/2 #Unix
TAPE=/dev/nsa3 #FreeBSD
export TAPE

1: Display status of the tape/drive

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 unity 1 i.e. tape device no. 1

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

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

2: Rewinds the tape

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

3: Eject the tape

mt off
mt offline
mt eject
mt -f /dev/mt/0 off
mt -f /dev/st0 eject

4: Erase the tape (rewind the tape and, if applicable, unload the tape)

mt erase
mt -f /dev/st0 erase  #Linux 
mt -f /dev/rmt/0 erase #Unix

5: Retensioning a magnetic tape cartridge

If errors occur when a tape is being read, you can retension the tape, clean the tape drive, and then try again as follows:

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

6: Writes n EOF marks in the current position of tape

mt eof
mt weof
mt -f /dev/st0 eof

7: Forward space count files i.e. jumps n EOF marks

The tape is positioned on the first block of the next file i.e. tape will position on first block of the field (see fig.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: Backward space count files i.e. rewinds n EOF marks

The tape is positioned on the first block of the next file i.e. tape positions after EOF mark (see fig.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 a list of the tape position commands:

       fsf    Forward space count files.  The tape is positioned on the first block of the next file.
 
       fsfm   Forward space count files.  The tape is positioned on the last block of the previous file.
 
       bsf    Backward space count files.  The tape is positioned on the last block of the previous file.
 
       bsfm   Backward space count files.  The tape is positioned on the first block of the next file.
 
       asf    The tape is positioned at the beginning of the count file. Positioning is done by first rewinding the tape and then spacing forward over count filemarks.
 
       fsr    Forward space count records.
 
       bsr    Backward space count records.
 
       fss    (SCSI tapes) Forward space count setmarks.
 
       bss    (SCSI tapes) Backward space count setmarks.

Basic backup commands

Let us see commands to backup and restore files

9: To backup directory (tar format)

tar cvf /dev/rmt/0n /etc
tar cvf /dev/st0 /etc

10: To restore directory (tar format)

tar xvf /dev/rmt/0n -C /path/to/restore
tar xvf /dev/st0 -C /tmp

11: List or check tape contents (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: Backup partition with dump or ufsdump

 ## 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: Restore partition with ufsrestore or restore

## Unix ##
ufsrestore xf /dev/rmt/0
## Unix interactive restore ##
ufsrestore if /dev/rmt/0
 
## Linux ##
restore rf /dev/nst0
## Restore interactive from the 6th backup on the tape media ##
restore isf 6 /dev/nst0
 
## FreeBSD restore ufsdump format ##
restore -i -f /dev/nsa0

13: Start writing at the beginning of the tape (see fig.02)

## This will overwrite all data on tape ##
mt -f /dev/st1 rewind
 
### Backup home ##
tar cvf /dev/st1 /home
 
## Offline and unload tape ##
mt -f /dev/st0 offline

To restore from the beginning of the tape:

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

14: Start writing after the last tar (see fig.02)

## This will kee all data written so far ##
mt -f /dev/st1 eom
 
### Backup home ##
tar cvf /dev/st1 /home
 
## Unload ##
mt -f /dev/st0 offline

15: Start writing after tar number 2 (see fig.02)

## To wrtite after tar number 2 (should be 2+1)
mt -f /dev/st0 asf 3
tar cvf /dev/st0 /usr
 
## asf equivalent command done using fsf ##
mt -f /dev/sf0 rewind
mt -f /dev/st0 fsf 2

To 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 backup tapes created using tar?

It is important that you do regular full system restorations and service testing, it’s the only way to know for sure that the entire system is working correctly. See our tutorial on verifying tar command tape backups for more information.

Sample 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 https://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

A note about third party backup utilities

Both Linux and Unix-like system provides many third-party utilities which you can use to schedule the creation of backups including tape backups such as:

  • Amanda
  • Bacula
  • rsync
  • duplicity
  • rsnapshot
See also

Read more

世界越快心越慢

在晚飯後的休息時間,我特別享受在客廳瀏灠youtube上各樣各式創作者的影音作品。很大不同於傳統媒體,節目多是針對大多數族群喜好挑選的,在youtube上我會依心情看無腦的動畫、一些旅拍記錄、新聞時事談論。 尤其在看了大量的Youtube的分享後,我真的感受到會限制我的是我的無知,特別是那些我想都沒想過的實際應用,在學習後大大幫助到我的生活和工作層面。 休息在家時,我喜歡想一些沒做過的菜,動手去設計生活和工作上的解決方案,自己是真的很難閒著沒事做。 如創作文章,陪養新的習慣都能感覺到成長的喜悅,是不同於吃喝玩樂的快樂的。 創作不去限制固定的形式,文字是創作、影像聲音也是創作,記錄生活也是創作,我想留下的就是創造—》實現—》回憶,這樣子的循環過程,在留下的足跡面看到自己一路上的成長、失敗、絕望、重新再來。 雖然大部份的時候去做這些創作也不明白有什麼特別的意義,但不去做也不會留下什麼,所以呀不如反事都去試試看,也許能有不一樣的水花也許有意想不到的結果,投資自己永遠不會是失敗的決定,不是嗎?先問問自己再開始計畫下一步,未來沒人說得準。 像最近看youtube仍大一群人在為DOS開

By Phillips Hsieh

知識管理的三個步驟:一小時學會把知識運用到生活上

摘錄瓦基「閱讀前哨站」文章作為自己學習知識管理的內容 Part1「篩選資訊」 如何從海量資訊中篩選出啟發性、實用性和相關性的精華,讓你在學習過程中不再迷失方向。 1. 實用性 2. 啟發性 Part2「提高理解」 如何通過譬喻法和應用法,將抽象的知識與日常生活和工作緊密結合,建立更深刻的理解。 1. 應用法 2. 譬喻法 Part3「運用知識」 如何連結既有知識,跟自己感興趣的領域和專案產生關聯,讓你在運用知識的路途上游刃有餘。 1. 跟日常工作專案、人際活動產生連結 # 為什麼要寫日記? * 寫日記是為了忘記,忘卻瑣碎事情,保持專注力 * 寫日記就像在翻譯這個世界,訓練自己的解讀能力 * 不只是透過日記來記錄生活,而是透過日記來發展生活 #如何寫日記? * 不要寫流水帳式的日記,而是寫覆盤式的日記 當我們試著記錄活動和感受之間的關聯,有助於辦認出真正快樂的事 日記的記錄方式要以過程為主,而非結果 * 感恩日記的科學建議,每日感恩的案例

By Phillips Hsieh
2024年 3月30日 14屆美利達環彰化百K

2024年 3月30日 14屆美利達環彰化百K

這是場半小時就被秒報名額滿的經典賽事, 能順利出賽實屬隊友的功勞, 這次的準備工作想試試新買的外胎, 因為是無內胎用的外胎, 特別緊超級難安裝的, 問了其他朋友才知道, 要沾上肥皂水才容易滑入車框。 一早四點起床準備, 五點集合備好咖啡在車上飲用, 約了六點在彰化田尾鄉南鎮國小, 整好裝四人一起出發前往會場。 被排在最後一批出發, 這次的路線會繞行的員林148上139縣道, 其實在早上五點多天就開始有點飄雨, 大伙就開始擔心不會要雨戰吧! 果不其然才出發準備上148爬坡雨勢越來越大, 戴著防風眼鏡的我在身體的熱氣加上雨水冷凝效果下, 鏡面上滿是霧氣肉眼可視距離才剩不到五公尺, 只能緊依前前方的車友幫忙開路, 之後洪大跟上來我立馬請求他幫忙開路, 上了139停下車把防風眼鏡收起來, 反正下雨天又陰天完全用不到太陽眼鏡了。 雨是邊下邊打雷, 大伙都在這條139上一台一台單車好像避電針, 一時有點害怕不然想平時沒做什麼壞事, 真打到自己就是天意了。 下了139雨勢開始變小, 大伙的速度開始有所提昇, 開高鐵列車的時機己成熟, 物色好列車就跟好跟滿。 最後找了一隊似乎整團有固定在練

By Phillips Hsieh
2023 12月9號 美利達單車嘉年華

2023 12月9號 美利達單車嘉年華

第二次參加美利達環南投賽事, 還記得去年第一次參加這美利達環南投, 還特地提前一天跟車友在魚池住了一晚。 這回用上了剛在7月份剛安裝的車頂架, 安裝了二種不同的攜車架, 都樂這邊可以不用拆車輪直上車頂, YAKIMA這邊選了經濟的款式, 折掉前輪利用前叉固定在攜車架上。 約了唯一一位一起參加的朋友, 二人一早四點約見面, 幫朋友帶上了拿鐵咖啡, 開上日月潭在水社碼頭停好車, 騎往向山遊客中心, 路過美麗的日月潭簡直不要太美了拍一張。 抵達會場己是人山人海了, 跟著大伙排隊順便也看網紅也欣賞名車。 出發就先沿著日月潭順時針騎, 騎到玄裝寺很急停下來上一下廁所, 比賽時都會尿都特別的滿, 一方面是比較緊張,一方面是特別興奮。 這時己經跟車友失散了, 只能獨推沿路看有沒有車友可以一起組隊的, 很可惜在山區大家的實力不一只求平安順騎了, 原則就是有補給就停有食物就吃。 下到水里人群再次聚集起來, 光等紅綠燈就是一條車龍。 騎行了一大圈水里再回到131縣道, 這時背後傳來熟悉的聲音叫菲哥, 終於跟車友重新集合接下來就一路邊聊邊騎。 最後來幾張專業攝影師拍攝的照片 回到終點台上

By Phillips Hsieh