Linux 压缩与打包

一. linux压缩后缀解释说明

*.Z         compress 程序压缩的文件;

*.gz        gzip 程序压缩的文件;

*.bz2       bzip2 程序压缩的文件;

*.tar       tar 程序打包的数据,并没有压缩过;

*.tar.gz    tar 程序打包的文件,其中并且经过 gzip 的压缩

*.tar.bz2   tar 程序打包的文件,其中并且经过 bzip2 的压缩

二. gzip, zcat

[root@www ~]# gzip [-cdtv#] 档名

[root@www ~]# zcat 档名.gz

选项与参数:

-c  :将压缩的数据输出到萤幕上,可透过数据流重导向来处理;

-d  :解压缩的参数;

-t  :可以用来检验一个压缩档的一致性~看看文件有无错误;

-v  :可以显示出原文件/压缩文件的压缩比等资讯;

-#  :压缩等级,-1 最快,但是压缩比最差、-9 最慢,但是压缩比最好!默认是 -6
范例一:将 /etc/man.config 复制到 /tmp ,并且以 gzip 压缩

[root@www ~]# cd /tmp 

[root@www tmp]# cp /etc/man.config .

[root@www tmp]# gzip -v man.config

man.config:      56.1% -- replaced with man.config.gz

[root@www tmp]# ll /etc/man.config /tmp/man*

-rw-r--r-- 1 root root 4617 Jan  6  2007 /etc/man.config

-rw-r--r-- 1 root root 2684 Nov 10 17:24 /tmp/man.config.back.Z

-rw-r--r-- 1 root root 2057 Nov 10 17:14 /tmp/man.config.gz  <==gzip压缩比较佳

范例二:由於 man.config 是文字档,请将范例一的压缩档的内容读出来!

[root@www tmp]# zcat man.config.gz

# 由於 man.config 这个原本的文件是是文字档,因此我们可以尝试使用 zcat  去读取!

# 此时萤幕上会显示 man.config.gz 解压缩之后的文件内容!



范例三:将范例一的文件解压缩

[root@www tmp]# gzip -d man.config.gz

# 不要使用 gunzip 这个命令,不好背!使用 gzip -d 来进行解压缩!

# 与 gzip 相反, gzip -d 会将原本的 .gz 删除,产生原本的 man.config 文件。



范例四:将范例三解开的 man.config 用最佳的压缩比压缩,并保留原本的文件

[root@www tmp]# gzip -9 -c man.config > man.config.gz
gzip压缩范例

三. bzip2,bzcat

[root@www ~]# bzip2 [-cdkzv#] 档名

[root@www ~]# bzcat 档名.bz2

选项与参数:

-c  :将压缩的过程产生的数据输出到萤幕上!

-d  :解压缩的参数

-k  :保留原始文件,而不会删除原始的文件喔!

-z  :压缩的参数

-v  :可以显示出原文件/压缩文件的压缩比等资讯;

-#  :与 gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!
范例一:将刚刚的 /tmp/man.config 以 bzip2 压缩

[root@www tmp]# bzip2 -z man.config 

# 此时 man.config 会变成 man.config.bz2 !



范例二:将范例一的文件内容读出来!

[root@www tmp]# bzcat man.config.bz2

# 此时萤幕上会显示 man.config.bz2 解压缩之后的文件内容!!



范例三:将范例一的文件解压缩

[root@www tmp]# bzip2 -d man.config.bz2



范例四:将范例三解开的 man.config 用最佳的压缩比压缩,并保留原本的文件

[root@www tmp]# bzip2 -9 -c man.config > man.config.bz2
bzip2范例

四. 打包命令

  • 压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称 
  • 查 询:tar -jtv -f filename.tar.bz2 
  • 解压缩:tar -jxv -f filename.tar.bz2 -C 欲解压缩的目录
[root@www ~]# tar [-j|-z] [cv] [-f 创建的档名] filename... <==打包与压缩

[root@www ~]# tar [-j|-z] [tv] [-f 创建的档名]             <==察看档名

[root@www ~]# tar [-j|-z] [xv] [-f 创建的档名] [-C 目录]   <==解压缩

选项与参数:

-c  :创建打包文件,可搭配 -v 来察看过程中被打包的档名(filename)

-t  :察看打包文件的内容含有哪些档名,重点在察看『档名』就是了;

