鸟哥学习笔记9:文件与文件系统的压缩与打包

第九章 文件与文件系统的压缩与打包

9.1 压缩文件的用途与技术

压缩:通过一些压缩算法对文件的存储体积进行压缩,以减少文件所在磁盘空间。

压缩比:文件压缩前与压缩后的文件所占用磁盘空间的比值。


9.2 Linux 系统常见的压缩命令

compress

特点:
  • 默认压缩(解压缩)后不保留源文件
  • 压缩文件扩展名为”.Z”
  • 压缩(解压缩)时自动为压缩文件添加扩展名
  • 只能对单个文件进行压缩
  • CentOS默认不安装此命令
yum安装ncompress
yum install ncompress
基本使用方法
[root@www ~]# compress [-rcv] 文件或目录  <==这里是压缩
[root@www ~]# uncompress 文件.Z           <==这里是解压缩
选项与参数:
-r  :可以连同目录下的文件也同时给予压缩呢!
-c  :将压缩数据输出成为 standard output (输出到萤幕)
-v  :可以秀出压缩后的文件资讯以及压缩过程中的一些档名变化。
范例一:将 /etc/man.config 复制到 /tmp ,并加以压缩
[root@www ~]# cd /tmp
[root@www tmp]# cp /etc/man.config .
[root@www tmp]# compress -v man.config
man.config:  -- replaced with man.config.Z Compression: 41.86%
[root@www tmp]# ls -l /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:14 /tmp/man.config.Z <==经过压缩的文件!
范例二:将刚刚的压缩档解开
[root@www tmp]# uncompress man.config.Z
[root@www tmp]# ll man*
-rw-r--r-- 1 root root 4617 Nov 10 17:14 man.config

范例三:将 man.config 压缩成另外一个文件来备份

[root@www tmp]# compress -c man.config > man.config.back.Z
[root@www tmp]# ll man*
-rw-r--r-- 1 root root 4617 Nov 10 17:14 man.config
-rw-r--r-- 1 root root 2684 Nov 10 17:24 man.config.back.Z
# 这个 -c 的选项比较有趣!他会将压缩过程的数据输出到萤幕上,而不是写入成为 
# *.Z 的压缩档。所以,我们可以透过数据流重导向的方法将数据输出成为另一个档名。
# 关於数据流重导向,我们会在第十一章 bash 详细谈论的啦!

通过使用-c选项,可以使compress达到压缩文件时保留源文件的效果。


gzip

特点:
  • 默认压缩(解压缩)后不保留源文件
  • 压缩文件扩展名为”.gz”
  • 压缩(解压缩)时自动为压缩文件添加扩展名
  • 只能对单个文件进行压缩
相关命令:
  • gzip 压缩文件
  • gunzip 解压缩文件
  • zcat 显示压缩文件内容
  • zless 逐行显示压缩文件内容
  • zdiff 比较压缩文件的差异
  • zcmp 比较压缩文件的差异
