2023.5.29Linux系统压缩打包

02.Linux系统压缩打包

1.zip压缩工具

2.TAR压缩工具

3.TAR实践案例

windows下我们接触最多的压缩文件就是 rar格式,但linux有自己所特有的压缩工具

如果希望windiows和linux互相能使用的压缩工具,建议.zip格式

压缩的好处主要有

节省磁盘空间占用率

节省网络传输带宽消耗

网络传输更加快捷

 

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

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

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

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

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

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

注意:

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

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

1.zip压缩工具

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

//压缩文件维zipz包

【root@wing ~】# zip   filename.zip  filename

//压缩目录为zip包

【root@wing  ~】# zip  -r  dir.zip dir/

//解压 zip文件包,默认解压至当前目录

【root@wing ~】unzip  filename.zip

2.TAR压缩工具

tar是linux下最常用的压缩与解压缩,支持文件和目录的压缩归档

语法

tar  [-zjvxvfpp]  filname

c     //创建新的归档文件

x    //对归档文件解包

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

v   //输出命令的归档或接报的过程

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

c    //指定解压目录位置

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

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

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

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

h    //打包软链接

--har-的reference    //打包硬链接

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

//常用打包与压缩组合

czf        //打包tar.gz格式

cjf        //打包tar.bz格式

cJf       //打包tar.xz格式

 

zxf      //解压tar.gz格式

jxf       //解压tar.gz格式

xf        //自动选择解压模式

tf         //查看压缩包内容

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

//以gzip归档方式打包压缩

tar czf    test.tar.gz    test/   test2/

//以bz2方式压缩

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

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

【root@qwing】# cd  /

【root@qwing】# tar czfh local.tar.gz   etc/rc.local

 

//打包/tmp下所有文件

【root@qwing】#  cd  /

【root@qwing】#  find  tmp/  -type  f |   xargs  tar  czf  tmp.tar.gz 

//打包/tmp下所有文件

【root@qwing】# tar  czf tmp.tar.gz |  xargs  find  tmp/  -type f

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

//排除单个文件

【root@qwing】#  tar czf  etyc.tar.gz  --exclude=services  etc/

//排除多个文件

【root@qwing】# tar czf etc.tar.gz  --exclude=etc/services  --exclude=etc/rc.local etc/

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

【root@qwing】# cat paichu.list

etc/services

etc/rc.local

etc/rc.d/rc.local

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

【root@qwing】#  tar czfx etc.tar.gz paichu.list etc/

3.查看压缩文件

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

【root@qwing】# tar  tf  test.tar.gz

4.解压缩文件

//解包或者解压缩

【root@qwing】#  tar xf  test.tar.gz

//将tar.gz解压刀片其他目录

【root@qwing】# tar  xf  /etc/local.tar.gz.  -C  /tmp

注意  不管是打包还是解包,源文件是不会被删除的,但会覆盖当前已经存在的文件或者目录。

3.TAR实践案例

其它环境准备

[root@localhost  ~】# yum  install  mariadb-server

[root@localhost  ~】#systemctl  start  maridb

[root@localhost  ~】# mkdir  /backup

[root@localhost  ~】# systemctl stop  mysqld

案例1 mysql物理备份

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

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

案例2mysql物理备份及恢复

[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  root@192.168..9.3:/tmp

//j建议方法

//接受B主机,需要接听端口

[root@lhostB  ~】# systemctl  stoop  firewalld.service

[root@lhostB  ~】#  nc  -1  8888  |tar  -xzf  -  -C  /tmp

//发送方A主机

[root@lhostA  ~】#  tar  -czf  -  /etc  |  nc  192.168.9.3  8888

tar:  Removing   leading  `/'  from   member  names

没有网络场景,没有账户密码,登录服务器,通过nc方式进行文件传输  命令操作 nc

posted @ 2023-05-29 23:07  必兮相语--  阅读(87)  评论(0)    收藏  举报