-x  :解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开

      特别留意的是, -c, -t, -x 不可同时出现在一串命令列中。

-j  :透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2

-z  :透过 gzip  的支持进行压缩/解压缩:此时档名最好为 *.tar.gz

-v  :在压缩/解压缩的过程中,将正在处理的档名显示出来!

-f filename:-f 后面要立刻接要被处理的档名!建议 -f 单独写一个选项罗!

-C 目录    :这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。



其他后续练习会使用到的选项介绍:

-p  :保留备份数据的原本权限与属性,常用於备份(-c)重要的配置档

-P  :保留绝对路径,亦即允许备份数据中含有根目录存在之意;

--exclude=FILE:在压缩的过程中,不要将 FILE 打包! 
tar参数详解
[root@www ~]# tar -zpcv -f /root/etc.tar.gz /etc

tar: Removing leading `/' from member names  <==注意这个警告信息

/etc/

....中间省略....

/etc/esd.conf

/etc/crontab

# 由於加上 -v 这个选项,因此正在作用中的档名就会显示在萤幕上。

# 如果你可以翻到第一页,会发现出现上面的错误信息!底下会讲解。

# 至於 -p 的选项,重点在於『保留原本文件的权限与属性』之意。



[root@www ~]# tar -jpcv -f /root/etc.tar.bz2 /etc

# 显示的信息会跟上面一模一样罗!



[root@www ~]# ll /root/etc*

-rw-r--r-- 1 root root  8740252 Nov 15 23:07 /root/etc.tar.bz2

-rw-r--r-- 1 root root 13010999 Nov 15 23:01 /root/etc.tar.gz

[root@www ~]# du -sm /etc

118     /etc

# 为什么建议您使用 -j 这个选项?从上面的数值你可以知道了吧?^_^
tar压缩范例
[root@www ~]# tar -jtv -f /root/etc.tar.bz2

....前面省略....

-rw-r--r-- root/root  1016 2008-05-25 14:06:20 etc/dbus-1/session.conf

-rw-r--r-- root/root   153 2007-01-07 19:20:54 etc/esd.conf

-rw-r--r-- root/root   255 2007-01-06 21:13:33 etc/crontab

范例:将档名中的(根)目录也备份下来,并察看一下备份档的内容档名

[root@www ~]# tar -jpPcv -f /root/etc.and.root.tar.bz2 /etc

....中间过程省略....

[root@www ~]# tar -jtf /root/etc.and.root.tar.bz2

/etc/dbus-1/session.conf

/etc/esd.conf

/etc/crontab

# 这次查阅档名不含 -v 选项,所以仅有档名而已!没有详细属性/权限等参数。
tar查看范例
#解压在当前目录下
[root@www ~]# tar -jxv -f /root/etc.tar.bz2

[root@www ~]# ll

....(前面省略)....

drwxr-xr-x 105 root root    12288 Nov 11 04:02 etc

....(后面省略)....


#解压在指定目录下
[root@www ~]# tar -jxv -f /root/etc.tar.bz2 -C /tmp

[root@www ~]# ll /tmp

....(前面省略)....

drwxr-xr-x 105 root root    12288 Nov 11 04:02 etc

....(后面省略)....


#仅解开单一文件的方法

# 1. 先找到我们要的档名,假设解开 shadow 文件好了:

[root@www ~]# tar -jtv -f /root/etc.tar.bz2 | grep 'shadow'

-r-------- root/root  1230 2008-09-29 02:21:20 etc/shadow-

-r-------- root/root   622 2008-09-29 02:21:20 etc/gshadow-

-r-------- root/root   636 2008-09-29 02:21:25 etc/gshadow

-r-------- root/root  1257 2008-09-29 02:21:25 etc/shadow  <==这是我们要的!
# 2. 将该文件解开!语法与实际作法如下:

[root@www ~]# tar -jxv -f 打包档.tar.bz2 待解开档名

[root@www ~]# tar -jxv -f /root/etc.tar.bz2 etc/shadow

etc/shadow

[root@www ~]# ll etc

total 8

-r-------- 1 root root 1257 Sep 29 02:21 shadow  <==呦喝!只有一个文件啦!


#打包某目录,但不含该目录下的某些文件之作法
[root@www ~]# tar -jcv  -f /root/system.tar.bz2 --exclude=/root/etc* --exclude=/root/system.tar.bz2  /etc /root
tar解压范例
# 1. 先由 find 找出比 /etc/passwd 还要新的文件

[root@www ~]# find /etc -newer /etc/passwd

....(过程省略)....

# 此时会显示出比 /etc/passwd 这个文件的 mtime 还要新的档名,





[root@www ~]# ll /etc/passwd

-rw-r--r-- 1 root root 1945 Sep 29 02:21 /etc/passwd



# 2. 好了,那么使用 tar 来进行打包吧!日期为上面看到的 2008/09/29

[root@www ~]# tar -jcv -f /root/etc.newer.then.passwd.tar.bz2 

> --newer-mtime="2008/09/29" /etc/*

....(中间省略)....

/etc/smartd.conf    <==真的有备份的文件

....(中间省略)....

/etc/yum.repos.d/   <==目录都会被记录下来!

tar: /etc/yum.repos.d/CentOS-Base.repo: file is unchanged; not dumped

# 最后行显示的是『没有被备份的』,亦即 not dumped 的意思!



# 3. 显示出文件即可

[root@www ~]# tar -jtv -f /root/etc.newer.then.passwd.tar.bz2 | 

> grep -v '/$' 

# 透过这个命令可以呼叫出 tar.bz2 内的结尾非 / 的档名!就是我们要的啦!
仅备份比某时刻新的范例
#类似于copy的效果:
# 1. 将 /etc 整个目录一边打包一边在 /tmp 解开
[root@www ~]# cd /tmp
[root@www tmp]# tar -cvf - /etc | tar -xvf -
# 这个动作有点像是 cp -r /etc /tmp 啦~依旧是有其有用途的!
# 要注意的地方在於输出档变成 - 而输入档也变成 - ,又有一个 | 存在~
# 这分别代表 standard output, standard input 与管线命令啦!
# 简单的想法中,你可以将 - 想成是在内存中的一个装置(缓冲区)。


#备份到磁带机:
tar 除了可以将数据打包成为文件之外,还能够将文件打包到某些特别的装置去,举例来说, 磁带机 (tape) 就是一个常见的例子。
磁带机由於是一次性读取/写入的装置,因此我们不能够使用类似 cp 等命令来复制的! 那如果想要将 /home, /root, /etc 备份到磁带机 (/dev/st0) 时,
就可以使用:『tar -cv -f /dev/st0 /home /root /etc』,很简单容易吧! 磁带机用在备份 (尤其是企业应用) 是很常见的工作喔!

五. 完整备份工具dump,restore

dump最主要的功能是可以完整的对文件系统进行整体备份(备份级别0),而且可以实现在此完整备份的基础上进行增量备份(备份级别1-9,依次增加),还原的时候先还原备份级别0,再还原1,依次增加。

dump也可以进行备份整个目录,但是限制是只能进行完整备份(备份级别0),同时不支持 -u 选项,亦即无法创建 /etc/dumpdates 这个各别 level 备份的时间记录档。

1. 如下为dump的备份命令

[root@www ~]# dump [-Suvj] [-level] [-f 备份档] 待备份数据

[root@www ~]# dump -W

选项与参数:

-S    :仅列出后面的待备份数据需要多少磁碟空间才能够备份完毕;

-u    :将这次 dump 的时间记录到 /etc/dumpdates 文件中;

-v    :将 dump 的文件过程显示出来;

-j    :加入 bzip2 的支持!将数据进行压缩,默认 bzip2 压缩等级为 2

-level:就是我们谈到的等级,从 -0 ~ -9 共十个等级;

-f    :有点类似 tar 啦!后面接产生的文件,亦可接例如 /dev/st0 装置档名等

-W    :列出在 /etc/fstab 里面的具有 dump 配置的 partition 是否有备份过?
# 1. 先找出系统中最小的那个文件系统,如下所示:

[root@www ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/hdc2             9.5G  3.7G  5.3G  42% /

/dev/hdc3             4.8G  651M  3.9G  15% /home

/dev/hdc1              99M   11M   83M  12% /boot  <==看起来最小的就是他啦!

tmpfs                 363M     0  363M   0% /dev/shm



# 2. 先测试一下,如果要备份此文件系统,需多少容量?

[root@www ~]# dump -S /dev/hdc1

5630976     <==注意一下,这个单位是 bytes ,所以差不多是 5.6MBytes。



# 3. 将完整备份的档名记录成为 /root/boot.dump ,同时升级记录档:

[root@www ~]# dump -0u -f /root/boot.dump /boot

  DUMP: Date of this level 0 dump: Tue Dec  2 02:53:45 2008 <==记录等级与备份时间

  DUMP: Dumping /dev/hdc1 (/boot) to /root/boot.dump        <==dump的来源与目标

  DUMP: Label: /boot                                        <==文件系统的 label

  DUMP: Writing 10 Kilobyte records

  DUMP: mapping (Pass I) [regular files]                    <==开始进行文件对应

  DUMP: mapping (Pass II) [directories]

  DUMP: estimated 5499 blocks.                              <==评估整体block数量

  DUMP: Volume 1 started with block 1 at: Tue Dec  2 02:53:46 2008

  DUMP: dumping (Pass III) [directories]                    <==开始 dump 工作

  DUMP: dumping (Pass IV) [regular files]

  DUMP: Closing /root/boot.dump                             <==结束写入备份档

  DUMP: Volume 1 completed at: Tue Dec  2 02:53:47 2008

  DUMP: Volume 1 5550 blocks (5.42MB)                       <==最终备份数据容量

  DUMP: Volume 1 took 0:00:01

  DUMP: Volume 1 transfer rate: 5550 kB/s

  DUMP: 5550 blocks (5.42MB) on 1 volume(s)

  DUMP: finished in 1 seconds, throughput 5550 kBytes/sec

  DUMP: Date of this level 0 dump: Tue Dec  2 02:53:45 2008

  DUMP: Date this dump completed:  Tue Dec  2 02:53:47 2008

  DUMP: Average transfer rate: 5550 kB/s

  DUMP: DUMP IS DONE

# 在命令的下达方面,dump 后面接 /boot 或 /dev/hdc1 都可以的!

# 而运行 dump 的过程中会出现如上的一些信息,您可以自行仔细的观察!



[root@www ~]# ll /root/boot.dump /etc/dumpdates

-rw-rw-r-- 1 root disk      43 Dec  2 02:53 /etc/dumpdates

-rw-r--r-- 1 root root 5683200 Dec  2 02:53 /root/boot.dump

# 由於加上 -u 的选项,因此 /etc/dumpdates 该文件的内容会被升级!注意,

# 这个文件仅有在 dump 完整的文件系统时才有支持主动升级的功能。



# 4. 观察一下系统主动创建的记录档:

[root@www ~]# cat /etc/dumpdates

/dev/hdc1     0   Tue Dec  2 02:53:47 2008 +0800

[文件系统] [等级] [       ctime 的时间         ]

# 0. 看一下有没有任何文件系统被 dump 过的数据?

[root@www ~]# dump -W

Last dump(s) done (Dump '>' file systems):

> /dev/hdc2     (     /) Last dump: never

> /dev/hdc3     ( /home) Last dump: never

  /dev/hdc1     ( /boot) Last dump: Level 0, Date Tue Dec  2 02:53:47 2008

# 如上列的结果,该结果会捉出 /etc/fstab 里面第五栏位配置有需要 dump 的 

# partition,然后与 /etc/dumpdates 进行比对,可以得到上面的结果啦!

# 尤其是第三行,可以显示我们曾经对 /dev/hdc1 进行过 dump 的备份动作喔!



# 1. 先恶搞一下,创建一个大约 10 MB 的文件在 /boot 内:

[root@www ~]# dd if=/dev/zero of=/boot/testing.img bs=1M count=10

10+0 records in

10+0 records out

10485760 bytes (10 MB) copied, 0.166128 seconds, 63.1 MB/s



# 2. 开始创建差异备份档,此时我们使用 level 1 吧:

[root@www ~]# dump -1u -f /root/boot.dump.1 /boot

....(中间省略)....



[root@www ~]# ll /root/boot*

-rw-r--r-- 1 root root  5683200 Dec  2 02:53 /root/boot.dump

-rw-r--r-- 1 root root 10547200 Dec  2 02:56 /root/boot.dump.1

# 看看文件大小,岂不是就是刚刚我们所创建的那个大文件的容量吗? ^_^



# 3. 最后再看一下是否有记录 level 1 备份的时间点呢?

[root@www ~]# dump -W

Last dump(s) done (Dump '>' file systems):

> /dev/hdc2     (     /) Last dump: never

> /dev/hdc3     ( /home) Last dump: never

> /dev/hdc1     ( /boot) Last dump: Level 1, Date Tue Dec  2 02:56:33 2008

....(中间省略)....
dump完整备份文件系统范例
# 让我们将 /etc 整个目录透过 dump 进行备份,且含压缩功能

[root@www ~]# dump -0j -f /root/etc.dump.bz2 /etc

  DUMP: Date of this level 0 dump: Tue Dec  2 12:08:22 2008

  DUMP: Dumping /dev/hdc2 (/ (dir etc)) to /root/etc.dump.bz2



  DUMP: Label: /1

  DUMP: Writing 10 Kilobyte records

  DUMP: Compressing output at compression level 2 (bzlib)

  DUMP: mapping (Pass I) [regular files]

  DUMP: mapping (Pass II) [directories]

  DUMP: estimated 115343 blocks.

  DUMP: Volume 1 started with block 1 at: Tue Dec  2 12:08:23 2008

  DUMP: dumping (Pass III) [directories]

  DUMP: dumping (Pass IV) [regular files]

  DUMP: Closing /root/etc.dump.bz2

  DUMP: Volume 1 completed at: Tue Dec  2 12:09:49 2008

  DUMP: Volume 1 took 0:01:26

  DUMP: Volume 1 transfer rate: 218 kB/s

  DUMP: Volume 1 124680kB uncompressed, 18752kB compressed, 6.649:1

  DUMP: 124680 blocks (121.76MB) on 1 volume(s)

  DUMP: finished in 86 seconds, throughput 1449 kBytes/sec

  DUMP: Date of this level 0 dump: Tue Dec  2 12:08:22 2008

  DUMP: Date this dump completed:  Tue Dec  2 12:09:49 2008

  DUMP: Average transfer rate: 218 kB/s

  DUMP: Wrote 124680kB uncompressed, 18752kB compressed, 6.649:1

  DUMP: DUMP IS DONE

# 上面特殊字体的部分显示:原本有 124680kb  的容量,被压缩成为 18752kb,

# 整个压缩比为 6.649:1 ,还可以的压缩情况啦!
dump备份单一目录范例

2. 如下为dump的恢复命令

[root@www ~]# restore -t [-f dumpfile] [-h]        <==用来察看 dump 档

[root@www ~]# restore -C [-f dumpfile] [-D 挂载点] <==比较dump与实际文件

[root@www ~]# restore -i [-f dumpfile]             <==进入互动模式

[root@www ~]# restore -r [-f dumpfile]             <==还原整个文件系统

选项与参数:

相关的各种模式,各种模式无法混用喔!例如不可以写 -tC 啦!

-t  :此模式用在察看 dump 起来的备份档中含有什么重要数据!类似 tar -t 功能;

-C  :此模式可以将 dump 内的数据拿出来跟实际的文件系统做比较,

      最终会列出『在 dump 文件内有记录的,且目前文件系统不一样』的文件;

-i  :进入互动模式,可以仅还原部分文件,用在 dump 目录时的还原!

-r  :将整个 filesystem 还原的一种模式,用在还原针对文件系统的 dump 备份;

其他较常用到的选项功能:

-h  :察看完整备份数据中的 inode 与文件系统 label 等资讯

-f  :后面就接你要处理的那个 dump 文件罗!

-D  :与 -C 进行搭配,可以查出后面接的挂载点与 dump 内有不同的文件!
# 让我们将 /etc 整个目录透过 dump 进行备份,且含压缩功能

[root@www ~]# dump -0j -f /root/etc.dump.bz2 /etc

  DUMP: Date of this level 0 dump: Tue Dec  2 12:08:22 2008

  DUMP: Dumping /dev/hdc2 (/ (dir etc)) to /root/etc.dump.bz2



  DUMP: Label: /1

  DUMP: Writing 10 Kilobyte records

  DUMP: Compressing output at compression level 2 (bzlib)

  DUMP: mapping (Pass I) [regular files]

  DUMP: mapping (Pass II) [directories]

  DUMP: estimated 115343 blocks.

  DUMP: Volume 1 started with block 1 at: Tue Dec  2 12:08:23 2008

  DUMP: dumping (Pass III) [directories]

  DUMP: dumping (Pass IV) [regular files]

  DUMP: Closing /root/etc.dump.bz2

  DUMP: Volume 1 completed at: Tue Dec  2 12:09:49 2008

  DUMP: Volume 1 took 0:01:26

  DUMP: Volume 1 transfer rate: 218 kB/s

  DUMP: Volume 1 124680kB uncompressed, 18752kB compressed, 6.649:1

  DUMP: 124680 blocks (121.76MB) on 1 volume(s)

  DUMP: finished in 86 seconds, throughput 1449 kBytes/sec

  DUMP: Date of this level 0 dump: Tue Dec  2 12:08:22 2008

  DUMP: Date this dump completed:  Tue Dec  2 12:09:49 2008

  DUMP: Average transfer rate: 218 kB/s

  DUMP: Wrote 124680kB uncompressed, 18752kB compressed, 6.649:1

  DUMP: DUMP IS DONE

# 上面特殊字体的部分显示:原本有 124680kb  的容量,被压缩成为 18752kb,

# 整个压缩比为 6.649:1 ,还可以的压缩情况啦!
restore观察dump备份详情范例
# 0. 先尝试变更文件系统的内容:

[root@www ~]# cd /boot

[root@www boot]# mv config-2.6.18-128.el5 config-2.6.18-128.el5-back



# 1. 看使进行文件系统与备份文件之间的差异!

[root@www boot]# restore -C -f /root/boot.dump

Dump   date: Tue Dec  2 02:53:45 2008

Dumped from: the epoch

Level 0 dump of /boot on www.vbird.tsai:/dev/hdc1

Label: /boot

filesys = /boot

restore: unable to stat ./config-2.6.18-128.el5: No such file or directory

Some files were modified!  1 compare errors

# 看到上面的特殊字体了吧!那就是有差异的部分!总共有一个文件被变更!

# 我们刚刚确实有更动过该文件,嘿嘿!这样是否能了解?!



# 2. 将文件系统改回来啊!

[root@www boot]# mv config-2.6.18-128.el5-back config-2.6.18-128.el5

[root@www boot]# cd /root

# 1. 先创建一个新的 partition 来使用,假设我们需要的是 150M 的容量

[root@www ~]# fdisk /dev/hdc

Command (m for help): n

First cylinder (2335-5005, default 2335): <==这里按 Enter

Using default value 2335

Last cylinder or +size or +sizeM or +sizeK (2335-5005, default 5005): +150M

Command (m for help): p

....中间省略....

/dev/hdc8            2335        2353      152586   83  Linux



Command (m for help): w



[root@www ~]# partprobe   <==很重要的动作!别忘记!

# 这样就能够创建一个 /dev/hdc8 的 partition ,然后继续格式化吧!



[root@www ~]# mkfs -t ext3 /dev/hdc8

[root@www ~]# mount /dev/hdc8 /mnt



# 2. 开始进行还原的动作!请您务必到新文件系统的挂载点底下去!

[root@www ~]# cd /mnt

[root@www mnt]# restore -r -f /root/boot.dump

restore: ./lost+found: File exists
restore比较差异并整体还原范例
[root@www ~]# cd /mnt

[root@www mnt]# restore -i -f /root/etc.dump

restore > 

# 此时你就已经进入 restore 的互动模式画面中!要注意的是:

# 你目前已经在 etc.dump 这个文件内了!所有的动作都是在 etc.dump 内!



restore > help

Available commands are:

        ls [arg] - list directory          <==列出 etc.dump 内的文件或目录

        cd arg - change directory          <==在 etc.dump 内变更目录

        pwd - print current directory      <==列出在 etc.dump 内的路径档名

        add [arg] - add `arg' to list of files to be extracted 

        delete [arg] - delete `arg' from list of files to be extracted

        extract - extract requested files

# 上面三个命令是重点!各命令的功能为:

# add file    :将 file 加入等一下要解压缩的文件列表中

# delete file :将 file 移除出解压缩的列表,并非删除 etc.dump 内的文件!别误会!^_^

# extract     :开始将刚刚选择的文件列表解压缩了去!

        setmodes - set modes of requested directories

        quit - immediately exit program

        what - list dump header information

        verbose - toggle verbose flag (useful with ``ls'')

        prompt - toggle the prompt display

        help or `?' - print this list



