Linux_centos7_文件与文件系统的压缩/打包与备份_(5)_(bird_bro)

文件与文件系统压缩、打包与备份

Linux_centos7_文件与文件系统的压缩/打包与备份_(5)_(bird_bro)_第1张图片

文件压缩


#按照文件大小检视文件,默认文件由大到小排序,参数 -r 可指定从小到大排序
[kingarthur@localhost ~]$ ls -ldSr /etc/*
-rw-r--r--.  1 root root         0 Jun 10  2014 /etc/wvdial.conf
-rw-r--r--.  1 root root         0 Oct 31  2018 /etc/subuid
-rw-r--r--.  1 root root         0 Oct 31  2018 /etc/subgid
-rw-r--r--.  1 root root         0 Jun  7  2013 /etc/motd
-rw-r--r--.  1 root root         0 Jun  7  2013 /etc/exports
-rw-r--r--.  1 root root         0 Oct 31  2018 /etc/environment
-rw-------.  1 root root         0 Mar 18 07:52 /etc/crypttab
-rw-------.  1 root root         0 Apr 11  2018 /etc/cron.deny
-rw-r--r--.  1 root root         1 Mar 19 16:30 /etc/resolv.conf.save
-rw-r--r--.  1 root root         1 Oct 31  2018 /etc/at.deny
drwxr-xr-x.  2 root root         6 Apr 11  2018 /etc/xinetd.d
drwxr-xr-x.  2 root root         6 Oct 31  2018 /etc/tmpfiles.d
...
-rw-r--r--.  1 root root     26832 Oct 31  2018 /etc/dnsmasq.conf
-rw-r--r--.  1 root root     51787 May 15  2013 /etc/mime.types
-rw-r--r--.  1 root root    121699 Mar 18 08:22 /etc/ld.so.cache
-rw-r--r--.  1 root root    670293 Jun  7  2013 /etc/services
[kingarthur@localhost ~]$ ls -ldS /etc/*
-rw-r--r--.  1 root root    670293 Jun  7  2013 /etc/services
-rw-r--r--.  1 root root    121699 Mar 18 08:22 /etc/ld.so.cache
-rw-r--r--.  1 root root     12589 Jul 23  2018 /etc/slp.conf
-rw-r--r--.  1 root root     12288 Mar 18 14:27 /etc/aliases.db
drwxr-xr-x.  2 root root     12288 Mar 18 08:07 /etc/brltty
...
-rw-r--r--.  1 root root         0 Jun  7  2013 /etc/motd
-rw-r--r--.  1 root root         0 Oct 31  2018 /etc/subgid
-rw-r--r--.  1 root root         0 Oct 31  2018 /etc/subuid
-rw-r--r--.  1 root root         0 Jun 10  2014 /etc/wvdial.conf
[kingarthur@localhost ~]$ cd /tmp/
[kingarthur@localhost tmp]$ cp /etc/services .
[kingarthur@localhost tmp]$ ls -l ./services 
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 ./services
#参数 -v 显示文件压缩比
[kingarthur@localhost tmp]$ gzip -v services 
services:	 79.7% -- replaced with services.gz
#查看压缩前后文件属性
[kingarthur@localhost tmp]$ ll /etc/services /tmp/services*
-rw-r--r--. 1 root       root       670293 Jun  7  2013 /etc/services
-rw-r--r--. 1 kingarthur kingarthur 136088 Apr 11 19:58 /tmp/services.gz
#源文件为文本文件,指令 zcat 读取压缩文件内容
[kingarthur@localhost tmp]$ zcat services.gz 
#/etc/services:
$Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#Network services, Internet style
#IANA services version: last updated 2013-04-10
#
#Note that it is presently the policy of IANA to assign a single well-known
#port number for both TCP and UDP; hence, most entries here have two entries
#even if the protocol doesn't support UDP operations.
#Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
#are included, only the more common ones.
#
#The latest IANA port assignments can be gotten from
#http://www.iana.org/assignments/port-numbers
#The Well Known Ports are those from 0 through 1023.
#The Registered Ports are those from 1024 through 49151
#The Dynamic and/or Private Ports are those from 49152 through 65535
#
#Each line describes one service, and is of the form:
#
#service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
echo            7/udp
...

#文件解压缩 参数 -d 
[kingarthur@localhost tmp]$ gzip -d services.gz 
[kingarthur@localhost tmp]$ ls -l service*
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 services
#参数 -n 指定压缩等级,最高等级为9,参数 -c 将压缩文件内容输出到屏幕
#符号 > 将压缩后文件存入新文件,源文件被保留
[kingarthur@localhost tmp]$ gzip -9 -c services > services.gz
[kingarthur@localhost tmp]$ ls -l service*
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 services
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 services.gz
#输出压缩文件中包含 'http' 关键字的行及其行号
[kingarthur@localhost tmp]$ zgrep -n 'http' services.gz 
14:#       http://www.iana.org/assignments/port-numbers
89:http            80/tcp          www www-http    # WorldWideWeb HTTP
90:http            80/udp          www www-http    # HyperText Transfer Protocol
91:http            80/sctp                         # HyperText Transfer Protocol
197:https           443/tcp                         # http protocol over TLS/SSL
198:https           443/udp                         # http protocol over TLS/SSL
199:https           443/sctp                        # http protocol over TLS/SSL
210:gss-http        488/tcp
211:gss-http        488/udp
...
10877:tcc-http        24680/tcp               # TCC User HTTP Service
10878:tcc-http        24680/udp               # TCC User HTTP Service
10938:kopek-httphead  27504/tcp               # Kopek HTTP Head Port
10939:kopek-httphead  27504/udp               # Kopek HTTP Head Port

#指令 bzip2 压缩文件,参数 -v 显示压缩比率
[kingarthur@localhost tmp]$ bzip2 -v services
  services:  5.409:1,  1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
[kingarthur@localhost tmp]$ ls -l services*
-rw-r--r--. 1 kingarthur kingarthur 123932 Apr 11 19:58 services.bz2
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 services.gz
#指令 bzcat 查看压缩文件内容
[kingarthur@localhost tmp]$ bzcat services.bz2 
#/etc/services:
#$Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#Network services, Internet style
#IANA services version: last updated 2013-04-10
#
#Note that it is presently the policy of IANA to assign a single well-known
#port number for both TCP and UDP; hence, most entries here have two entries
#even if the protocol doesn't support UDP operations.
#Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
#are included, only the more common ones.
#
#The latest IANA port assignments can be gotten from
#http://www.iana.org/assignments/port-numbers
#The Well Known Ports are those from 0 through 1023.
#The Registered Ports are those from 1024 through 49151
#The Dynamic and/or Private Ports are those from 49152 through 65535
#
#Each line describes one service, and is of the form:
#
#service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
...
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
#压缩文件解压
[kingarthur@localhost tmp]$ bzip2 -d services.bz2 
#保留源文件,将压缩后的文件输出/写入新文件,最大压缩等级压缩文件
[kingarthur@localhost tmp]$ bzip2 -9 -c services > services.bz2
[kingarthur@localhost tmp]$ ls -l ./servi*
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 ./services
-rw-rw-r--. 1 kingarthur kingarthur 123932 Apr 12 11:16 ./services.bz2
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 ./services.gz
#文件压缩,显示压缩比率
[kingarthur@localhost tmp]$ xz -v services
services (1/1)
  100 %        97.3 KiB / 654.6 KiB = 0.149                                    
[kingarthur@localhost tmp]$ ls -l ./serv*
-rw-rw-r--. 1 kingarthur kingarthur 123932 Apr 12 11:16 ./services.bz2
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 ./services.gz
-rw-r--r--. 1 kingarthur kingarthur  99608 Apr 11 19:58 ./services.xz
#压缩文件文件名查看
[kingarthur@localhost tmp]$ xzcat services.xz 
#/etc/services:
#$Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#Network services, Internet style
#IANA services version: last updated 2013-04-10
#
#Note that it is presently the policy of IANA to assign a single well-known
#port number for both TCP and UDP; hence, most entries here have two entries
#even if the protocol doesn't support UDP operations.
#Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
#are included, only the more common ones.
#
#The latest IANA port assignments can be gotten from
#http://www.iana.org/assignments/port-numbers
#The Well Known Ports are those from 0 through 1023.
#The Registered Ports are those from 1024 through 49151
#The Dynamic and/or Private Ports are those from 49152 through 65535
#
#Each line describes one service, and is of the form:
#
#service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
tcpmux          1/udp                           # TCP port service multiplexer
rje             5/tcp                           # Remote Job Entry
rje             5/udp                           # Remote Job Entry
echo            7/tcp
echo            7/udp
...
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
#参数 -n 测试,失败,参数不可用
[kingarthur@localhost tmp]$ xzcat -n services.xz 
xzcat: invalid option -- 'n'
xzcat: Try `xzcat --help' for more information.
#指令 cat 直接查看文档,报错,乱码
[kingarthur@localhost tmp]$ cat services.xz
疴F!t/夦`¨Fr»軩sݳd+ ^ѝ*dSdF¼¤fꆘ*::4T50ª贎-lǐFPuԤ
¢cđrt׮#ڜ)@±nܦ_#6                                    µWZꃀ½¦$¾¢2ع	i»!¾ݔ?Ȱ௃8߭.[ϠS 
                 L羋6Gÿ¸ºH¾'6ë¤~؛¼$QxկtB¶¥	΃΂Գ 
...
                                    q¯(
                                       ŧթ>	´¥³װ?S|®P܋N´'ÿ¸D~¥Ր¦vk9C¼aTg͋q9و̼<®q
                                                                                     ¤ގ>      ¬¨m	¢ºƘ4l'C髂uР¹4ӽ?\>R+wF`tfЋ@*®\^ˮ3_b<Ɍ{ԌQ죾hmȽ´.¸?Ć¼Wt߂µ=¹K<à_M뭮؁媔H 
                         чKZ0	ɥ³:|WwBz®㪵Ik±(s"þcQ臢ս<h[B#l4ȓ<￘�¥T岩O_Q't½E­{b�eb!£ڻ4-­£ܡA[}!J±ç㘚Xshell[kingarthur@localhost tmp]$ XshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshellXshell^C
#压缩文件解压
[kingarthur@localhost tmp]$ xz -d services.xz 
[kingarthur@localhost tmp]$ ls -l ./services*
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 ./services
-rw-rw-r--. 1 kingarthur kingarthur 123932 Apr 12 11:16 ./services.bz2
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 ./services.gz
#参数 -k 指定保留原本文件不删除
[kingarthur@localhost tmp]$ xz -k services
[kingarthur@localhost tmp]$ ls -l ./services*
-rw-r--r--. 1 kingarthur kingarthur 670293 Apr 11 19:58 ./services
-rw-rw-r--. 1 kingarthur kingarthur 123932 Apr 12 11:16 ./services.bz2
-rw-rw-r--. 1 kingarthur kingarthur 135489 Apr 11 20:16 ./services.gz
-rw-r--r--. 1 kingarthur kingarthur  99608 Apr 11 19:58 ./services.xz
[kingarthur@localhost tmp]$ su
Password: 
#文件打包
#参数 -z 指定 gzip2 进行压缩/解压缩
#参数 -p 指定备份数据的原本权限与属性
#参数 -c 建立打包文件
#参数 -v 在解压缩过程中,将正在处理的文件名显示出来
#参数 -f 后接将要处理的文件 
[root@localhost tmp]# time tar -zpcv -f /root/etc.tar.gz /etc
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
...
/etc/resolv.conf.save
/etc/aliases.db
/etc/sudoers

real	0m4.378s
user	0m1.687s
sys	0m2.837s
#参数 -j 指定 bzip2 进行解压缩
[root@localhost tmp]# time tar -jpcv -f /root/etc.tar.bz2 /etc
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
...
/etc/resolv.conf.save
/etc/aliases.db
/etc/sudoers

real	0m4.328s
user	0m3.203s
sys	0m1.400s
#参数 -J 指定 xz 进行解压缩
[root@localhost tmp]# time tar -Jpcv -f /root/etc.tar.xz /etc
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
...
/etc/.updated
/etc/resolv.conf.save
/etc/aliases.db
/etc/sudoers

real	0m22.755s
user	0m18.871s
sys	0m4.640s

[root@localhost tmp]# ll /root/etc*
-rw-r--r--. 1 root root 10792900 Apr 12 12:21 /root/etc.tar.bz2
-rw-r--r--. 1 root root 12453062 Apr 12 12:17 /root/etc.tar.gz
-rw-r--r--. 1 root root  9043424 Apr 12 12:24 /root/etc.tar.xz
#目录占用空间大小
[root@localhost tmp]# du -sm /etc
44	/etc
[root@localhost tmp]# ls
bashrc        ssh-JJqlAjkG1sCe                                                              testtouch
mvtest2       systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-bolt.service-n9rGJl          tracker-extract-files.1000
services      systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-colord.service-HPaExE        vmware-root_6350-1003010269
services.bz2  systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-cups.service-frU5TO          vmware-root_6366-968933069
services.gz   systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-fwupd.service-3RuQkY         vmware-root_6401-1992238250
services.xz   systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-rtkit-daemon.service-R0ON8X  vmware-root_6440-994228546
#查看打包文件内文件名,参数 -v 文件权限与属性均被列出
[root@localhost tmp]# tar  -jtv -f /root/etc.tar.bz2
drwxr-xr-x root/root         0 2020-03-22 18:06 etc/
-rw-r--r-- root/root       465 2020-03-18 07:52 etc/fstab
...
-rw-r--r-- root/root        37 2020-03-18 08:29 etc/vconsole.conf
-rw-r--r-- root/root        19 2020-03-18 08:29 etc/locale.conf
-rw-r--r-- root/root        22 2020-03-18 08:29 etc/hostname
-rw-r--r-- root/root       163 2020-03-18 07:55 etc/.updated
-rw-r--r-- root/root         1 2020-03-19 16:30 etc/resolv.conf.save
-rw-r--r-- root/root     12288 2020-03-18 14:27 etc/aliases.db
-r--r----- root/root      4353 2020-03-22 15:42 etc/sudoers
#查看打包文件内文件名
[root@localhost tmp]# tar  -jt -f /root/etc.tar.bz2
etc/
etc/fstab
etc/crypttab
etc/mtab
...
etc/locale.conf
etc/hostname
etc/.updated
etc/resolv.conf.save
etc/aliases.db
etc/sudoers
#参数 -j 指定 bzip2
#参数 -p 指定保留权限与属性
#参数 -P 保留绝对路径,允许备份数据中含有根目录
[root@localhost tmp]# tar -jpPcv -f /root/etc.and.root.tar.bz2 /etc
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
...
/etc/hostname
/etc/.updated
/etc/resolv.conf.save
/etc/aliases.db
/etc/sudoers
#查看打包文件内文件名称(不显示权限与属性)
[root@localhost tmp]# tar -jtf /root/etc.and.root.tar.bz2 
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
...
/etc/hostname
/etc/.updated
/etc/resolv.conf.save
/etc/aliases.db
/etc/sudoers

[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls 
bashrc        ssh-JJqlAjkG1sCe                                                              testtouch
mvtest2       systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-bolt.service-n9rGJl          tracker-extract-files.1000
services      systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-colord.service-HPaExE        vmware-root_6350-1003010269
services.bz2  systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-cups.service-frU5TO          vmware-root_6366-968933069
services.gz   systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-fwupd.service-3RuQkY         vmware-root_6401-1992238250
services.xz   systemd-private-f936b3d8a5af4098a0d35ddb06644d1f-rtkit-daemon.service-R0ON8X  vmware-root_6440-994228546

#参数 -x  压缩文件解压缩/解打包
[root@localhost tmp]# tar -jxv -f /root/etc.tar.bz2 
etc/
etc/fstab
etc/crypttab
etc/mtab
...
etc/hostname
etc/.updated
etc/resolv.conf.save
etc/aliases.db
etc/sudoers

[root@localhost tmp]# ll
total 1036
-rw-r--r--.   1 root       root          176 Jun 15  2014 bashrc
drwxr-xr-x. 175 root       root        12288 Mar 22 18:06 etc
drwxr-xr-x.   2 root       root           50 Apr  1 20:32 mvtest2
-rw-r--r--.   1 kingarthur kingarthur 670293 Apr 11 19:58 services
-rw-rw-r--.   1 kingarthur kingarthur 123932 Apr 12 11:16 services.bz2
-rw-rw-r--.   1 kingarthur kingarthur 135489 Apr 11 20:16 services.gz
-rw-r--r--.   1 kingarthur kingarthur  99608 Apr 11 19:58 services.xz
...
drwx------.   2 kingarthur kingarthur      6 Mar 22 16:35 tracker-extract-files.1000
drwx------.   2 root       root            6 Mar 30 00:13 vmware-root_6350-1003010269
drwx------.   2 root       root            6 Apr 10 03:45 vmware-root_6366-968933069
drwx------.   2 root       root            6 Apr  8 03:27 vmware-root_6401-1992238250
drwx------.   2 root       root            6 Mar 30 16:15 vmware-root_6440-994228546
[root@localhost tmp]# ll etc
total 1620
drwxr-xr-x.  3 root root       101 Mar 18 08:05 abrt
-rw-r--r--.  1 root root        16 Mar 18 08:29 adjtime
drwxr-xr-x.  2 root root       116 Mar 18 08:18 akonadi
-rw-r--r--.  1 root root      1518 Jun  7  2013 aliases
-rw-r--r--.  1 root root     12288 Mar 18 14:27 aliases.db
drwxr-xr-x.  3 root root        65 Mar 18 08:07 alsa
...
drwxr-xr-x.  6 root root       100 Mar 18 08:02 yum
-rw-r--r--.  1 root root       970 Nov  5  2018 yum.conf
drwxr-xr-x.  2 root root       187 Nov  5  2018 yum.repos.d
#参数 -C directory , 指定文件解压至 directory
[root@localhost tmp]# tar -jxv -f /root/etc.tar.bz2 -C ~/
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/resolv.conf
...
etc/.updated
etc/resolv.conf.save
etc/aliases.db
etc/sudoers

[root@localhost tmp]# ls ~
anaconda-ks.cfg  etc  etc.and.root.tar.bz2  etc.tar.bz2  etc.tar.gz  etc.tar.xz  initial-setup-ks.cfg
[root@localhost tmp]# ls /etc/
abrt                       dhcp                        hosts                     man_db.conf          popt.d            slp.spi
adjtime                    DIR_COLORS                  hosts.allow               maven                portreserve       smartmontools
akonadi                    DIR_COLORS.256color         hosts.deny                mcelog               postfix           snmp
aliases                    DIR_COLORS.lightbgcolor     hp                        mime.types           ppp               softhsm2.conf
aliases.db                 dirsrv                      httpd                     mke2fs.conf          prelink.conf.d    sos.conf
auto.master.d              ethertypes                  iscsi                     named.conf           rc0.d             sudoers
auto.misc                  exports                     issue                     named.iscdlv.key     rc1.d             sudoers.d
auto.net                   exports.d                   issue.d                   named.rfc1912.zones  rc2.d             sudo-ldap.conf
auto.smb                   extlinux.conf               issue.net                 named.root.key       rc3.d             sysconfig
...
cupshelpers                gshadow-                    machine-id                pkcs11               shadow-           xml
custodia                   gss                         magic                     pki                  shells            yum
dbus-1                     gssproxy                    mail                      plymouth             skel              yum.conf
dconf                      ha.d                        mailcap                   pm                   slp.conf          yum.repos.d
default                    host.conf                   mail.rc                   pnm2ppa.conf         slp.reg
depmod.d                   hostname                    makedumpfile.conf.sample  polkit-1             slp.reg.d
[root@localhost tmp]# rm -rf /root/etc /tmp/etc
[root@localhost tmp]# ls -d /tmp/etc
ls: cannot access /tmp/etc: No such file or directory
[root@localhost tmp]# ls -d ~/etc
ls: cannot access /root/etc: No such file or directory
[root@localhost tmp]# ls -d ~/etc.
etc.and.root.tar.bz2  etc.tar.bz2           etc.tar.gz            etc.tar.xz            
[root@localhost tmp]# ls -d ~/etc.
etc.and.root.tar.bz2  etc.tar.bz2           etc.tar.gz            etc.tar.xz            
[root@localhost tmp]# ls -d /tmp/etc
ls: cannot access /tmp/etc: No such file or directory
#仅解开单一文件,首先查找文件是否存在
[root@localhost tmp]# tar -jtv -f /root/etc.tar.bz2 | grep 'shadow'
-rw-r--r-- root/root       214 2018-10-31 07:23 etc/pam.d/sssd-shadowutils
---------- root/root      1038 2020-03-22 18:06 etc/gshadow
---------- root/root      1703 2020-03-18 08:29 etc/shadow
---------- root/root      1023 2020-03-18 08:29 etc/gshadow-
---------- root/root      1708 2020-03-18 08:29 etc/shadow-
#仅解开单一文件,文件存在,将该文件解压
[root@localhost tmp]# tar -jxv -f /root/etc.tar.bz2  etc/shadow
etc/shadow
[root@localhost tmp]# ll etc/
total 4
----------. 1 root root 1703 Mar 18 08:29 shadow
#打包 /etc/ /root 目录,排除 /root/etc* 开头的文件,并指定压缩文件
#放置成为/root/system.tar.bz2,并且不可打包自己
#参数 --exclude 指定排除项
[root@localhost tmp]# tar -jcv -f /root/system.tar.bz2 --exclude=/root/etc*  --exclude=/root/system.tar.bz2 /etc /root 
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
/etc/fonts/
...
/root/.config/abrt/
/root/initial-setup-ks.cfg
/root/.local/
/root/.local/share/
/root/.bash_history
/root/.lesshst

[root@localhost tmp]# ls ~/system.tar.bz2 
/root/system.tar.bz2
[root@localhost tmp]# ll  ~/system.tar.bz2 
-rw-r--r--. 1 root root 10797563 Apr 12 17:50 /root/system.tar.bz2
[root@localhost tmp]# pwd
/tmp

#将 /etc 整个目录一边打包一边在 /tmp 解开
[root@localhost tmp]# tar -cvf - /etc | tar -xvf -
tar: Removing leading `/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
/etc/fonts/
...
etc/locale.conf
etc/hostname
etc/.updated
etc/resolv.conf.save
etc/aliases.db
etc/sudoers
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ll -d ./etc
drwxr-xr-x. 175 root root 12288 Mar 22 18:06 ./etc
[root@localhost tmp]# cd ~

#压缩与备份工具
#将 /etc.passwd 备份到 /tmp/passwd.back 当中
#指令 dd 可以读取磁盘装置内容,然后将整个装置备份成一个文件
[root@localhost ~]# dd if=/etc/passwd of=/tmp/passwd.back
6+1 records in
6+1 records out
3529 bytes (3.5 kB) copied, 0.000407216 s, 8.7 MB/s
[root@localhost ~]# ll /etc/passwd /tmp/passwd.back 
-rw-r--r--. 1 root root 3529 Mar 18 08:29 /etc/passwd
-rw-r--r--. 1 root root 3529 Apr 12 18:03 /tmp/passwd.back

你可能感兴趣的:(Linux_bird_bro)