02.Linux系统压缩打包

  1. Linux系统压缩打包

    windows下我们接触最多的压缩文件就是.rar格式, 但Linux有自己所特有的压缩工具。如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式

    压缩的好处主要有:

    节省磁盘空间占用率节省网络传输带宽消耗网络传输更加快捷

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

    .gz gzip //压缩工具压缩的文件
    .bz2 bzip2 //压缩工具压缩的文件
    .tar tar //tar没有压缩功能,只是把一个目录合并成一个文件
    .tar.gz //先使用tar打包,然后使用gzip压缩归档
    .tar.bz2 //先使用tar打包,然后使用bzip压缩归档

    注意:
    1.Linux下常用压缩文件以.tar.gz结尾.
    2.Linux下压缩文件必须带后缀.

  2. 1.ZIP压缩工具

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

    //压缩文件为zip包

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

    //压缩目录为zip包

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

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

    [root@lxgyw ~]# unzip filename.zip

  3. 2.TAR压缩工具

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

    语法:tar [-zjxcvfpP] filename

    c//创建新的归档文件

    x //对归档文件解包

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

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

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

    C//指定解压目录位置

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

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

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

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

    h //打包软链接

    --hard-dereference //打包硬链接

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

    //常用打包与压缩组合

    czf //打包tar.gz格式

    cjf //打包tar.bz格式

    cJf //打包tar.xz格式

    zxf //解压tar.gz格式

    jxf //解压tar.bz格式

    xf //自动选择解压模式

    tf //查看压缩包内容

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

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

    tar czf test.tar.gz test/ test2/

    //以bz2方式压缩

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

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

    [root@lxgyw ~]# cd /

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

    //打包/tmp下所有文件

    [root@lxgyw ~]# cd /

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

    //打包/tmp下所有文件

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

    //打包/tmp下所有文件

    [root@lxgyw /]# tar czf tmp.tar.gz $(find tmp/ -type f)

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

    //排除单个文件

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

    //排除多个文件

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

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

    [root@lxgyw /]# cat paichu.list

    etc/services

    etc/rc.local

    etc/rc.d/rc.local

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

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

    3.查看压缩文件

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

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

    4.解压缩文件

    //解包或者解压缩

    [root@lxgyw /]# tar xf test.tar.gz

    //将tar.gz解压至其他目录

    [root@student ~]# tar xf /etc/local.tar.gz -C /tmp

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

  4. 3.TAR实践案例

    基础环境准备

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

    [root@localhost ~]# systemctl start mariadb

    [root@localhost ~]# mkdir /backup

    案例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 root@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
发出的红包

打赏作者

郭亚望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值