Chinaunix首页 | 论坛 | 博客
  • 博客访问: 850763
  • 博文数量: 436
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -103
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-01 09:48
个人简介

爱生活,爱IT

文章分类

全部博文(436)

文章存档

2015年(1)

2014年(2)

2013年(6)

2011年(39)

2010年(176)

2009年(30)

2008年(28)

2007年(54)

2006年(91)

2005年(9)

分类: LINUX

2007-11-22 17:25:50

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/st0 rewind
STEP 2 (check to see if you are at block 0)
# mt -f /dev/st0 tell
At block 0.
STEP 3 (Backup “tar compress” directories “one” and “two”)
# tar -czf /dev/st0 one two
STEP 4 (Check to see what block you are at)
# mt -f /dev/st0 tell
You should get something like block 2 at this point.
STEP 5 (Rewind the tape)
# mt -f /dev/st0 rewind
STEP 6 (List the files)
# tar -tzf /dev/st0
one/
one/test
two/
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/st0″.
# cd junk
# mt -f /dev/st0 rewind
# mt -f /dev/st0 tell
At block 0.
# tar -xzf /dev/st0 one
STEP 8 (Next, take a look to see what block the tape is at)
# mt -f /dev/st0 tell
At block 2.
STEP 9 (Now backup directories three and four)
# tar -czf /dev/st0 three four
After backing up the files, the tape should be past block 2.
Check this.
# mt -f /dev/st0 tell
At block 4.
Currently the following exist:
At block 1:
one/
one/test
two/
At block 2:
three/
three/samplehere
four/
At block 4:
(* This is empty *)
A few notes. You can set the blocking factor and a label
with tar. For example:
$ tar –label=”temp label” –create –blocking-factor=128 –file=/dev/st0 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/st0
tar: /dev/st0: 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/st0 rewind
$ tar -t –blocking-factor=128 –file=/dev/st0
temp label
Notes
Take advantage of the label command.
$ MYCOMMENTS=”Big_important_tape”
$ tar –label=”$(date +%F)”+”${MYCOMMENTS}”
Writing to tape on a remote 192.168.1.155 computer
$ tar cvzf - ./tmp | ssh -l chirico 192.168.1.155 ‘(mt -f /dev/st0 rewind; dd of=/dev/st0 )’
Restoring the contents from tape on a remote computer
$ ssh -l chirico 192.168.1.155 ‘(mt -f /dev/st0 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/st0 rewind
$ tar –label=”Contenets of Notes” –create –blocking-factor=128 –file=/dev/st0 Notes
$ mt -f /dev/st0 rewind
$ dd ibs=1048576 if=/dev/st0 of=notes.tar
The above will probably work with ibs=64k as well

Linux下使用磁带机的方式有多种,主要有通过Amanda、Tar等软件进行操作。
Amanda是提供了远程集中备份的功能,通过分别设置客户端、服务器端,实现远程集中存储备份。而Tar主要用于单机环境下,将数据直接写入磁带的备份。针对单一节点的备份,只要简单的使用Tar命令进行备份、恢复即可。
安装
对于目前厂家的HP DAT24/40系列外置SCSI磁带机,都带有自动回卷功能。将其接在外置SCSI的总线上,并重新启动服务器。重新引导后,执行dmesg将可以看到新的磁带机设备叫/dev/st0。

blk: queue ef0d7a14, I/O limit 4095Mb (mask 0xffffffff)audit subsystem ver 0.1 initialized(scsi0:A:3): 10.000MB/s transfers (10.000MHz, offset 15)  Vendor: HP        Model: C1537A            Rev: L805  Type:   Sequential-Access                  ANSI SCSI revision: 02blk: queue ef0e4614, I/O limit 4095Mb (mask 0xffffffff)Attached scsi tape st0 at scsi0, channel 0, id 3, lun 0
磁带操作
装入磁带后,可执行如下操作:
倒带,将磁带卷至起始位置
mt -f /dev/st0 rewind
擦除,擦掉磁带上的内容
mt -f /dev/st0 erase
注意:擦除工作非常慢,并且对磁带有损伤,最好不要执行,当数据写满后,可以继续写入数据并覆盖原有数据,无须执行擦除动作。新磁带打开封装后立刻可以使用,不需要执行擦除。
出带,将磁带卷至初始位置然后从磁带机内弹出
mt –f /dev/st0 offline
数据操作
基本操作如下:
1. 列目录操作tar tvf /dev/st0
假如磁带上没有任何文件,则列目录会报错,这个错误没有关系,不影响磁带的使用。
[root@dev131 /root]# tar tvf /dev/st0tar: /dev/st0: Cannot read: Input/output errortar: At beginning of tape, quitting nowtar: Error is not recoverable: exiting now[root@dev131 /root]#
2. 写入数据操作tar cvf /dev/st0 <要写入的文件名>
写入数据的方法有两种:不打包直接写入文件和打包压缩后写入文件。二者各有优缺点。不打包直接写入文件的方法,看起来效率低,操作复杂,但是可以提高数据的生存率。磁带是一种线性存储的设备,所有数据紧挨着顺序写入。当磁带的某一点损坏的时候,其余位置的磁带还可以继续读取,其中的文件也都能读出。如果采用了打包压缩后写入的方法,则磁带上存储的只有一个文件。当磁带有任何一个地方发生了故障无法读取,则这个压缩文件将缺少一些细节,即使是一个bit的错误,也将导致压缩文件报告CRC错误无法解压。所以,如果是存储大量的数据,建议直接写入。如果是存取小文件,则最好打包并给文件名加时间标记写入。
例如要将/root/test1.tar.gz写入到磁带并覆盖磁带的内容:
tar cvf /dev/st0 test1.tar.gz
磁带机的传输速度比较慢,对于不是很大的文件备份,稍等几秒,写入即可完成。使用参数cvf将覆盖此带上的已有文件。
随后列出磁带上的文件:
[root@dev131 /root]# tar tvf /dev/st0-rw-r--r-- root/root       320 2006-12-01 09:29:02 test1.tar.gz[root@dev131 /root]#
可看到数据写入成功。
注意:这个命令只能在磁带是全新的时候写入,在第二次执行的时候,就会覆盖掉磁带上原来的数据。另外,使用tar命令备份,要求备份文件体积必须小于单盘磁带的容量,即一个文件不能跨越两盘磁带。如果遇到这种情况,则不能使用tar命令备份,需要换其他备份程序进行操作。
3. 继续写入数据tar rvf /dev/st0 <要写入的文件名>
磁带存储是线性存储,所有数据是依次写入的。为了不覆盖前边的内容,在写入时候,要使用rvf参数写入内容。
tar rvf /dev/st0 test2.tar.gz
再执行一次rvf继续写入其他的文件:
tar rvf /dev/st0 test3.tar.gz
写入完成后,查看磁带上有哪些文件:
[root@dev131 /root]# tar tvf /dev/st0-rw-r--r-- root/root       320 2006-12-01 09:29:02 test1.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:44:19 test2.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:30:14 test3.tar.gz[root@dev131 /root]#
可以看到,新写入的文件和原文件都存储在了磁带上。
磁带的线性存储特性,使得磁带上可以写入两次相同的文件。例如现在test3文件发生了文件大小的变化,再次写入到磁带:
tar rvf /dev/st0 test3.tar.gz
查看文件列表,可以看到磁带上有如下文件:
[root@dev131 /root]# tar tvf /dev/st0-rw-r--r-- root/root       320 2006-12-01 09:29:02 test1.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:30:14 test2.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:44:19 test3.tar.gz-rw-r--r-- root/root     67085 2006-12-01 09:44:19 test3.tar.gz[root@dev131 /root]#
这里可看到,磁带上有两个文件同名的文件写入。这个文件的两次备份大小和时间可以相同,也可以不相同。
注意:在磁带上如果相同文件写入了多次,在恢复时候会比较麻烦,需要先将磁带卷至文件所存储的地方,而后读取磁带当前位置所存储的文件,操作较为复杂,速度也比较慢。所以不建议在一盘磁带上写入相同文件名的文件。备份前,最好将备份文件的文件名上加上时间标记,便于查找备份。例如要备份的文件名是test-20061201-0930.tar.gz和test-20061201-0945.tar.gz,则写入到一盘磁带上。如此在执行备份和恢复操作时候,能够更加便利快捷的进行存取操作。
4. 读取数据tar xvf /dev/st0 <要读取的文件名>
读取数据前,首先查看磁带上的内容,获取要恢复的文件名。例如查看后得到如下结果:
[root@dev131 /root]# tar tvf /dev/st0-rw-r--r-- root/root       320 2006-12-01 09:29:02 test1.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:30:14 test2.tar.gz-rw-r--r-- root/root       320 2006-12-01 09:44:19 test3.tar.gz[root@dev131 /root]#
现在恢复其中的test3.tar.gz文件,执行如下命令:
tar xvf /dev/st0 test3.tar.gz
读取数据后。
[root@dev131 ~]# tar xvf /dev/st0 test3.tar.gztest3.tar.gz[root@dev131 ~]#
随后可以在当前的路径下找到从磁带中读取出来的文件:
[root@dev131 ~]# ls -l test3.tar.gz-rw-r--r--   1   root  root   320  Dec 1  11:12   test3.tar.gz[root@dev131 ~]#
至此恢复操作完成。
通过上文所介绍的tar命令,使用tvf/cvf/rvf/xvf参数,即可实现对磁带的读取、写入操作。使用预先写好的复制数据脚本,配合上磁带机操作命令,并且放置到/etc/crontab中,即可实现自动备份。
阅读(3474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~