基本使用方法
[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

同样的,通过使用-c选项,可以使gzip达到压缩文件时保留源文件的效果。


bzip2

特点
  • 默认压缩(解压缩)后不保留源文件
  • 压缩文件扩展名为”.bz2”
  • 压缩(解压缩)时自动为压缩文件添加扩展名
  • 只能对单个文件进行压缩
相关命令
  • bzip2 压缩文件
  • bunzip2 解压缩文件
  • 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

zip, unzip

特点
  • 默认压缩(解压缩)后保留源文件
  • 压缩文件扩展名为”.zip”
  • 压缩时需要手动指定压缩文件扩展名
  • 可以将多个目录或文件压缩成单个文件
相关命令
  • zip 压缩文件
  • unzip 解压缩文件
  • zipcloak 加密zipfile中的条目
  • zipnote 编辑zipfile的comments,以及重命名zipfile中的文件
  • zipsplit 将一个zipfile分割成多个较小的zipfile

9.3 打包命令:tar

compress, gzip, bzip2 命令都只能对单个文件进行压缩,gzip和bzip2可以对目录进行压缩,但指的是“对目录中的文件分别进行压缩”,并不是将目录打包压缩。

tar可以将多个目录或文件打包成单个大文件。

tarfile是指仅用tar打包后的.tar文件。
tarball是指使用tar打包后还压缩了的tar.gz或tar.bz2文件。

特点:

  • 默认打包(解包)后保留源文件
  • tar包文件扩展名为“.tar”
  • tar打包时不会自动命名tar包文件的扩展名,需手动指定扩展名
  • 可以在打包(解包)的同时进行文件的压缩(解压缩)

基本使用方法

打包或压缩:

[root@www ~]# tar [-j|-z] -c[v] [-f 创建的档名[.tar|.tar.gz|.tar.bz2]] filename...

察看档名:

[root@www ~]# tar [-j|-z] -t[v] [-f 创建的档名[.tar|.tar.gz|.tar.bz2]]

解包或解压缩:

[root@www ~]# tar [-j|-z] -x[v] [-f 创建的档名[.tar|.tar.gz|.tar.bz2]] [tar包中待解压的特定文件] [-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 打包!

仅解压单一文件

# 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  <==这是我们要的!
# 先搜寻重要的档名!其中那个 grep 是『撷取』关键字的功能!我们会在第三篇说明!
# 这里您先有个概念即可!那个管线 | 配合 grep 可以撷取关键字的意思!

# 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  <==呦喝!只有一个文件啦!
# 很有趣!此时只会解开一个文件而已!不过,重点是那个档名!你要找到正确的档名。
# 在本例中,你不能写成 /etc/shadow !因为记录在 etc.tar.bz2 内的档名之故!

9.4 完整备份工具:dump,restore

dump

dump支持对单个文件系统或单个目录进行备份,并支持指定备份级别(level)。

image

level 0为完整备份;level 1 是比较目前的文件系统与 level 0 之间的差异,然后备份变化的文件;level 2 则是与 level 1 的增量备份,以此类推!


dump备份文件系统和备份目录的区别
  • 对单一文件系统备份
    1. 可以使用完整的dump功能
    2. 可以使用0~9个level进行备份
    3. 可以使用挂载点和设备文件名来进行备份
  • 对目录(而非文件系统)进行备份
    1. 所有备份数据都只能位于该目录之下
    2. 只能使用level 0进行完整备份
    3. 不支持 -u 选项,即无法创建/etc/dumpdates这个level备份的时间记录
相关配置文件
  • /etc/dumpdates
基本使用方法
[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 是否有备份过?

restore

dump备份后,还原时需使用restore命令。

基本使用方法
[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 内有不同的文件!

9.5 光盘写入工具

命令行下光盘刻录步骤:
1. 使用命令 mkisofs 将待备份的数据制作成iso镜像文件;
2. 使用命令 cdrecord 进行光盘刻录;


mkisofs

基本使用方法
[root@www ~]# mkisofs [-o 输出的镜像文件] [-rv] [-m file] 待备份文件.. [-V vol] \
>  -graft-point isodir=systemdir ...
选项与参数:
  • -o :后面接你想要产生的那个映像档档名。
  • -r :命令默认不记录文件属性(UID/GID等)和权限;指定此选项后,透过 Rock Ridge 产生支持 Unix/Linux 的文件数据,可记录文件属性和权限信息;
  • -v :显示建置 ISO 文件的过程
  • -m file :-m 为排除文件 (exclude) 的意思,后面的文件不备份到映像档中
  • -V vol :创建 Volume,有点像 Windows 在文件总管内看到的 CD title 的东西
  • -graft-point :允许对文件名嫁接点的使用。如果这个选项被使用,所有文件名都会针对嫁接点被检查。文件名被第一个非转义等号(=)所分隔。如果-graft-points被指定,所有出现’\’和’=’的地方都必须用反斜杠‘\’转义。

1. 将 /etc 和 /boot 制作成镜像文件test.iso

[root@CentOS ~]# mkisofs -r -v -o /tmp/test.iso /boot /etc
I: -input-charset not specified, using utf-8 (detected in locale settings)
genisoimage 1.1.9 (Linux)
Scanning /boot
Scanning /boot/efi
Scanning /boot/efi/EFI
Scanning /boot/efi/EFI/redhat
Scanning /boot/lost+found
Scanning /boot/grub
Scanning /etc
Scanning /etc/dbus-1
Scanning /etc/dbus-1/system.d
Scanning /etc/cron.daily

# ...输出太长,在此略过...

Scanning /etc/yum/protected.d
Scanning /etc/xinetd.d
Scanning /etc/NetworkManager
Scanning /etc/NetworkManager/dispatcher.d
Scanning /etc/dracut.conf.d
Using SYSTE000.;1 for  /system-release-cpe (system-release)
Using SBIN_000.X86;1 for  /etc/alternatives/sbin-ip6tables-multi.x86_64 (sbin-ip6tables-save.x86_64)
Using MAN_I000.X86;1 for  /etc/alternatives/man-iptables.x86_64 (man-iptables-save.x86_64)
Using MAN_I001.X86;1 for  /etc/alternatives/man-ip6tables.x86_64 (man-ip6tables-save.x86_64)
Using MAN_I002.X86;1 for  /etc/alternatives/man-ip6tables-save.x86_64 (man-ip6tables-restore.x86_64)
Using MTA_S000.;1 for  /etc/alternatives/mta-sendmailman (mta-sendmail)
Using LIBIP000.X86;1 for  /etc/alternatives/libip6tc000.x86_64 (libip6tc0.x86_64)
Using SBIN_001.X86;1 for  /etc/alternatives/sbin-iptables-restore.x86_64 (sbin-iptables-save.x86_64)
Using SBIN_002.X86;1 for  /etc/alternatives/sbin-ip6tables-save.x86_64 (sbin-ip6tables-restore.x86_64)
Using SBIN_003.X86;1 for  /etc/alternatives/sbin-iptables-save.x86_64 (sbin-iptables-multi.x86_64)
Using MTA_N000.;1 for  /etc/alternatives/mta-newaliasesman (mta-newaliases)
Using MAN_I003.X86;1 for  /etc/alternatives/man-iptables-save.x86_64 (man-iptables-restore.x86_64)
Using LIBIP001.X86;1 for  /etc/alternatives/libip4tc000.x86_64 (libip4tc0.x86_64)
Using LIBIP002.X86;1 for  /etc/alternatives/libiptc000.x86_64 (libiptc0.x86_64)
Using MKISO000.;1 for  /etc/alternatives/mkisofs-mkhybrid (mkisofs-mkisofsman)
Using LIBXT000.X86;1 for  /etc/alternatives/libxtables400.x86_64 (libxtables4.x86_64)
Using MTA_M000.;1 for  /etc/alternatives/mta-mailqman (mta-mailq)
Using MAN_I004.X86;1 for  /etc/alternatives/man-iptables-restore.x86_64 (man-iptables-xml.x86_64)
Using SMART000.;1 for  /etc/pam.d/smartcard-auth-ac (smartcard-auth)
Using SYSTE000.;1 for  /etc/pam.d/system-auth-ac (system-auth)
Using PASSW000.;1 for  /etc/pam.d/password-auth (password-auth-ac)
Using FINGE000.;1 for  /etc/pam.d/fingerprint-auth-ac (fingerprint-auth)
Using 70_PE000.RUL;1 for  /etc/udev/rules.d/70-persistent-net.rules (70-persistent-cd.rules)
Using CA_BU000.CRT;1 for  /etc/pki/tls/certs/ca-bundle.trust.crt (ca-bundle.crt)
Using RPM_G000.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 (RPM-GPG-KEY-CentOS-Testing-6)
Using RPM_G001.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-6 (RPM-GPG-KEY-CentOS-Debug-6)
Using RPM_G002.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-6 (RPM-GPG-KEY-CentOS-Security-6)
Using RESTO000.CON;1 for  /etc/selinux/restorecond.conf (restorecond_user.conf)
Using SEMAN000.LOC;1 for  /etc/selinux/targeted/modules/semanage.read.LOCK (semanage.trans.LOCK)
Using UNCON000.PP;1 for  /etc/selinux/targeted/modules/active/modules/unconfined.pp (unconfineduser.pp)
Using OPENS000.PP;1 for  /etc/selinux/targeted/modules/active/modules/openshift-origin.pp (openshift.pp)
Using DEFAU000.;1 for  /etc/selinux/targeted/contexts/default_type (default_contexts)
Using VIRTU000.;1 for  /etc/selinux/targeted/contexts/virtual_image_context (virtual_domain_context)
Using SSH_H000.;1 for  /etc/ssh/ssh_host_rsa_key (ssh_host_key)
Using SSH_H001.;1 for  /etc/ssh/ssh_host_key (ssh_host_dsa_key)
Using SSH_H000.PUB;1 for  /etc/ssh/ssh_host_rsa_key.pub (ssh_host_key.pub)
Using SSH_H001.PUB;1 for  /etc/ssh/ssh_host_key.pub (ssh_host_dsa_key.pub)
Using IPTAB000.;1 for  /etc/sysconfig/iptables (iptables-config)
Using IP6TA000.;1 for  /etc/sysconfig/ip6tables-config (ip6tables)
Using IFDOW000.;1 for  /etc/sysconfig/network-scripts/ifdown-ppp (ifdown-post)
Using IFDOW001.;1 for  /etc/sysconfig/network-scripts/ifdown-ipv6 (ifdown-isdn)
Using IFDOW002.;1 for  /etc/sysconfig/network-scripts/ifdown-isdn (ifdown-ippp)
Using NETWO000.;1 for  /etc/sysconfig/network-scripts/network-functions-ipv6 (network-functions)
Writing:   Initial Padblock                        Start Block 0
Done with: Initial Padblock                        Block(s)    16
Writing:   Primary Volume Descriptor               Start Block 16
Done with: Primary Volume Descriptor               Block(s)    1
Writing:   End Volume Descriptor                   Start Block 17
Done with: End Volume Descriptor                   Block(s)    1
Writing:   Version block                           Start Block 18
Done with: Version block                           Block(s)    1
Writing:   Path table                              Start Block 19
Done with: Path table                              Block(s)    4
Writing:   Directory tree                          Start Block 23
Done with: Directory tree                          Block(s)    191
Writing:   Directory tree cleanup                  Start Block 214
Done with: Directory tree cleanup                  Block(s)    0
Writing:   Extension record                        Start Block 214
Done with: Extension record                        Block(s)    1
Writing:   The File(s)                             Start Block 215
 13.49% done, estimate finish Tue Mar 28 00:48:55 2017
 26.94% done, estimate finish Tue Mar 28 00:48:55 2017
 40.45% done, estimate finish Tue Mar 28 00:48:55 2017
 53.92% done, estimate finish Tue Mar 28 00:48:55 2017
 67.37% done, estimate finish Tue Mar 28 00:48:56 2017
 80.86% done, estimate finish Tue Mar 28 00:48:56 2017
 94.30% done, estimate finish Tue Mar 28 00:48:58 2017
Total translation table size: 0
Total rockridge attributes bytes: 113820
Total directory bytes: 364544
Path table size(bytes): 2098
Done with: The File(s)                             Block(s)    36753
Writing:   Ending Padblock                         Start Block 36968
Done with: Ending Padblock                         Block(s)    150
Max brk space used 107000
37118 extents written (72 MB)

2.刻录完成后,挂载镜像文件,查看挂载点目录下的文件情况

[root@CentOS /]# mkdir /media/dvd
[root@CentOS /]# mount -o loop /tmp/test.iso /media/dvd
[root@CentOS /]# ls /media/dvd/
adjtime                       gcrypt                                 motd            rwtab
aliases                       gnupg                                  mtab            rwtab.d
aliases.db                    group                                  my.cnf          sasl2
alternatives                  group-                                 NetworkManager  screenrc
anacrontab                    grub                                   networks        securetty
audisp                        grub.conf                              nsswitch.conf   security
audit                         gshadow                                openldap        selinux
bash_completion.d             gshadow-                               opt             services
bashrc                        host.conf                              pam.d           sestatus.conf
blkid                         hosts                                  passwd          shadow
centos-release                hosts.allow                            passwd-         shadow-
chkconfig.d                   hosts.deny                             pkcs11          shells
config-2.6.32-573.el6.x86_64  init                                   pki             skel
cron.d                        init.d                                 plymouth        ssh
cron.daily                    initramfs-2.6.32-573.el6.x86_64.img    pm              ssl
cron.deny                     initrd-2.6.32-573.el6.x86_64kdump.img  popt.d          statetab
cron.hourly                   inittab                                postfix         statetab.d
cron.monthly                  inputrc                                ppp             sudo.conf
crontab                       iproute2                               prelink.conf.d  sudoers
cron.weekly                   issue                                  printcap        sudoers.d
crypttab                      issue.net                              profile         sudo-ldap.conf
csh.cshrc                     kdump-adv-conf                         profile.d       symvers-2.6.32-573.el6.x86_64.gz
csh.login                     kdump.conf                             protocols       sysconfig
dbus-1                        krb5.conf                              rc              sysctl.conf
default                       ld.so.cache                            rc0.d           System.map-2.6.32-573.el6.x86_64
depmod.d                      ld.so.conf                             rc1.d           system-release
dhcp                          ld.so.conf.d                           rc2.d           system-release-cpe
DIR_COLORS                    libaudit.conf                          rc3.d           terminfo
DIR_COLORS.256color           libuser.conf                           rc4.d           testfile
DIR_COLORS.lightbgcolor       localtime                              rc5.d           test.img
dracut.conf                   login.defs                             rc6.d           udev
dracut.conf.d                 logrotate.conf                         rc.d            vimrc
dumpdates                     logrotate.d                            rc.local        virc
efi                           lost+found                             rc.sysinit      vmlinuz-2.6.32-573.el6.x86_64
environment                   lvm                                    redhat-release  X11
ethers                        magic                                  resolv.conf     xdg
exports                       makedev.d                              rmt             xinetd.d
favicon.png                   man.config                             rpc             yum
filesystems                   mdadm.conf                             rpm             yum.conf
fstab                         mke2fs.conf                            rsyslog.conf    yum.repos.d
gai.conf                      modprobe.d                             rsyslog.d

可以注意到目录 /boot/etc 下的所有文件和子目录,甚至连lost+found目录,都被放置到了镜像文件的根目录中!

3.使用-graft-point选项来指定待备份目录下的文件和子目录分别被放置到镜像文件根目录下的哪个目录中;

[root@CentOS /]# umount /media/dvd
[root@CentOS /]# mkisofs -r -v -V 'Linux_file' -o /tmp/test2.iso -m /boot/lost+found/ -graft-point /boot=/boot /etc=/etc
I: -input-charset not specified, using utf-8 (detected in locale settings)
genisoimage 1.1.9 (Linux)
Scanning /boot
Scanning /boot/efi
Scanning /boot/efi/EFI
Scanning /boot/efi/EFI/redhat
Scanning /boot/lost+found
Scanning /boot/grub
Scanning /etc
Scanning /etc/dbus-1
Scanning /etc/dbus-1/system.d
Scanning /etc/cron.daily
Scanning /etc/logrotate.d
Scanning /etc/profile.d

# ...输出太多,此处略过...

Scanning /etc/xinetd.d
Scanning /etc/NetworkManager
Scanning /etc/NetworkManager/dispatcher.d
Scanning /etc/dracut.conf.d
Using SYSTE000.;1 for  etc/system-release-cpe (system-release)
Using SBIN_000.X86;1 for  /etc/alternatives/sbin-ip6tables-multi.x86_64 (sbin-ip6tables-save.x86_64)
Using MAN_I000.X86;1 for  /etc/alternatives/man-iptables.x86_64 (man-iptables-save.x86_64)
Using MAN_I001.X86;1 for  /etc/alternatives/man-ip6tables.x86_64 (man-ip6tables-save.x86_64)
Using MAN_I002.X86;1 for  /etc/alternatives/man-ip6tables-save.x86_64 (man-ip6tables-restore.x86_64)
Using MTA_S000.;1 for  /etc/alternatives/mta-sendmailman (mta-sendmail)
Using LIBIP000.X86;1 for  /etc/alternatives/libip6tc000.x86_64 (libip6tc0.x86_64)
Using SBIN_001.X86;1 for  /etc/alternatives/sbin-iptables-restore.x86_64 (sbin-iptables-save.x86_64)
Using SBIN_002.X86;1 for  /etc/alternatives/sbin-ip6tables-save.x86_64 (sbin-ip6tables-restore.x86_64)
Using SBIN_003.X86;1 for  /etc/alternatives/sbin-iptables-save.x86_64 (sbin-iptables-multi.x86_64)
Using MTA_N000.;1 for  /etc/alternatives/mta-newaliasesman (mta-newaliases)
Using MAN_I003.X86;1 for  /etc/alternatives/man-iptables-save.x86_64 (man-iptables-restore.x86_64)
Using LIBIP001.X86;1 for  /etc/alternatives/libip4tc000.x86_64 (libip4tc0.x86_64)
Using LIBIP002.X86;1 for  /etc/alternatives/libiptc000.x86_64 (libiptc0.x86_64)
Using MKISO000.;1 for  /etc/alternatives/mkisofs-mkhybrid (mkisofs-mkisofsman)
Using LIBXT000.X86;1 for  /etc/alternatives/libxtables400.x86_64 (libxtables4.x86_64)
Using MTA_M000.;1 for  /etc/alternatives/mta-mailqman (mta-mailq)
Using MAN_I004.X86;1 for  /etc/alternatives/man-iptables-restore.x86_64 (man-iptables-xml.x86_64)
Using SMART000.;1 for  /etc/pam.d/smartcard-auth-ac (smartcard-auth)
Using SYSTE000.;1 for  /etc/pam.d/system-auth-ac (system-auth)
Using PASSW000.;1 for  /etc/pam.d/password-auth (password-auth-ac)
Using FINGE000.;1 for  /etc/pam.d/fingerprint-auth-ac (fingerprint-auth)
Using 70_PE000.RUL;1 for  /etc/udev/rules.d/70-persistent-net.rules (70-persistent-cd.rules)
Using CA_BU000.CRT;1 for  /etc/pki/tls/certs/ca-bundle.trust.crt (ca-bundle.crt)
Using RPM_G000.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 (RPM-GPG-KEY-CentOS-Testing-6)
Using RPM_G001.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-6 (RPM-GPG-KEY-CentOS-Debug-6)
Using RPM_G002.;1 for  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-6 (RPM-GPG-KEY-CentOS-Security-6)
Using RESTO000.CON;1 for  /etc/selinux/restorecond.conf (restorecond_user.conf)
Using SEMAN000.LOC;1 for  /etc/selinux/targeted/modules/semanage.read.LOCK (semanage.trans.LOCK)
Using UNCON000.PP;1 for  /etc/selinux/targeted/modules/active/modules/unconfined.pp (unconfineduser.pp)
Using OPENS000.PP;1 for  /etc/selinux/targeted/modules/active/modules/openshift-origin.pp (openshift.pp)
Using DEFAU000.;1 for  /etc/selinux/targeted/contexts/default_type (default_contexts)
Using VIRTU000.;1 for  /etc/selinux/targeted/contexts/virtual_image_context (virtual_domain_context)
Using SSH_H000.;1 for  /etc/ssh/ssh_host_rsa_key (ssh_host_key)
Using SSH_H001.;1 for  /etc/ssh/ssh_host_key (ssh_host_dsa_key)
Using SSH_H000.PUB;1 for  /etc/ssh/ssh_host_rsa_key.pub (ssh_host_key.pub)
Using SSH_H001.PUB;1 for  /etc/ssh/ssh_host_key.pub (ssh_host_dsa_key.pub)
Using IPTAB000.;1 for  /etc/sysconfig/iptables (iptables-config)
Using IP6TA000.;1 for  /etc/sysconfig/ip6tables-config (ip6tables)
Using IFDOW000.;1 for  /etc/sysconfig/network-scripts/ifdown-ppp (ifdown-post)
Using IFDOW001.;1 for  /etc/sysconfig/network-scripts/ifdown-ipv6 (ifdown-isdn)
Using IFDOW002.;1 for  /etc/sysconfig/network-scripts/ifdown-isdn (ifdown-ippp)
Using NETWO000.;1 for  /etc/sysconfig/network-scripts/network-functions-ipv6 (network-functions)
Writing:   Initial Padblock                        Start Block 0
Done with: Initial Padblock                        Block(s)    16
Writing:   Primary Volume Descriptor               Start Block 16
Done with: Primary Volume Descriptor               Block(s)    1
Writing:   End Volume Descriptor                   Start Block 17
Done with: End Volume Descriptor                   Block(s)    1
Writing:   Version block                           Start Block 18
Done with: Version block                           Block(s)    1
Writing:   Path table                              Start Block 19
Done with: Path table                              Block(s)    4
Writing:   Directory tree                          Start Block 23
Done with: Directory tree                          Block(s)    192
Writing:   Directory tree cleanup                  Start Block 215
Done with: Directory tree cleanup                  Block(s)    0
Writing:   Extension record                        Start Block 215
Done with: Extension record                        Block(s)    1
Writing:   The File(s)                             Start Block 216
 13.48% done, estimate finish Tue Mar 28 03:36:48 2017
 26.98% done, estimate finish Tue Mar 28 03:36:48 2017
 40.43% done, estimate finish Tue Mar 28 03:36:50 2017
 53.88% done, estimate finish Tue Mar 28 03:36:49 2017
 67.37% done, estimate finish Tue Mar 28 03:36:49 2017
 80.86% done, estimate finish Tue Mar 28 03:36:49 2017
 94.30% done, estimate finish Tue Mar 28 03:36:50 2017
Total translation table size: 0
Total rockridge attributes bytes: 114239
Total directory bytes: 387072
Path table size(bytes): 2122
Done with: The File(s)                             Block(s)    36753
Writing:   Ending Padblock                         Start Block 36969
Done with: Ending Padblock                         Block(s)    150
Max brk space used 108000
37119 extents written (72 MB)

4.可以看到 /boot/etc下的文件和目录现在没有散落在镜像文件的根目录下!

[root@CentOS /]# mount -o loop /tmp/test2.iso /media/dvd
[root@CentOS /]# ls -l /media/dvd
total 22
dr-xr-xr-x.  5 root root  2048 Mar 27 23:57 boot
dr-xr-xr-x. 61 root root 20480 Mar 28 03:34 etc

注意:
mkisofs -r -v -V 'Linux_file' -o /tmp/test2.iso -m /boot/lost+found/ -graft-point /boot=/boot /etc=/etc
命令中,选项-graft-point的参数,等号左边是镜像文件中的目录,等号右边是系统中的实际文件(目录)!


cdrecord

基本使用方法
  • 查询烧录机位置
[root@www ~]# cdrecord -scanbus dev=ATA
  • 抹除重复读写片
[root@www ~]# cdrecord -v dev=ATA:x,y,z blank=[fast|all]
  • 格式化DVD+RW
[root@www ~]# cdrecord -v dev=ATA:x,y,z -format
  • 进行光盘刻录
[root@www ~]# cdrecord -v dev=ATA:x,y,z [可用选项功能] file.iso
选项与参数:
  • -scanbus :用在扫瞄磁碟汇流排并找出可用的烧录机,后续的装置为 ATA 介面
  • -v :在 cdrecord 运行的过程中,显示过程而已。
  • dev=ATA:x,y,z :后续的 x, y, z 为你系统上烧录机所在的位置,非常重要!
  • blank=[fast|all]:blank 为抹除可重复写入的CD/DVD-RW,使用fast较快,all较完整
  • -format :仅针对 DVD+RW 这种格式的 DVD 而已;

[可用选项功能] 主要是写入 CD/DVD 时可使用的选项,常见的选项包括有:
- -data :指定后面的文件以数据格式写入,不是以 CD 音轨(-audio)方式写入!
- speed=X :指定烧录速度,例如CD可用 speed=40 为40倍数,DVD则可用 speed=4 之类
- -eject :指定烧录完毕后自动退出光盘
- fs=Ym :指定多少缓冲内存,可用在将映像档先缓存至缓冲内存。默认为 4m,
一般建议可添加到 8m ,不过,还是得视你的烧录机而定。

针对 DVD 的选项功能:
- driveropts=burnfree :打开 Buffer Underrun Free 模式的写入功能
- -sao :支持 DVD-RW 的格式

实际操作
刻录CD

1.检测刻录机的位置

[root@www ~]# cdrecord -scanbus dev=ATA
Cdrecord-Clone 2.01 (cpu-pc-linux-gnu) Copyright (C) 1995-2004 J?rg Schilling
....中间省略....
scsibus1:
        1,0,0   100) *
        1,1,0   101) 'ASUS    ' 'DRW-2014S1      ' '1.01' Removable CD-ROM
        1,2,0   102) *
        1,3,0   103) *
        1,4,0   104) *
        1,5,0   105) *
        1,6,0   106) *
        1,7,0   107) *

