2007-10-24
環境:Ubuntu 7.04 feisty, HP DAT-72 usb外置磁帶機。
基本概念
* 磁帶是線性存放裝置,沒有什麼分區表的概念,磁頭在哪裡,就從哪裡開始讀。
* 磁帶機好像只能用root用戶操作。
* 磁帶上可以劃分存儲空間,每個存儲空間有一個file number,從0開始順序排列。
* 磁頭在存儲空間中的位置用block number來表示,0代表開始,大於0的整數代表塊,比如39代表磁頭在本file number的第39塊的位置,而-1則代表磁頭位於本file number的結尾。
* 同樣一部磁帶機,用/dev/st0時是當自動回卷設備使用,每次操作完成之後,磁頭都返回file number=0, block number=0, BOT的位置;而用/dev/nst0調用時,操作完成時磁頭就停在那個位置,不會回到開頭。
* 使用nst非回卷方式,用tar tvf列檔,只有當block number=0時才能出內容,並且列完之後block number為大於0的整數,注意這並不代表本段存儲空間的結尾(-1)。
由於磁帶機本身具有壓縮功能,所以添加檔的時候不建議指定zip或者bz2壓縮,因為如果一點點壓縮後的資料損壞,會導致全部內容不可識別;如果的確需要壓縮的話,可以壓縮成本地檔再存儲到磁帶上,減少對其他檔的影響。
自動回卷的使用方式
這種方式比較簡單,用tar直接操作即可,基本上和tar操作本地檔相同。
往新磁帶上存儲檔,注意如果磁帶上已有檔,會被覆蓋掉:
tar cvf /dev/st0 file_to_store
向磁帶上添加檔:
tar rvf /dev/st0 file_to_store
更新磁帶上的同名檔:
tar uvf /dev/st0 file_to_store
列出磁帶上的現有檔:
tar tvf /dev/st0
刪除磁帶上的檔:
tar vf /dev/st0 --delete file_to_delete
但我使用這個命令的時候總是提示錯誤,檔倒是刪掉了,可查看的時候也總有錯誤資訊,難道是無法刪除,或者只能全部xvf出來再存到磁帶上?不過我們也應當養成一個好的習慣,每次向磁帶機存儲檔的時候一定要在檔案名中帶上日期標記。
非自動回卷的使用方式
一般這樣使用,都是用到多個存儲空間file number的時候;如果還是一個存儲段,只是手工移動檔指標就沒有什麼意義了。
在操作上,檔的存儲方式和自動回卷是基本相同的,不同之處在於要注意磁頭的位置,並且手工進行必要的位移,以一個新磁帶的操作為例來說明(status我只截取有用的部分顯示):
# mt -f /dev/nst0 status
file number = 0
block number = 0
General status bits on (41010000):
BOT ONLINE IM_REP_EN
存入一個新的檔A:
# tar cvf /dev/nst0 A
A
# mt -f /dev/nst0 status
file number = 1
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
注意這時磁頭已經移動到了第二段存儲空間了,現在再存入一個新的檔B,當然B和A是不在一個存儲空間中的:
# tar cvf /dev/nst0 B
B
# mt -f /dev/nst0 status
file number = 2
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
磁頭又移動到第三段存儲空間了,如果要查看剛才存的第二段存儲空間中的內容,需要先移動磁頭:
# mt -f /dev/nst0 bsf 2
# mt -f /dev/nst0 status
file number = 0
block number = -1
General status bits on (1010000):
ONLINE IM_REP_EN
# mt -f /dev/nst0 fsf 1
# mt -f /dev/nst0 status
file number = 1
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
由於有存儲空間結束標記,所以這裡採用退2進1的方式,詳細的磁頭移動方式可以看man mt。然後查看文件列表:
# tar tvf /dev/nst0
-rw-r--r-- root/root 21194792 2007-10-24 11:37 B
# mt -f /dev/nst0 status
file number = 1
block number = 2070
General status bits on (1010000):
ONLINE IM_REP_EN
列出檔清單之後,磁頭停在了檔結束的位置,但不是存儲空間的結尾。現在移動磁頭到本段存儲空間的開始,並且添加檔C:
# mt -f /dev/nst0 bsf 1
# mt -f /dev/nst0 status
file number = 0
block number = -1
General status bits on (1010000):
ONLINE IM_REP_EN
# mt -f /dev/nst0 fsf 1
# mt -f /dev/nst0 status
file number = 1
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
# tar rvf /dev/nst0 C
C
# mt -f /dev/nst0 status
file number = 2
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
首先還是要移動磁頭,到這裡基本上可以發現mt的fsf和bsf的規律了,fsf是磁頭向前移動,並且總是停留在block number = 0的位置,而bsf是磁頭向後移動,總是停留在 block number = -1的位置。添加完成檔之後,磁頭又定位到了第三個存儲空間(下一個)的開始。現在我們依然是移動磁頭到第二段存儲空間開始,並且用覆蓋方式添加檔 D:
# mt -f /dev/nst0 bsf 2
# mt -f /dev/nst0 fsf 1
# mt -f /dev/nst0 status
file number = 1
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
# tar cvf /dev/nst0 D
D
# mt -f /dev/nst0 status
file number = 2
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
檔D是用覆蓋方式添加的,所以剛才添加的檔B和C現在應該都消失了:
# mt -f /dev/nst0 bsf 2
# mt -f /dev/nst0 fsf 1
# mt -f /dev/nst0 status
file number = 1
block number = 0
General status bits on (81010000):
EOF ONLINE IM_REP_EN
# tar tvf /dev/nst0
-rw-r--r-- root/root 34201298 2007-10-24 11:57 D
# mt -f /dev/nst0 status
file number = 1
block number = 3341
General status bits on (1010000):
ONLINE IM_REP_EN
完成,回卷磁帶,從開始一直tvf:
# mt -f /dev/nst0 rewind
# tar tvf /dev/nst0
-rw-r--r-- root/root 341054 2007-10-24 11:37 A
# tar tvf /dev/nst0
# tar tvf /dev/nst0
-rw-r--r-- root/root 34201298 2007-10-24 11:57 D
# tar tvf /dev/nst0
# tar tvf /dev/nst0
# tar tvf /dev/nst0
tar: /dev/nst0: Cannot read: Input/output error
tar: At beginning of tape, quitting now
tar: Error is not recoverable: exiting now
# mt -f /dev/nst0 status
file number = 2
block number = -1
General status bits on (9010000):
EOD ONLINE IM_REP_EN
現在,能看懂這個了麼?我用空行隔開的位置正好是block number為0的位置。還有一些其他的操作就比較簡單了:
出帶,將磁帶卷至初始位置然後從磁帶機內彈出
# mt –f /dev/nst0 offline
清除磁帶中的所有內容,特慢,還傷帶,輕易不要用
# mt –f /dev/nst0 erase
我的幾個疑問
* 如何刪除file number?
* 既然磁帶是線性存儲,如果向前面一個file number中添加檔,存儲空間不夠怎麼辦?
小結
搞明白磁帶機的存儲方式和基本操作,用起來就簡單了。個人感覺如果要存的檔數量不多,尤其是只需要存幾個大檔的情況,直接用自動回卷方式,只使用一個存儲空間就可以了,這樣操作比較簡單。如果一定要用磁帶存儲複雜結構的資料,可以考慮手工在各個存儲空間之間來回切換的方式,原理都是一樣,只是更高一點點的技巧和小心。
同時,保存一份磁帶上現有檔的索引也是非常有必要的(tvf的結果),既方便檔的查找,也可以由此推算出磁帶上的剩餘空間大小。
參考
How to use the DAT-tape with Linux
This document describes how to use the DAT-tape standing besides Gettysburg with the Linux machine cs-zz12. It asumes some familiarity with Linux or other UNIX-systems. It is itentionaly kept short, so that you can find the important information quickly. If you are in a hurry, check the typical sessions section.
Tape organisation
The tape is structured in multiple files which are in a sequential order. The individual files are usually tar-archives. You can always append new files (tar-archives) to the tape (if there is enough space left).
Handling the tape
Insert the tape softly with the arrow on the upper side, pointing in the direction of the drive.
It will then be sucked in automatically. Ejecting the tape is done with the mt-command (see below).
Important: Before the first "mt" or "tar" command, you must switch the SCSI Switch to "1". Otherwise the system will fail to recognize the SCSI devices (including the tape drive).
Tape operation
You can operate the tape with the command "mt" (magnetic tape). You use it like this: "mt -f /dev/nst0 command". For more detailed information, see the man page. Examples are given below. The most important commands are:
* stat: prints the current status of the tape such as
o file number: The number of the file where the tape currently is (where 0 is the first file, -1 is the last).
o block number: The block number in the current file (here too, 0 is the first block and -1 is the last one).
* rewind: rewind the tape to its beginning.
* fsf
* bsf
* eom: Wind to end of media. Useful to append new files to the end of the tape.
* offline: Rewind the tape and eject it.
Examples for the mt-command
* To get the status of the tape: mt -f /dev/nst0 stat
* Rewind the tape to the start: mt -f /dev/nst0 rewind
* Wind to the next file on tape: mt -f /dev/nst0 fsf 1
Transfering data to and from the tape
You usually do this with the "tar" command. "Tar" always needs the parameter "-f" with the corresponding device, in this case "/dev/nst0". The parameter "-v" (verbose) is also suggested. The other parameters you usually need are:
* -c: Create (overwrite) tar-archive. If you overwrite a tar-file which is not the last one on the tape, all files after the overwritten one are lost!
* -x: Extract from tar-archive at current position on tape.
* -t: List contents of tar-archive at current position on tape.
Examples for tar-command
* List the contents of the next tar-file on the tape: tar -tvf /dev/nst0
* Put the directory (or files and directories)
* Extract everything from the next tar-file on the tape into the current directory: tar -xvf /dev/nst0
* Extract (only)
Typical sessions
Backup / Store
1. Insert tape in tape drive and probably switch SCSI Switch to one.
2. Check status of tape: mt -f /dev/nst0 stat
3. Probably go to end of tape: mt -f /dev/nst0 eom
4. Store
5. Rewind and eject tape: mt -f /dev/nst0 offline
Restore
1. Insert tape in tape drive and probably switch SCSI Switch to one.
2. Check status of tape: mt -f /dev/nst0 stat
3. Go to the directory where you want to restore your file(s).
4. Go to the right file on the tape with the following commands:
* Check file number and position in file: mt -f /dev/nst0 stat
* Advance one file: mt -f /dev/nst0 fsf 1
* View contents of tar-file: tar -tvf /dev/nst0
* Go back one file: mt -f /dev/nst0 bsf 1
* If you are in the last block of a file and you should be at the beginning of the file, do the following:
1. mt -f /dev/nst0 bsf 1
2. mt -f /dev/nst0 fsf 1
And check with: mt -f /dev/nst0 stat
5. Extract your file(s): tar -xvf /dev/nst0 [
6. Rewind and eject tape: mt -f /dev/nst0 offline
Remote operation
If your tape drive is in another machine than the machine you want to backup from, you can use the same tar-commands as described above with one little change: Login to the machine where the data is and instead of the tar-archive filename /dev/nst0, you specify hostname:/dev/nst0 (where hostname is the name of the machine where the tape-drive is). If you need to be another user on the machine with the drive to access the tape, you can even use the filename user@hostname:/dev/nst0.
The mt command must still be executed on the machine where the tape drive is. There is no remot-mt as far as I know.
Last change: 21 February 2001
rauch@inf.ethz.ch