restore > ls

.:

etc/  <==会显示出在 etc.dump 内主要的目录,因为我们备份 /etc ,所以档名为此!



restore > cd etc                  <==在 etc.dump 内变换路径到 etc 目录下

restore > pwd                     <==列出本目录的档名为?

/etc

restore > ls passwd shadow group  <==看看,真的有这三个文件喔!

passwd

shadow

group

restore > add passwd shadow group <==加入解压缩列表

restore > delete group            <==加错了!将 group 移除解压缩列表

restore > ls passwd shadow group

*passwd  <==有要被解压缩的,档名之前会出现 * 的符号呢!

*shadow

group

restore > extract                 <==开始进行解压缩去!

You have not read any volumes yet.   <==这里会询问你需要的volume

Unless you know which volume your file(s) are on you should start

with the last volume and work towards the first.

Specify next volume # (none if no more volumes): 1 <==只有一个 volume

set owner/mode for '.'? [yn] n <==不需要修改权限



restore > quit                    <==离开 restore 的功能



[root@www ~]# ll -d etc

drwxr-xr-x 2 root root 1024 Dec 15 17:49 etc  <==解压缩后,所创建出来的目录啦!

[root@www ~]# ll etc

total 6

-rw-r--r-- 1 root root 1945 Sep 29 02:21 passwd

