linux固件打包工具,02-Linux系统压缩打包

压缩的好处主要有:

节省磁盘空间占用率

节省网络传输带宽消耗

网络传输更加快捷

Linux系统常见的后缀名所对应的压缩工具

.gz gzip //压缩工具压缩的文件

.bz2 bzip2 //压缩工具压缩的文件

.tar tar //tar没有压缩功能,只是把一个目录合并成一个文件

.tar.gz //先使用tar打包,然后使用gzip压缩归档

.tar.bz2 //先使用tar打包,然后使用bzip压缩归档

注意:

1.Linux下常用压缩文件以.tar.gz结尾.

2.Linux下压缩文件必须带后缀.

ZIP压缩工具

zip是压缩工具,unzip是解压缩工具

//压缩文件为zip包

[root@localhost ~]# zip filename.zip filename

//压缩目录为zip包

[root@localhost ~]# zip -r dir.zip dir/

//解压zip文件包

[root@localhost ~]# unzip filename.zip

TAR压缩工具

语法:tar [-zjxcvfpP] filename

c //创建新的归档文件

x //对归档文件解包

t //列出归档文件里的文件列表

v //输出命令的归档或解包的过程

f //指定包文件名,多参数f写最后

C //指定解压目录位置

z //使用gzip压缩归档后的文件(.tar.gz)

j //使用bzip2压缩归档后的文件(.tar.bz2)

J //使用xz压缩归档后的文件(tar.xz)

X //排除多个文件(写入需要排除的文件名称)

h //打包软链接

**--exclude //在打包的时候写入需要排除文件或目录**

1.将文件或目录进行打包压缩

//以gzip方式打包压缩

tar czf test.tar.gz test/ test2/

//以bz2方式压缩

tar cjf test.tar.bz2 dir.txt dir/

//打包链接文件,打包链接文件的真实文件

[root@localhost ~]# cd /

[root@localhost /]# tar czfh local.tar.gz etc/rc.local

//打包/tmp下所有文件

[root@localhost ~]# cd /

[root@localhost /]# find tmp/ -type f |xargs tar czf tmp.tar.gz

//打包/tmp下所有文件

[root@localhost /]# tar czf tmp.tar.gz | xargs find tmp/ -type f

//打包/tmp下所有文件

[root@localhost /]# tar czf tmp.tar.gz `find tmp/ -type f`

2.排除文件, 并打包压缩

//排除单个文件

[root@localhost /]# tar czf etc.tar.gz --exclude=etc/services etc/

//排除多个文件

[root@localhost /]# tar czf etc.tar.gz --exclude=etc/services --exclude=etc/rc.local etc/

//将需要排除的文件写入文件中

[root@localhost /]# cat paichu.list

etc/services

etc/rc.local

etc/rc.d/rc.local

//指定需要排除的文件列表, 最后进行打包压缩

[root@localhost /]# tar czfX etc.tar.gz paichu.list etc/

3.查看压缩文件

//查看压缩包内容和解压

[root@xuliangwei /]# tar tf test.tar.gz

4.解压缩文件

//排除单个文件

[root@localhost /]# tar czf etc.tar.gz --exclude=etc/services etc/

//排除多个文件

[root@localhost /]# tar czf etc.tar.gz --exclude=etc/services --exclude=etc/rc.local etc/

//将需要排除的文件写入文件中

[root@localhost /]# cat paichu.list

etc/services

etc/rc.local

etc/rc.d/rc.local

//指定需要排除的文件列表, 最后进行打包压缩

[root@localhost /]# tar czfX etc.tar.gz paichu.list etc/

TAR实践案例

案例1 mysql物理备份及恢复

[root@localhost ~]# tar -cJf /backup/mysql.tar.xz /var/lib/mysql

[root@localhost ~]# tar -xf /backup/mysql.tar.xz -C /

案例2 mysql物理备份及恢复

[root@localhost ~]# cd /var/lib/mysql

[root@localhost mysql]# tar -cJf /backup/mysql.tar.xz *

[root@localhost mysql]# tar -tf /backup/mysql.tar.xz

[root@localhost mysql]# tar -xf /backup/mysql.tar.xz -C /var/lib/mysql

案例3 host A /etc (海量小文件) --> host A /tmp

[root@localhost ~]# tar -czf - /etc |tar -xzf - -C /tmp

案例4 host A /etc (海量小文件) --> host B /tmp

//常规方法

[root@localhost ~]# scp -r /etc 192.168.69.113:/tmp

//建议方法:

//接收B主机, 需要监听端口

[root@hostB ~]# systemctl stop firewalld.service

[root@hostB ~]# nc -l 8888 |tar -xzf - -C /tmp

//发送方A主机

[root@hostA ~]# tar -czf - /etc | nc 192.168.69.113 8888

tar: Removing leading `/' from member names

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值