天天看点

Linux其他常见压缩备份工具 - dd,cpio

dd

        dd 可以读取磁碟装置的内容(几乎是直接读取磁区"sector"),然后将整个装置备份成一个文件呢!真的是相当的好用啊~ dd 的用途有很多啦~但是我们仅讲一些比较重要的选项,如下:

[[email protected] ~]# dd if="input_file" of="output_file" bs="block_size" \
> count="number"
选项与参数:
if   :就是 input file 罗~也可以是装置喔!
of   :就是 output file 喔~也可以是装置;
bs   :规划的一个 block 的大小,若未指定则默认是 512 bytes(一个 sector 的大小)
count:多少个 bs 的意思。

范例一:将 /etc/passwd 备份到 /tmp/passwd.back 当中
[[email protected] ~]# dd if=/etc/passwd of=/tmp/passwd.back
3+1 records in
3+1 records out
1945 bytes (1.9 kB) copied, 0.000332893 seconds, 5.8 MB/s
[[email protected] ~]# ll /etc/passwd /tmp/passwd.back
-rw-r--r-- 1 root root 1945 Sep 29 02:21 /etc/passwd
-rw-r--r-- 1 root root 1945 Dec 17 18:09 /tmp/passwd.back
# 仔细的看一下,我的 /etc/passwd 文件大小为 1945 bytes,因为我没有配置 bs ,
# 所以默认是 512 bytes 为一个单位,因此,上面那个 3+1 表示有 3 个完整的 
# 512 bytes,以及未满 512 bytes 的另一个 block 的意思啦!
# 事实上,感觉好像是 cp 这个命令啦~

范例二:将自己的磁碟之第一个磁区备份下来
[[email protected] ~]# dd if=/dev/hdc of=/tmp/mbr.back bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0104586 seconds, 49.0 kB/s
# 第一个磁区内含有 MBR 与 partition table ,透过这个动作,
# 我们可以一口气将这个磁碟的 MBR 与 partition table 进行备份哩!

范例三:找出你系统最小的那个分割槽,并且将他备份下来:
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.9G  5.1G  44% /
/dev/hdc3             4.8G  651M  3.9G  15% /home
/dev/hdc1              99M   21M   73M  23% /boot  <==就捉他好了!
[[email protected] ~]# dd if=/dev/hdc1 of=/tmp/boot.whole.disk
208782+0 records in
208782+0 records out
106896384 bytes (107 MB) copied, 6.24721 seconds, 17.1 MB/s
[[email protected] ~]# ll -h /tmp/boot.whole.disk
-rw-r--r-- 1 root root 102M Dec 17 18:14 /tmp/boot.whole.disk
# 等於是将整个 /dev/hdc1 通通捉下来的意思~如果要还原呢?就反向回去!
# dd if=/tmp/boot.whole.disk of=/dev/hdc1 即可!非常简单吧!
# 简单的说,如果想要整个硬盘备份的话,就类似 Norton 的 ghost 软件一般,
# 由 disk 到 disk ,嘿嘿~利用 dd 就可以啦~厉害厉害!
      

例题:

        你想要将你的 /dev/hdc1 进行完整的复制到另一个 partition 上,请使用你的系统上面未分割完毕的容量再创建一个与 /dev/hdc1 差不多大小的分割槽 (只能比 /dev/hdc1 大,不能比他小!),然后将之进行完整的复制 (包括需要复制 boot sector 的区块)。

答:

        由於需要复制 boot sector 的区块,所以使用 cp 或者是 tar 这种命令是无法达成需求的! 此时那个 dd 就派的上用场了。你可以这样做:

# 1. 先进行分割的动作
[[email protected] ~]# fdisk -l /dev/hdc
   Device Boot   Start    End    Blocks   Id  System
/dev/hdc1   *        1     13    104391   83  Linux
# 上面鸟哥仅撷取重要的数据而已!我们可以看到 /dev/hdc1 仅有 13 个磁柱

[[email protected] ~]# fdisk /dev/hdc
Command (m for help): n
First cylinder (2354-5005, default 2354): 这里按 enter
Using default value 2354
Last cylinder or +size or +sizeM or +sizeK (2354-5005, default 5005): 2366

Command (m for help): p
   Device Boot   Start    End    Blocks   Id  System
