这世界没有压缩怎么行
一、压缩简介
日常的文件都是由01这两个数字组成的,1B=8bit,记录整数1时,其他位会补零,压缩就是使文件的空余的位放出来,把容量节省出来
更多解释文档
我们常见的压缩包后缀有
*.Z
*.zip
*.gz
*.bz2
*.xz
*.tar
*.tar.gz
*.tar.bz2
*.tar.xz
二、压缩指令
gzip
- 可以解压
compress、zip、gzip
等软件的压缩包
- 建立的压缩包后缀是
*.gz
语法
1 2 3 4 5 6
| gzip [-] file -c: 将压缩的数据输出到屏幕上 -d: 解压缩 -t: 检验压缩包的一致性 -v: 显示原文件与压缩文件之比 -
|
- 要想查看
gzip
压缩的内容要使用zcat
/zmore
/zless
,查找某数据则使用egrep
- 因为
gzip
主要是想替换compress
这个压缩软件,所以gzip
同样可以解压compress
压缩的文件,zcat
也可以查看compress
压缩的内容
- 压缩文件名自定义使用指令
gzip -c filename > 自定义文件名.gz
- 更多高级用法man
bzip2
语法
1 2 3 4 5 6 7
| bzip2 [-] file -c: 将压缩的数据输出到屏幕上 -d: 解压缩 -k: 保留原来的文件 -z: 压缩(默认) -v: 显示原文件与压缩文件之比 -
|
xz
1 2 3 4 5 6
| xz [-] file -d: 解压缩 -t: 测试压缩文件是否错误 -l: 列出压缩文件的信息 -k: 保留源文件 -c: 输出数据
|
- 后缀
*.xz
- xzcat/xzmore/xzless/xzgrep
总结
- 三个软件的压缩比从小到大是
xz>bzip2>gzip
,压缩比越小压缩效率越好
- 压缩效率越好花费时间越长
三、打包指令tar
使用tar时要自己命名
语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| tar [-z|j|J] [cv|tv|xv] [-f filename] [sourcefile] options -c: 打包文件,-v可以查看打包的文件 -t: 查看打包文件包含了哪些文件 -x: 解包,使用-C后可接解包目录 -z: 通过gzip的形式解压、压缩,建议后缀tar.gz -j: 通过bizp2的形式压缩、解压,建议后缀tar.bzip2 -J: 通过xz的形式压缩、解压,建议后缀tar.xz -v: 压缩、解压过程中,显示处理的文件,解压时显示属性权限 -f filename: c{z|j|J} 代表处理后的名字,t|x代表要处理的文件 -C dir: 解压的目录 -p: 保留权限和属性 -P:保留绝对路径 --exclude=FILE: FILE表示这个文件不会被打包
|
尝试压缩/etc这个目录的文件,42M的源文件
- 纯tar后37M
- gz形式的tar12M
- xz形式的tar8.2M
1.不过值得一提的是gz花的时间几乎1s,xz花了几乎有半分钟
2.解压过程中把路径中的/
自动去掉了是防止解压时将文件放到/etc中去,要备份/
就要加-P
3.使用time 指令
会把这个指令花费的时间显示出来
1 2 3 4 5
| [root@study ~] 42M /etc -rw-r--r--. 1 root root 12M 1月 12 21:58 etc.tar.gz -rw-r--r--. 1 root root 37M 1月 12 12:54 /tmp/etc.tar -rw-r--r--. 1 root root 8.2M 1月 12 22:13 /tmp/etc.tar.xz
|
特殊用法
不想打包包里的某文件时
打包比某时刻还新的文件
- 先确定时间种类和某个时间
- 参考指令
tar -cv -f filename --newer-mtime="YYYY/mm/DD" souredir/file
四、xfs文件系统备份和还原
xfsdump&xfsrestore这两个工具对CentOS来说挺重要的,专门针对xfs文件系统
(一)xfsdump备份xfs文件系统
主要两个功能
- 完整的备份整个文件系统
- 和以前的备份对比进行增量备份
限制
- 只能备份挂载的文件系统
- 需要神的旨意(root)
- 只能搞xfs系统
- 恢复只能让xfsrestore解析
- 不能备份两个相同UUID的文件系统,因为xfsdump使用UUID来识别每个文件
语法
1 2 3 4 5 6
| xfs [-L label] [-M label] [-l -L: 记录每次备份的session标头,针对该文件系统的简易说明 -M: 记录存储媒介表头,写这个媒介的简易说明 -l: 指定等级0-9,0代表完全备份 -f: 后接备份后的文件名 -I: 从/var/lib/xfsdump/inventory读取备份信息
|
例子
- 整个备份/boot(注意/boot是一个单独的文件系统)到/srv/boot.dump
1
| xfsdump -l 0 -L boot -M boot -f /srv/boot.dump /boot
|
1
| xfsdump -l 1 -L boot_1 -M boot_1 -f /srv/boot.dump1 /boot
|
(二)系统还原xfsrestore
1 2 3 4 5 6 7 8 9
| 1.xfsrestore - Is 2.xfsrestore [-f backupfile] [-L label] restoredir 3.xfsrestore [-f backupfile] -i restoredir -I: 与xfsdump -I相同,显示备份信息 -f: 后接备份文件 -L: 后接session的标头 -s: 后接恢复的指定文件和目录 -r: 有磁带时使用 -i: 进入交互模式,高端恢复操作
|
例子
1
| xfsrestore -f /srv/boot.dump -L boot /tmp/boot
|
1
| xfsrestore -f /srv/boot.dump1 -L boot_1 /tmp/boot
|
使用diff -r dir1 dir2
可以比较连个目录的不同支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| [root@study ~] xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.7 (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: study.centos.vbird xfsrestore: mount point: /boot xfsrestore: volume: /dev/sda2 xfsrestore: session time: Mon Jan 13 21:39:48 2020 xfsrestore: level: 0 xfsrestore: session label: "boot" xfsrestore: media label: "boot" xfsrestore: file system id: 1bedab37-0c01-459c-a8f7-cfd4c5f203f9 xfsrestore: session id: fe824faa-abad-43a8-ae88-9bcfe030fa59 xfsrestore: media id: 705e7452-4a61-4879-bcce-c358b1cfe43d xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 11 directories and 339 entries processed xfsrestore: directory post-processing
========================== subtree selection dialog ==========================
the following commands are available: pwd ls [ <path> ] cd [ <path> ] add [ <path> ] delete [ <path> ] extract quit help
-> ls 114 initramfs-3.10.0-1062.el7.x86_64kdump.img 113 1.text 75 initramfs-3.10.0-1062.el7.x86_64.img 77 vmlinuz-0-rescue-1eb9996494284b65aee1669488d9aae0 76 initramfs-0-rescue-1eb9996494284b65aee1669488d9aae0.img 74 vmlinuz-3.10.0-1062.el7.x86_64 73 symvers-3.10.0-1062.el7.x86_64.gz 72 config-3.10.0-1062.el7.x86_64 71 System.map-3.10.0-1062.el7.x86_64 70 .vmlinuz-3.10.0-1062.el7.x86_64.hmac 68 grub/ 1572928 grub2/ 67 efi/
-> q
--------------------------------- end dialog ---------------------------------
xfsrestore: Unmark and quit xfsrestore: restore complete: 44 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /srv/boot.dump OK (success) xfsrestore: Restore Status: SUCCESS
|
add
添加恢复文件、目录,extract
开始恢复
另外使用-s file/dir
也可以恢复指定文件目录
五、其他压缩备份指令
dd(disk dump)
常用来备份文件系统,会将文件系统完整的备份,包括UUID
语法
1 2 3 4 5
| dd if= of= bs= count= if: input file/dev of: output file/dev bs: 每个block的大小,默认512KB count: block的数量
|
例子
我想把/boot备份一下,步骤如下
- 备份的地方:U盘,其他分区等,没多余的U盘,这里我选择分区
gdisk分出2GB的分区/dev/sda4(容量的要求要比挂载/boot的设备大)
1 2 3 4 5
| Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 2 6144 2103295 1024.0 MiB 0700 3 2103296 65026047 30.0 GiB 8E00 4 65026048 69220351 2.0 GiB 8300 Linux filesystem
|
1 2 3
| [root@study /] 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda2 1038336 218416 819920 22% /boot
|
- 使用
dd if=/dev/sda2 of=dev/sda4
备份
1 2 3 4
| [root@study /] 记录了2097152+0 的读入 记录了2097152+0 的写出 1073741824字节(1.1 GB)已复制,7.00172 秒,153 MB/秒
|
1 2 3 4 5
| [root@study /] [root@study /] 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda4 1014M 214M 801M 22% /mnt /dev/sda2 1014M 214M 801M 22% /boot
|
可以发现备份的sda4与sda2一模一样,但是有个小问题,我原本的sda4是有2G的,怎么变成1G了?
那是因为dd将sda2的所有信息都拷过来了,分区,格式,元数据,导致两个硬盘一模一样,这时候需要执行xfs_growfs /mnt
即可(具体原因在未来的章节)