-r-------- 1 root root 1257 Sep 29 02:21 shadow
仅还原部分的restore互动模式范例

六. dd命令详解

dd命令非常强大,可以备份整块磁盘,由于备份整块磁盘需要复制 boot sector 的区块,所以使用 cp 或者是 tar 这种命令是无法达成需求的!此时 dd 就派的上用场了。新分割出来的 partition 不需要经过格式化,因为 dd 可以将原本旧的 partition 上面,将 sector 表面的数据整个复制过来!当然连同 superblock, boot sector, meta data 等等通通也会复制过来!是否很有趣呢?未来你想要建置两颗一模一样的磁碟时,只要下达类似: dd if=/dev/sda of=/dev/sdb,就能够让两颗磁碟一模一样,甚至 /dev/sdb 不需要分割与格式化,因为该命令可以将 /dev/sda 内的所有数据,包括 MBR 与 partition table 也复制到 /dev/sdb 说!

所以说, tar 可以用来备份关键数据,而 dd 则可以用来备份整颗 partition 或 整颗 disk ,这样就明确了两个强大工具的用处了。

[root@www ~]# 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 的意思。
[root@www ~]# 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 当中

[root@www ~]# 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

[root@www ~]# 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 这个命令啦~



范例二:将自己的磁碟之第一个磁区备份下来