2.先抹除光盘的原始内容:(非可重复读写则可略过此步骤)

[root@www ~]# cdrecord -v dev=ATA:1,1,0 blank=fast
# 中间会跑出一堆信息告诉你抹除的进度,而且会有 10 秒钟的时间等待你的取消!
# 可以避免『手滑』的情况!^_^

3.开始烧录:

[root@www ~]# cdrecord -v dev=ATA:1,1,0 fs=8m -dummy -data \
>  /tmp/system.img
....中间省略....
Track 01:  168 of  176 MB written (fifo 100%) [buf 100%]  10.5x. <==显示百分比
# 上面会显示进度,还有 10.5x 代表目前的烧录速度!
cdrecord: fifo had 2919 puts and 2919 gets.
cdrecord: fifo was 0 times empty and 2776 times full, min fill was 97%.

4.烧录完毕后,测试挂载一下,检验内容:

[root@www ~]# mount -t iso9660 /dev/cdrom /mnt
[root@www ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdd              177M  177M     0 100% /mnt      <==瞧!确实是光盘内容!

[root@www ~]# ll /mnt
dr-xr-xr-x 105 root root 32768 Dec 17 11:54 etc
dr-xr-xr-x   5 root root  2048 Dec 17 11:54 home
dr-xr-xr-x   7 root root  4096 Dec 17 11:54 root

[root@www ~]# umount /mnt    <==不要忘了卸载
进行DVD-RW刻录

1.同样的,先来抹除一下原本的内容:

[root@www ~]# cdrecord -v dev=ATA:1,1,0 blank=fast

2.开始写入 DVD ,请注意,有些选项与 CD 并不相同了喔!

[root@www ~]# cdrecord -v dev=ATA:1,1,0 fs=8m -data -sao \
>  driveropts=burnfree /tmp/system.img

3.同样的,来给他测试测试!

[root@www ~]# mount /dev/cdrom /mnt
[root@www ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdd              177M  177M     0 100% /mnt
[root@www ~]# umount /mnt

9.6 其他常见的压缩与备份工具

9.6.1 dd

dd可以完整地备份整个分区或硬盘,因为dd是通过读取硬盘底层每个扇区(sector)的原始信息来进行备份的。

基本使用方法
[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命令!

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

[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

第一个扇区(Sector)内含有 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即可!简单地说,dd可以对整个分区,甚至整块硬盘进行备份,从一个 disk 到另一个disk。


9.6.2 cpio

cpio是非常常用的备份工具,不过cpio并不主动寻找文件,所以必须搭配类似find的命令来读入欲备份数据的文件名,才能进行备份。

基本使用方法

1.备份

[root@www ~]# cpio -ovcB  > [file|device]

2.还原

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

3.查看

[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 方式储存

实际操作
  • 备份:
    找出 /boot 底下的所有文件,然后将他备份到 /tmp/boot.cpio 去!
[root@www ~]# find /boot -print
/boot
/boot/message
/boot/initrd-2.6.18-128.el5.img
....以下省略....
# 透过这个 find 我们可以找到 /boot 底下应该要存在的档名!包括文件与目录

[root@www ~]# find /boot | cpio -ocvB > /tmp/boot.cpio
[root@www ~]# ll -h /tmp/boot.cpio
-rw-r--r-- 1 root root 16M Dec 17 23:30 /tmp/boot.cpio
  • 还原
    将刚刚的文件给他在 /root/ 目录下解开
[root@www ~]# cpio -idvc < /tmp/boot.cpio
[root@www ~]# ll /root/boot
# 你可以自行比较一下 /root/boot 与 /boot 的内容是否一模一样!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值