/dev/hdc9         2354   2366    104391   83  Linux

Command (m for help): w
# 为什么要使用 2366 呢?因为 /dev/hdc1 使用 13 个磁柱,因此新的 partition
# 我们也给她 13 个磁柱,因此 2354 + 13 -1 = 2366 罗!

[[email protected] ~]# partprobe

# 2. 不需要格式化,直接进行 sector 表面的复制!
[[email protected] ~]# dd if=/dev/hdc1 of=/dev/hdc9
208782+0 records in
208782+0 records out
106896384 bytes (107 MB) copied, 16.8797 seconds, 6.3 MB/s

[[email protected] ~]# mount /dev/hdc9 /mnt
[[email protected] ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hdc1               101086     21408     74459  23% /boot
/dev/hdc9               101086     21408     74459  23% /mnt
# 这两个玩意儿会『一模一样』喔!
[[email protected] ~]# umount /mnt
      

cpio

[[email protected] ~]# cpio -ovcB  > [file|device] <==备份
[[email protected] ~]# cpio -ivcdu < [file|device] <==还原
[[email protected] ~]# cpio -ivct  < [file|device] <==察看
备份会使用到的选项与参数:
  -o :将数据 copy 输出到文件或装置上 
  -B :让默认的 Blocks 可以添加至 5120 bytes ,默认是 512 bytes ! 
     这样的好处是可以让大文件的储存速度加快(请参考 i-nodes 的观念) 
还原会使用到的选项与参数:
  -i :将数据自文件或装置 copy 出来系统当中 
  -d :自动创建目录!使用 cpio 所备份的数据内容不见得会在同一层目录中,因此我们
       必须要让 cpio 在还原时可以创建新目录,此时就得要 -d 选项的帮助!
  -u :自动的将较新的文件覆盖较旧的文件!
  -t :需配合 -i 选项,可用在"察看"以 cpio 创建的文件或装置的内容 
一些可共享的选项与参数:
  -v :让储存的过程中文件名称可以在萤幕上显示 
  -c :一种较新的 portable format 方式储存 
      
范例:找出 /boot 底下的所有文件,然后将他备份到 /tmp/boot.cpio 去!
[[email protected] ~]# find /boot -print
/boot
/boot/message
/boot/initrd-2.6.18-128.el5.img
....以下省略....
# 透过这个 find 我们可以找到 /boot 底下应该要存在的档名!包括文件与目录

[[email protected] ~]# find /boot | cpio -ocvB > /tmp/boot.cpio
[[email protected] ~]# ll -h /tmp/boot.cpio
-rw-r--r-- 1 root root 16M Dec 17 23:30 /tmp/boot.cpio
      
范例:将刚刚的文件给他在 /root/ 目录下解开
[[email protected] ~]# cpio -idvc < /tmp/boot.cpio
[[email protected] ~]# ll /root/boot
# 你可以自行比较一下 /root/boot 与 /boot 的内容是否一模一样!
      

其实系统里面已经含有一个使用 cpio 创建的文件喔!那就是 /boot/initrd-xxx 这个文件啦!

# 1. 我们先来看看该文件是属於什么文件格式,然后再加以处理:
[[email protected] ~]# file /boot/initrd-2.6.18-128.el5.img
/boot/initrd-2.6.18-128.el5.img: gzip compressed data, ...
# 唔!看起来似乎是使用 gzip 进行压缩过~那如何处理呢?

# 2. 透过更名,将该文件添加扩展名,然后予以解压缩看看:
[[email protected] ~]# mkdir initrd
[[email protected] ~]# cd initrd
[[email protected] initrd]# cp /boot/initrd-2.6.18-128.el5.img initrd.img.gz
[[email protected] initrd]# gzip -d initrd.img.gz
[[email protected] initrd]# ll
-rw------- 1 root root 5408768 Dec 17 23:53 initrd.img
[[email protected] initrd]# file initrd.img
initrd.img: ASCII cpio archive (SVR4 with no CRC)
# 嘿嘿!露出马脚了吧!确实是 cpio 的文件档喔!

# 3. 开始使用 cpio 解开此文件:
[[email protected] initrd]# cpio -iduvc < initrd.img
sbin
init
sysroot
....以下省略....
# 瞧!这样就将这个文件解开罗!这样了解乎?      

继续阅读