[root@www ~]# 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 进行备份哩!



范例三:找出你系统最小的那个分割槽,并且将他备份下来:

[root@www ~]# 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  <==就捉他好了!

[root@www ~]# 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

[root@www ~]# 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 就可以啦~厉害厉害!
dd命令范例1
你想要将你的 /dev/hdc1 进行完整的复制到另一个 partition 上,请使用你的系统上面未分割完毕的容量再创建一个与 /dev/hdc1 差不多大小的分割槽 (只能比 /dev/hdc1 大,不能比他小!),然后将之进行完整的复制 (包括需要复制 boot sector 的区块)。
答:
由於需要复制 boot sector 的区块,所以使用 cp 或者是 tar 这种命令是无法达成需求的!此时那个 dd 就派的上用场了。你可以这样做:
# 1. 先进行分割的动作

[root@www ~]# fdisk -l /dev/hdc

   Device Boot   Start    End    Blocks   Id  System

/dev/hdc1   *        1     13    104391   83  Linux

# 上面鸟哥仅撷取重要的数据而已!我们可以看到 /dev/hdc1 仅有 13 个磁柱



[root@www ~]# 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 罗!



[root@www ~]# partprobe



# 2. 不需要格式化,直接进行 sector 表面的复制!

[root@www ~]# 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



[root@www ~]# mount /dev/hdc9 /mnt

[root@www ~]# df

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/hdc1               101086     21408     74459  23% /boot

/dev/hdc9               101086     21408     74459  23% /mnt

# 这两个玩意儿会『一模一样』喔!

[root@www ~]# umount /mnt
dd命令范例2

七. cpio命令详解

cpio 可以备份任何东西,包括装置设备文件。不过 cpio 得要配合类似 find 等可以找到档名的命令来告知 cpio 该被备份的数据在哪里。

[root@www ~]# cpio -ovcB  > [file|device] <==备份

[root@www ~]# cpio -ivcdu < [file|device] <==还原

[root@www ~]# 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 方式储存 
原文地址:https://www.cnblogs.com/wangzengyi/p/12392118.html