5 ll参数 mark_5.打包与压缩

5.打包与压缩

本章同步视频:https://edu.51cto.com/sd/e4874

5.1 Linux 系统常见的压缩指令

5.1.1常见后缀

n*.Z         compress 程序压缩的档案;

n*.zip       zip程序压缩的档案;

n*.gzgzip程序压缩的档案;

n*.bz2       bzip2 程序压缩的档案;

n*.xzxz程序压缩的档案;

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

n*.tar.gz    tar 程序打包的档案,其中并且经过gzip的压缩

n*.tar.bz2   tar 程序打包的档案,其中并且经过 bzip2 的压缩

n*.tar.xz    tar 程序打包的档案,其中并且经过xz的压缩

5.1.2  gzip, gunzip, zcat - compress or expand files

1.语法

[dmtsai@study ~]$ gzip [-cdtv#] 檔名

[dmtsai@study ~]$ zcat檔名.gz

选项与参数:

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

-d  :解压缩的参数;

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

-v  :可以显示出原档案/压缩文件案的压缩比等信息;

-#  :# 为数字的意思,代表压缩等级,-1 最快,但是压缩比最差、-9 最慢,但是压缩比最好!预设是 -6

2.用法

(1)测试一般压缩

[root@localhost dir]# ll

total 1020

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

[root@localhost dir]# gzip etc.conf      #不带任何选项的压缩

[root@localhost dir]# ll

total 244

-rw-r--r--. 1 root root 247318 Mar 30 20:36 etc.conf.gz

(2)解压缩

[root@localhost dir]# gzip -d etc.conf.gz     #解压缩

[root@localhost dir]# ll

total 1020

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

(3)显示压缩比

[root@localhost dir]# gzip -v etc.conf    #显示压缩比的压缩

etc.conf:  76.3% -- replaced with etc.conf.gz

(4)保留源文件

[root@localhost dir]# gzip -c etc.conf >etc.conf.c   #保留源文件的压缩

[root@localhost dir]# ll

total 1264

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

-rw-r--r--. 1 root root  247318 Mar 30 20:47 etc.conf.c

(5)设定不同的压缩比(压缩比为1-9)

[root@localhost dir]# gzip -9 -c etc.conf>etc.conf.9    #9级压缩

[root@localhost dir]# gzip -1 -c etc.conf>etc.conf.1    #1级压缩

[root@localhost dir]# ll    

total 1784

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

-rw-r--r--. 1 root root  282311 Mar 30 20:48 etc.conf.1

-rw-r--r--. 1 root root  246914 Mar 30 20:48 etc.conf.9

-rw-r--r--. 1 root root  247318 Mar 30 20:47 etc.conf.c

(6)递归压缩

[root@localhost dir]# mkdir dir     #创建一个子目录

[root@localhost dir]# ll

total 1020

drwxr-xr-x. 2 root root       6 Mar 30 20:50 dir

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

[root@localhost dir]# cp etc.conf dir/    #将etc.conf复制到子目录

[root@localhost dir]# cd ..     #退回到父目录

[root@localhost tmp]# gzip -c dir/* > dir.1     #压缩外层dir目录

gzip: dir/dir is a directory -- ignored     #显示忽略子目录内容

[root@localhost tmp]# gzip -c -r dir/>dir.2   #递归压缩外层dir目录

[root@localhost tmp]# ll

drwxrwxrwx. 3 root root      31 Mar 30 20:50 dir

-rw-r--r--. 1 root root  247318 Mar 30 20:52 dir.1

-rw-r--r--. 1 root root  494636 Mar 30 20:53 dir.2

#注意对比dir.1和dir.2的大小。

(7)查看压缩内容

[root@localhost dir]# ll

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

[root@localhost dir]# gzip passwd

[root@localhost dir]# ll

-rw-r--r--. 1 root root 800 Mar 30 21:05 passwd.gz

[root@localhost dir]# zcat passwd.gz

[root@localhost dir]# zmore passwd.gz

[root@localhost dir]# zless passwd.gz

[root@localhost dir]# zgrep -n "calf" passwd.gz

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

5.1.3 bzip2, bzcat/bzmore/bzless/bzgrep

1.语法

[dmtsai@study ~]$ bzip2 [-cdkzv#] 檔名

[dmtsai@study ~]$ bzcat檔名.bz2

选项与参数:

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

-d  :解压缩的参数

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

-z  :压缩的参数 (默认值,可以不加)

-v  :可以显示出原档案/压缩文件案的压缩比等信息;

-#  :与gzip同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!

2.用法

(1)基本用法

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

[root@localhost dir]# bzip2 passwd

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 837 Mar 30 21:05 passwd.bz2

(2)解压缩

[root@localhost dir]# bzip2 -d passwd.bz2

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

(3)显示压缩参数

[root@localhost dir]# bzip2 -v passwd

  passwd:   2.393:1,  3.343 bits/byte, 58.21% saved, 2003 in, 837 out.

(4)保留源文件

[root@localhost dir]# bzip2 -k passwd

[root@localhost dir]# ll

total 8

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

-rw-r--r--. 1 root root  837 Mar 30 21:05 passwd.bz2

(5)指定压缩比

[root@localhost dir]# bzip2 -9 passwd

[root@localhost dir]# bzip2 -1 passwd

#注意:连续执行这两条命令会报错,bzip2: Output file passwd.bz2 already exists. 提示文件已经存在,需要解压后再做第二步。或者执行下面两条命令:

[root@localhost dir]# bzip2 -9 -c passwd>passwd.9.bz2

[root@localhost dir]# bzip2 -1 -c passwd>passwd.1.bz2

(6)查看压缩内容

[root@localhost dir]# bzcat passwd.bz2

[root@localhost dir]# bzmore passwd.bz2

[root@localhost dir]# bzless passwd.bz2

[root@localhost dir]# bzgrep -n "calf" passwd.bz2

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

5.1.4 xz,    xzcat,   - Compress or decompress   .xz  files

1.语法

[dmtsai@study ~]$ xz [-dtlkc#] 檔名

[dmtsai@study ~]$ xcat檔名.xz

选项与参数:

-d  :就是解压缩啊!

-t  :测试压缩文件的完整性,看有没有错误

-l  :列出压缩文件的相关信息

-k  :保留原本的档案不删除~

-c  :同样的,就是将数据由屏幕上输出的意思!

-#  :同样的,也有较佳的压缩比的意思!

2.用法

(1)一般用法

[root@localhost dir]# xz passwd

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 864 Mar 30 21:05 passwd.xz

#压缩后源文件会消失

(2)解压缩

[root@localhost dir]# xz -d passwd.xz

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

(3)保留源文件

[root@localhost dir]# xz -k passwd

[root@localhost dir]# ll

total 8

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

-rw-r--r--. 1 root root  864 Mar 30 21:05 passwd.xz

(4)显示压缩参数

[root@localhost dir]# xz -l passwd.xz

Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename

    1       1        864 B      2,003 B  0.431  CRC64   passwd.xz

(5)指定压缩比

[root@localhost dir]# xz -9 passwd

[root@localhost dir]# xz -1 passwd

#或者执行下面两条命令

[root@localhost dir]# xz -9 -c passwd>passwd.9.xz

[root@localhost dir]# xz -1 -c passwd>passwd.1.xz

(6)查看压缩内容

[root@localhost dir]# xzcat passwd.xz

[root@localhost dir]# xzmore passwd.xz

[root@localhost dir]# xzless passwd.xz

[root@localhost dir]# xzgrep -n "calf" passwd.xz

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

5.2 打包指令: tar

5.2.1 tar语法

[dmtsai@study ~]$ tar [-z|-j|-J] [cv] [-f 待建立的新檔名] filename...<==打包与压缩< span="">

[dmtsai@study ~]$ tar [-z|-j|-J] [tv] [-f 既有的 tar檔名]            <==察看檔名< span="">

[dmtsai@study ~]$ tar [-z|-j|-J] [xv] [-f 既有的 tar檔名] [-C 目录]  <==解压缩< span="">

选项与参数:

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

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

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

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

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

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

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

特别留意, -z, -j, -J 不可以同时出现在一串指令列中

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

-f filename:-f 后面要立刻接要被处理的档名!建议 -f 单独写一个选项啰!(比较不会忘记)

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

5.2.2 tar打包与压缩

1.直接打包(不压缩)

[root@localhost tmp]# tar -cvf dir.tar dir/

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

#如果不带v,则不会有上面4行的输出。

2.用gzip压缩

[root@localhost tmp]# tar -zcv -f dir.tar.gz dir/

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

dir/dir.tar.gz

3.用bzip2压缩

[root@localhost tmp]# tar -jvc -f dir.tar.bz dir/

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

dir/dir.tar.gz

4.用xz压缩

[root@localhost tmp]# tar -Jcv -f dir.tar.xz dir/

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

dir/dir.tar.gz

5.三种压缩对比

[root@localhost tmp]# ll

total 36

drwxrwxrwx. 2 root root    70 Mar 30 22:05 dir

-rw-r--r--. 1 root root 10240 Mar 30 22:01 dir.tar

-rw-r--r--. 1 root root  2541 Mar 30 22:07 dir.tar.bz

-rw-r--r--. 1 root root  2093 Mar 30 22:05 dir.tar.gz

-rw-r--r--. 1 root root  1956 Mar 30 22:08 dir.tar.xz

#tar包比原文件大属于正常情况

5.2.3 查看打包内容

1.查看gzip压缩的tar包

[root@localhost tmp]# tar -ztv -f dir.tar.gz

drwxrwxrwx root/root         0 2020-03-30 22:05 dir/

-rw-r--r-- root/root      2003 2020-03-30 21:05 dir/passwd

-rw-r--r-- root/root       864 2020-03-30 21:05 dir/passwd.xz

-rw-r--r-- root/root       864 2020-03-30 21:46 dir/passwd.9.xz

-rw-r--r-- root/root        45 2020-03-30 22:05 dir/dir.tar.gz

2.查看bzip2压缩的tar包

[root@localhost tmp]# tar -jtv -f dir.tar.bz

drwxrwxrwx root/root         0 2020-03-30 22:05 dir/

-rw-r--r-- root/root      2003 2020-03-30 21:05 dir/passwd

-rw-r--r-- root/root       864 2020-03-30 21:05 dir/passwd.xz

-rw-r--r-- root/root       864 2020-03-30 21:46 dir/passwd.9.xz

-rw-r--r-- root/root        45 2020-03-30 22:05 dir/dir.tar.gz

3.查看xz压缩的tar包

[root@localhost tmp]# tar -Jtv -f dir.tar.xz

drwxrwxrwx root/root         0 2020-03-30 22:05 dir/

-rw-r--r-- root/root      2003 2020-03-30 21:05 dir/passwd

-rw-r--r-- root/root       864 2020-03-30 21:05 dir/passwd.xz

-rw-r--r-- root/root       864 2020-03-30 21:46 dir/passwd.9.xz

-rw-r--r-- root/root        45 2020-03-30 22:05 dir/dir.tar.gz

4.查看未压缩的tar包

[root@localhost tmp]# tar -tv -f dir.tar

drwxrwxrwx root/root         0 2020-03-30 21:46 dir/

-rw-r--r-- root/root      2003 2020-03-30 21:05 dir/passwd

-rw-r--r-- root/root       864 2020-03-30 21:05 dir/passwd.xz

-rw-r--r-- root/root       864 2020-03-30 21:46 dir/passwd.9.xz

5.2.4 解压

1.解压tar包到当前目录

[root@localhost tmp]# tar -xv -f dir.tar

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

2.解压tar包到指定目录

[root@localhost tmp]# tar -xv -f dir.tar -C /root

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

3.解压gzip压缩的tar包

[root@localhost tmp]# tar -zvx -f dir.tar.gz   #解压到当前目录

[root@localhost tmp]# tar -zvx -f dir.tar.gz -C /root  #解压到/root

4.解压bzip2压缩的tar包

[root@localhost tmp]# tar -jvx -f dir.tar.bz   #解压到当前目录

[root@localhost tmp]# tar -jvx -f dir.tar.bz -C /root  #解压到/root

5.解压xz压缩的tar包

[root@localhost tmp]# tar -Jvx -f dir.tar.xz    #解压到当前目录

[root@localhost tmp]# tar -Jvx -f dir.tar.xz -C /root   #解压到/root

5.2.5 特殊用法

1.解压部分文件

[root@localhost tmp]# tar -t -f dir.tar   #查看压缩文件

dir/

dir/passwd

dir/passwd.xz

dir/passwd.9.xz

[root@localhost tmp]# ll dir    #查看dir源目录里的内容(用于验证实验)

total 12

-rw-r--r--. 1 root root  45 Mar 30 22:05 dir.tar.gz

-rw-r--r--. 1 root root 864 Mar 30 21:46 passwd.9.xz

-rw-r--r--. 1 root root 864 Mar 30 21:05 passwd.xz

[root@localhost tmp]# tar -xv -f dir.tar dir/passwd    #解压指定文件

dir/passwd

[root@localhost tmp]# ll dir     #再次查看dir源目录里的内容

total 16

-rw-r--r--. 1 root root   45 Mar 30 22:05 dir.tar.gz

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

-rw-r--r--. 1 root root  864 Mar 30 21:46 passwd.9.xz

-rw-r--r--. 1 root root  864 Mar 30 21:05 passwd.xz

#注:如果tar包是被压缩的,需要带对应的选项z/j/J。

2.打包某目录,但不含该目录下的某些档案

[root@localhost tmp]# ll dir

total 12

-rw-r--r--. 1 root root  45 Mar 30 22:05 dir.tar.gz

-rw-r--r--. 1 root root 864 Mar 30 21:46 passwd.9.xz

-rw-r--r--. 1 root root 864 Mar 30 21:05 passwd.xz

[root@localhost tmp]# tar -Jcv -f dir.tar.xz --exclude=dir/passwd.xz dir/

dir/

dir/dir.tar.gz

dir/passwd.9.xz

[root@localhost tmp]# tar -Jtv -f dir.tar.xz

drwxrwxrwx root/root         0 2020-03-30 22:34 dir/

-rw-r--r-- root/root        45 2020-03-30 22:05 dir/dir.tar.gz

-rw-r--r-- root/root       864 2020-03-30 21:46 dir/passwd.9.xz

3.只打包比某个时间更近的文件

[root@localhost tmp]# touch -m -d "20200501" dir/passwd.xz 

#修改passwd.xz的时间戳

[root@localhost tmp]# ll dir

total 12

-rw-r--r--. 1 root root  45 Mar 30 22:05 dir.tar.gz

-rw-r--r--. 1 root root 864 Mar 30 21:46 passwd.9.xz

-rw-r--r--. 1 root root 864 May  1  2020 passwd.xz

[root@localhost tmp]# tar -jcv -f dir.tar.bz2 --newer-mtime="20200404" dir

#只备份2020-4-4以后的文件

tar: Option --newer-mtime: Treating date `20200404' as 2020-04-04 00:00:00

dir/    #备份

tar: dir/dir.tar.gz: file is unchanged; not dumped    #未备份

dir/passwd.xz    #备份

tar: dir/passwd.9.xz: file is unchanged; not dumped   #未备份

[root@localhost tmp]# ll

total 16

drwxrwxrwx. 2 root root   57 Mar 30 22:50 dir

-rw-r--r--. 1 root root 1296 Mar 30 22:52 dir.tar.bz2   #备份文件

-rw-r--r--. 1 root root 1144 Mar 30 22:36 dir.tar.xz

-rwxrw----. 1 root root    6 Mar 19 10:24 root

-rw-r--r--. 1 root root   65 Mar 12 11:04 test

[root@localhost tmp]# tar -jtv -f dir.tar.bz2   #查看备份文件的内容

drwxrwxrwx root/root         0 2020-03-30 22:50 dir/

-rw-r--r-- root/root       864 2020-05-01 00:00 dir/passwd.xz

5.3 xfs文件系统的备份和恢复

       XFS提供了 xfsdump 和 xfsrestore 工具协助备份XFS文件系统中的数据。XFS不需要在备份前被卸载;对使用中的XFS文件系统做备份就可以保证镜像的一致性。XFS的备份和恢复的过程是可以被中断然后继续的,无须冻结文件系统。xfsdump 甚至提供了高性能的多线程备份操作——它把一次dump拆分成多个数据流,每个数据流可以被发往不同的目的地。

5.3.1 备份策略

1.完全备份

       完全备份是指把所有需要备份的数据全部备份。完全备份可以备份整块硬盘、整个分区或某个具体的目录。

       完全备份的好处是,所有数据都进行了备份,系统中任何数据丢失都能恢复,且恢复效率较高。

       完全备份的缺点也很明显,那就是需要备份的数据量较大,备份时间较长,备份了很多无用数据,占用的空间较大,所以完全备份不可能每天执行。

2.增量备份

       累计增量备份是指先进行一次完全备份,服务器运行一段时间之后,比较当前系统和完全备份的备份数据之间的差异,只备份有差异的数据。服务器继续运行,再经过一段时间,进行第二次增量备份。在进行第二次增量备份时,当前系统和第一次增量备份的数据进行比较,也是只备份有差异的数据。第三次增量备份是和第二次增量备份的数据进行比较,以此类推。

d35564c07d6ceaaa469a2a87420a9186.png

       采用累计增量备份的好处是,每次备份需要备份的数据较少,耗时较短,占用的空间较小;坏处是数据恢复比较麻烦,那么当进行数据恢复时,就要先恢复完全备份的数据,再依次恢复第一次增量备份的数据、第二次增量备份的数据和第三次增量备份的数据,最终才能恢复所有的数据。

3.差异备份

       差异备份也要先进行一次完全备份,但是和累计增量备份不同的是,每次差异备份都备份和原始的完全备份不同的数据。也就是说,差异备份每次备份的参照物都是原始的完全备份,而不是上一次的差异备份。

2954ebaad62bb4d1b991d8f6c944bbe8.png

       相比较而言,差异备份既不像完全备份一样把所有数据都进行备份,也不像增量备份在进行数据恢复时那么麻烦,只要先恢复完全备份的数据,再恢复差异备份的数据即可。不过,随着时间的增加,和完全备份相比,变动的数据越来越多,那么差异备份也可能会变得数据量庞大、备份速度缓慢、占用空间较大。

5.3.2  XFS 文件系统备份xfsdump

1.xfsdump的限制

uxfsdump不支援没有挂载的文件系统备份!所以只能备份已挂载的!

uxfsdump必须使用 root 的权限才能操作 (涉及文件系统的关系)

uxfsdump只能备份 XFS 文件系统啊!

uxfsdump备份下来的数据 (档案或储存媒体) 只能让xfsrestore解析

uxfsdump是透过文件系统的 UUID 来分辨各个备份档的,因此不能备份两个具有相同 UUID 的文件系统喔!

2.xfsdump - XFS filesystem incremental dump utility

[root@study ~]# xfsdump [-L S_label] [-M M_label] [-l #] [-f 备份档] 待备份资料

[root@study ~]# xfsdump -I

选项与参数:

-L  :xfsdump会纪录每次备份的 session 标头,这里可以填写针对此文件系统的简易说明

-M  :xfsdump可以纪录储存媒体的标头,这里可以填写此媒体的简易说明

-l  :是 L 的小写,就是指定等级~有 0~9 共 10 个等级喔! (预设为 0,即完整备份)

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

-I  :从 /var/lib/xfsdump/inventory 列出目前备份的信息状态

3.备份前准备

使用fdisk划分一个分区sda3,并格式化为xfs。(此部分操作省略)

[root@localhost mnt]# mount /dev/sda3 /mnt/sda3/    #挂载

[root@localhost mnt]# df -h /dev/sda3     #查看挂载情况,可省略

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       197M   11M  187M   6% /mnt/sda3

[root@localhost mnt]# blkid /dev/sda3   #查看分区信息,可省略

/dev/sda3: UUID="f598c75c-bd26-4543-af2f-8192841c4c8b" TYPE="xfs"

4.完全备份

[root@localhost mnt]# touch /mnt/sda3/1.txt

[root@localhost mnt]# xfsdump -l 0 -f /tmp/sda3 /dev/sda3

xfsdump: using file dump (drive_simple) strategy

xfsdump: version 3.1.3 (dump format 3.0) - type ^C for status and control

 =============== dump label dialog =================

please enter label for this dump session (timeout in 300 sec)

 -> dump_sda3_20200331    #备份说明

session label entered: "dump_sda3_20200331"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/mnt/sda3

xfsdump: dump date: Tue Mar 31 20:30:53 2020

xfsdump: session id: 0dab0e83-90fb-4fef-88db-974bd0e5a2f3

xfsdump: session label: "dump_sda3_20200331"

xfsdump: ino map phase 1: constructing initial dump list

xfsdump: ino map phase 2: skipping (no pruning necessary)

xfsdump: ino map phase 3: skipping (only one dump stream)

xfsdump: ino map construction complete

xfsdump: estimated dump size: 21120 bytes

xfsdump: /var/lib/xfsdump/inventory created

 =============== media label dialog =================

please enter label for media in drive 0 (timeout in 300 sec)

 -> sda3      #备份媒体说明

media label entered: "sda3"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)

xfsdump: dumping ino map

xfsdump: dumping directories

xfsdump: dumping non-directory files

xfsdump: ending media file

xfsdump: media file size 21656 bytes

xfsdump: dump size (non-dir files) : 0 bytes

xfsdump: dump complete: 33 seconds elapsed

xfsdump: Dump Summary:

xfsdump:   stream 0 /tmp/sda3 OK (success)

xfsdump: Dump Status: SUCCESS

[root@localhost mnt]# ls /tmp/

root  sda3  test

#上面的备份命令需要交互,下面的命令无交互,常用于脚本。

[root@localhost mnt]# xfsdump -l 0 -L dump_sda3_20200331  -M sda3 -f /tmp/sda3 /dev/sda3

[root@localhost mnt]# xfsdump -I     #

file system 0:

fs id:  f598c75c-bd26-4543-af2f-8192841c4c8b

session 0:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:42:51 2020

session label: "dump_sda3_20200331"

session id: b2315293-f79f-43b2-9276-157268eea187

level:  0

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3

start:  ino 131 offset 0

end:  ino 132 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21656

mfile start: ino 131 offset 0

mfile end: ino 132 offset 0

media label: "sda3"

media id: 23a27bd0-e47b-4bee-9005-d530e6303f63

xfsdump: Dump Status: SUCCESS

5.第一次增量备份

[root@localhost mnt]# ls sda3/

1.txt

[root@localhost mnt]# touch sda3/2.txt

[root@localhost mnt]# ls sda3/

1.txt  2.txt

[root@localhost mnt]# xfsdump -l 1 -L dump_sda3_20200331-1 -M sda3 -f /tmp/sda3-1 /dev/sda3

xfsdump: using file dump (drive_simple) strategy

xfsdump: version 3.1.3 (dump format 3.0) - type ^C for status and control

xfsdump: level 1 incremental dump of localhost.localdomain:/mnt/sda3 based on level 0 dump begun Tue Mar 31 20:42:51 2020

xfsdump: dump date: Tue Mar 31 20:47:58 2020

xfsdump: session id: 989e4ade-b8c3-40be-9714-10c648f6b46c

xfsdump: session label: "dump_sda3_20200331-1"

xfsdump: ino map phase 1: constructing initial dump list

xfsdump: ino map phase 2: skipping (no pruning necessary)

xfsdump: ino map phase 3: skipping (only one dump stream)

xfsdump: ino map construction complete

xfsdump: estimated dump size: 21120 bytes

xfsdump: creating dump session media file 0 (media 0, file 0)

xfsdump: dumping ino map

xfsdump: dumping directories

xfsdump: dumping non-directory files

xfsdump: ending media file

xfsdump: media file size 21680 bytes

xfsdump: dump size (non-dir files) : 0 bytes

xfsdump: dump complete: 0 seconds elapsed

xfsdump: Dump Summary:

xfsdump:   stream 0 /tmp/sda3-1 OK (success)

xfsdump: Dump Status: SUCCESS

[root@localhost mnt]# xfsdump -I

file system 0:

fs id:  f598c75c-bd26-4543-af2f-8192841c4c8b

session 0:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:42:51 2020

session label: "dump_sda3_20200331"

session id: b2315293-f79f-43b2-9276-157268eea187

level:  0

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3

start:  ino 131 offset 0

end:  ino 132 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21656

mfile start: ino 131 offset 0

mfile end: ino 132 offset 0

media label: "sda3"

media id: 23a27bd0-e47b-4bee-9005-d530e6303f63

session 1:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:47:58 2020

session label: "dump_sda3_20200331-1"

session id: 989e4ade-b8c3-40be-9714-10c648f6b46c

level:  1

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3-1

start:  ino 132 offset 0

end:  ino 133 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21680

mfile start: ino 132 offset 0

mfile end: ino 133 offset 0

media label: "sda3"

media id: 42121b62-abf0-4e26-98f1-48d50f01b633

xfsdump: Dump Status: SUCCESS

6.第二次增量备份

[root@localhost mnt]# touch sda3/{3,4,5,6}.txt

[root@localhost mnt]# ls sda3/

1.txt  2.txt  3.txt  4.txt  5.txt  6.txt

[root@localhost mnt]# xfsdump -l 2 -L dump_sda3_20200331-2 -M sda3 -f /tmp/sda3-2 /dev/sda3

xfsdump: using file dump (drive_simple) strategy

xfsdump: version 3.1.3 (dump format 3.0) - type ^C for status and control

xfsdump: level 2 incremental dump of localhost.localdomain:/mnt/sda3 based on level 1 dump begun Tue Mar 31 20:47:58 2020

xfsdump: dump date: Tue Mar 31 20:56:34 2020

xfsdump: session id: 1a6e946b-fd0e-4e70-bb00-b82c04291de7

xfsdump: session label: "dump_sda3_20200331-2"

xfsdump: ino map phase 1: constructing initial dump list

xfsdump: ino map phase 2: skipping (no pruning necessary)

xfsdump: ino map phase 3: skipping (only one dump stream)

xfsdump: ino map construction complete

xfsdump: estimated dump size: 22080 bytes

xfsdump: creating dump session media file 0 (media 0, file 0)

xfsdump: dumping ino map

xfsdump: dumping directories

xfsdump: dumping non-directory files

xfsdump: ending media file

xfsdump: media file size 23624 bytes

xfsdump: dump size (non-dir files) : 0 bytes

xfsdump: dump complete: 0 seconds elapsed

xfsdump: Dump Summary:

xfsdump:   stream 0 /tmp/sda3-2 OK (success)

xfsdump: Dump Status: SUCCESS

[root@localhost mnt]# xfsdump -I

file system 0:

fs id:  f598c75c-bd26-4543-af2f-8192841c4c8b

session 0:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:42:51 2020

session label: "dump_sda3_20200331"

session id: b2315293-f79f-43b2-9276-157268eea187

level:  0

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3

start:  ino 131 offset 0

end:  ino 132 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21656

mfile start: ino 131 offset 0

mfile end: ino 132 offset 0

media label: "sda3"

media id: 23a27bd0-e47b-4bee-9005-d530e6303f63

session 1:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:47:58 2020

session label: "dump_sda3_20200331-1"

session id: 989e4ade-b8c3-40be-9714-10c648f6b46c

level:  1

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3-1

start:  ino 132 offset 0

end:  ino 133 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21680

mfile start: ino 132 offset 0

mfile end: ino 133 offset 0

media label: "sda3"

media id: 42121b62-abf0-4e26-98f1-48d50f01b633

session 2:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:56:34 2020

session label: "dump_sda3_20200331-2"

session id: 1a6e946b-fd0e-4e70-bb00-b82c04291de7

level:  2

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3-2

start:  ino 133 offset 0

end:  ino 137 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 23624

mfile start: ino 133 offset 0

mfile end: ino 137 offset 0

media label: "sda3"

media id: f410ea13-eada-4566-87c8-844b1a939ce1

xfsdump: Dump Status: SUCCESS

5.3.3 XFS 文件系统还原xfsrestore

1.xfsrestore - XFS filesystem incremental restore utility

[root@study ~]# xfsrestore -I                          

 <==用来察看备份文件资料< span="">

[root@study ~]# xfsrestore [-f 备份档] [-L S_label] [-s] 待复原目录

<==单一档案全系统复原< span="">

[root@study ~]# xfsrestore [-f 备份文件] -r 待复原目录

<==透过累积备份文件来复原系统< span="">

[root@study ~]# xfsrestore [-f 备份文件] -i待复原目录

<==进入互动模式< span="">

选项与参数:

-I  :跟xfsdump相同的输出!可查询备份数据,包括 Label 名称与备份时间等

-f  :后面接的就是备份档!

-L  :就是 Session 的 Label name 喔!可用 -I 查询到的数据,在这个选项后输入!

-s  :需要接某特定目录,亦即仅复原某一个档案或目录之意!

-r  :如果是用档案来储存备份数据,那这个就不需要使用。如果是一个磁带内有多个档案,需要这东西来达成累积复原

-i:进入互动模式,进阶管理员使用的!一般我们不太需要操作它!

2.查看备份信息

[root@localhost mnt]# xfsrestore -I

file system 0:

fs id:  f598c75c-bd26-4543-af2f-8192841c4c8b

session 0:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:42:51 2020

session label: "dump_sda3_20200331"

session id: b2315293-f79f-43b2-9276-157268eea187

level:  0

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3

start:  ino 131 offset 0

end:  ino 132 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21656

mfile start: ino 131 offset 0

mfile end: ino 132 offset 0

media label: "sda3"

media id: 23a27bd0-e47b-4bee-9005-d530e6303f63

session 1:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:47:58 2020

session label: "dump_sda3_20200331-1"

session id: 989e4ade-b8c3-40be-9714-10c648f6b46c

level:  1

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3-1

start:  ino 132 offset 0

end:  ino 133 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 21680

mfile start: ino 132 offset 0

mfile end: ino 133 offset 0

media label: "sda3"

media id: 42121b62-abf0-4e26-98f1-48d50f01b633

session 2:

mount point: localhost.localdomain:/mnt/sda3

device:  localhost.localdomain:/dev/sda3

time:  Tue Mar 31 20:56:34 2020

session label: "dump_sda3_20200331-2"

session id: 1a6e946b-fd0e-4e70-bb00-b82c04291de7

level:  2

resumed: NO

subtree: NO

streams: 1

stream 0:

pathname: /tmp/sda3-2

start:  ino 133 offset 0

end:  ino 137 offset 0

interrupted: NO

media files: 1

media file 0:

mfile index: 0

mfile type: data

mfile size: 23624

mfile start: ino 133 offset 0

mfile end: ino 137 offset 0

media label: "sda3"

media id: f410ea13-eada-4566-87c8-844b1a939ce1

xfsdump: Dump Status: SUCCESS

3.还原level0

[root@localhost mnt]# rm -f sda3/*

[root@localhost mnt]# ls sda3/

[root@localhost mnt]# xfsrestore -f /tmp/sda3 /mnt/sda3/

xfsrestore: using file dump (drive_simple) strategy

xfsrestore: version 3.1.3 (dump format 3.0) - type ^C for status and control

xfsrestore: searching media for dump

xfsrestore: examining media file 0

xfsrestore: dump description:

xfsrestore: hostname: localhost.localdomain

xfsrestore: mount point: /mnt/sda3

xfsrestore: volume: /dev/sda3

xfsrestore: session time: Tue Mar 31 20:42:51 2020

xfsrestore: level: 0

xfsrestore: session label: "dump_sda3_20200331"

xfsrestore: media label: "sda3"

xfsrestore: file system id: f598c75c-bd26-4543-af2f-8192841c4c8b

xfsrestore: session id: b2315293-f79f-43b2-9276-157268eea187

xfsrestore: media id: 23a27bd0-e47b-4bee-9005-d530e6303f63

xfsrestore: using online session inventory

xfsrestore: searching media for directory dump

xfsrestore: reading directories

xfsrestore: 1 directories and 1 entries processed

xfsrestore: directory post-processing

xfsrestore: restoring non-directory files

xfsrestore: restore complete: 0 seconds elapsed

xfsrestore: Restore Summary:

xfsrestore:   stream 0 /tmp/sda3 OK (success)

xfsrestore: Restore Status: SUCCESS

[root@localhost mnt]# ls sda3/

1.txt

4.还原level 1

[root@localhost mnt]# xfsrestore -f /tmp/sda3-1 /mnt/sda3/

[root@localhost mnt]# ls sda3/

1.txt  2.txt

5.还原level 2

[root@localhost mnt]# xfsrestore -f /tmp/sda3-2 /mnt/sda3/

[root@localhost mnt]# ls sda3/

1.txt  2.txt  3.txt  4.txt  5.txt  6.txt

6.交互式部分还原

[root@localhost mnt]# xfsrestore -f /tmp/sda3-2 -i /mnt/sda3/

xfsrestore: using file dump (drive_simple) strategy

xfsrestore: version 3.1.3 (dump format 3.0) - type ^C for status and control

xfsrestore: searching media for dump

xfsrestore: examining media file 0

xfsrestore: dump description:

xfsrestore: hostname: localhost.localdomain

xfsrestore: mount point: /mnt/sda3

xfsrestore: volume: /dev/sda3

xfsrestore: session time: Tue Mar 31 20:56:34 2020

xfsrestore: level: 2

xfsrestore: session label: "dump_sda3_20200331-2"

xfsrestore: media label: "sda3"

xfsrestore: file system id: f598c75c-bd26-4543-af2f-8192841c4c8b

xfsrestore: session id: 1a6e946b-fd0e-4e70-bb00-b82c04291de7

xfsrestore: media id: f410ea13-eada-4566-87c8-844b1a939ce1

xfsrestore: using online session inventory

xfsrestore: searching media for directory dump

xfsrestore: reading directories

xfsrestore: 1 directories and 6 entries processed

xfsrestore: directory post-processing

 ================= subtree selection dialog ==================

the following commands are available:

       pwd

ls []

cd []

add []

delete []

       extract

       quit

       help

 -> ls

             136 6.txt

             135 5.txt

             134 4.txt

             133 3.txt

             132 2.txt

             131 1.txt

 -> add 2.txt     #本条无效,因为2.txt不在本次备份文件中

 -> add 4.txt

 -> add 6.txt

 -> extract

 --------------------------------- end dialog ---------------------------------

xfsrestore: restoring non-directory files

xfsrestore: restore complete: 97 seconds elapsed

xfsrestore: Restore Summary:

xfsrestore:   stream 0 /tmp/sda3-2 OK (success)

xfsrestore: Restore Status: SUCCESS

[root@localhost mnt]# ls sda3/

4.txt  6.txt

7.非交互式部分还原

如果只想要复原某一个目录或档案的话,直接加上『 -s 目录』这个选项与参数即可!前提是你要知道被还原的文档名。

[root@localhost mnt]# xfsrestore -f /tmp/sda3-2 -s 6.txt /mnt/sda3

[root@localhost mnt]# ls sda3/

6.txt

5.4 其他备份工具

5.4.1 dd - convert and copy a file

1.语法

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

2.用法

(1)将本地的/dev/hdb整盘备份到/dev/hdd

[root@localhost ~]# dd if=/dev/hdb of=/dev/hdd

(2)将/dev/hdb全盘数据备份到指定路径的image文件

[root@localhost ~]# dd if=/dev/hdb of=/root/image

(3)将备份文件恢复到指定盘

[root@localhost ~]# dd if=/root/image of=/dev/hdb

(4)备份与恢复MBR

[root@localhost ~]# dd if=/dev/hda of=/root/image count=1 bs=512

[root@localhost ~]# dd if=/root/image of=/dev/had

3./dev/null

        把/dev/null看作"黑洞", 它等价于一个只写文件,所有写入它的内容都会永远丢失.,而尝试从它那儿读取内容则什么也读不到。然而, /dev/null对命令行和脚本都非常的有用。

(1)禁止标准输出

[root@localhost ~]# echo "aaaa"

aaaa

[root@localhost ~]# echo "aaaa" >/dev/null

(2)禁止标准错误输出

[root@localhost ~]# cat aaaaa

cat: aaaaa: No such file or directory

[root@localhost ~]# cat aaaaa 2>/dev/null

(3)禁止标准输出和标准错误输出

[root@localhost tmp]# ls

root  test

[root@localhost tmp]# cat root

aaaaa

[root@localhost tmp]# cat calf

cat: calf: No such file or directory

[root@localhost tmp]# cat root calf >/dev/null 2>/dev/null

(4)清空文件内容

[root@localhost tmp]# cat /dev/null >root

[root@localhost tmp]# cat root

4./dev/zero

        /dev/zero也是一个伪文件, 但它实际上产生连续不断的null的流(二进制的零流,而不是ASCII型的)。  /dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件。

(1)创建一个10M大小的临时文件test

[root@localhost tmp]# dd if=/dev/zero of=test bs=1M count=10

10+0 records in

10+0 records out

10485760 bytes (10 MB) copied, 0.021191 s, 495 MB/s

5.5 总结

l压缩指令为透过一些运算方法去将原本的档案进行压缩,以减少档案所占用的磁盘容量。

l压缩文件案的扩展名大多是:『*.gz, *.bz2, *.xz, *.tar, *.tar.gz, *.tar.bz2, *.tar.xz』

l常见的压缩指令有 gzip, bzip2, xz。压缩率最佳的是 xz。

ltar 可以用来进行档案打包,并可支持 gzip, bzip2, xz 的压缩。

lxfsdump 指令可备份文件系统或单一目录,xfsrestore 指令可还原被 xfsdump 建置的备份档;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值