zoukankan      html  css  js  c++  java
  • 第8章:归档与压缩

    第8章:归档与压缩

      定期备份不可小视,我们可以通过shell脚本来实现备份自动化。其中数据备份一般要使用到归档与压缩,归档与压缩对于系统管理员或普通用户来说是比较常用的工具,有许多不同的压缩格式,要结合不同的使用方法才可以获得最佳的压缩效果。还有文件加密也是一种保护数据常用的方法,文件加密之前通常都要先进行归档和压缩。

    8.1tar 归档

    tar命令主要用来归档文件,可以用tar将多个文件和文件夹保存为单个文件,同时还能保留所有文件的属性、权限等。

    范例1创建tar归档

    [root@cloucentos6 home]# tar --help | grep '-f\,'

      -f, --file=ARCHIVE         使用归档文件或 ARCHIVE 设备

    [root@cloucentos6 home]# tar --help | grep "-c\,"    #查看系统tar帮助 –c选项的说明

      -c, --create               创建一个新归档

     [root@cloucentos6 home]# for((i=1;i<4;i++));do ifconfig > font$i.txt;done   #使用for循环创建3个测试文本

    [root@cloucentos6 home]# ls font*

    font1.txt  font2.txt  font3.txt

    [root@cloucentos6 home]# tar -cf font.tar font1.txt font2.txt font3.txt

    范例2查看归档及追加归档文件

    [root@cloucentos6 home]# tar --help | grep '-v\,'

      -v, --verbose              详细地列出处理的文件

    [root@cloucentos6 home]# tar --help | grep '-r\,'

      -r, --append               追加文件至归档结尾

    [root@cloucentos6 home]# tar -tf font.tar              #查看归档文件

    font1.txt

    font2.txt

    font3.txt

    [root@cloucentos6 home]# tar -rvf font.tar font4.txt

    font4.txt

    [root@cloucentos6 home]# tar -tf font.tar

    font1.txt

    font2.txt

    font3.txt

    font4.txt

    [root@cloucentos6 home]# tar -tvf font.tar             #查看更加详细的归档文件

    -rw-r--r-- root/root     10240 2017-04-29 18:05 font1.txt

    -rw-r--r-- root/root       884 2017-04-29 18:03 font2.txt

    -rw-r--r-- root/root       884 2017-04-29 18:03 font3.txt

    -rw-r--r-- root/root       884 2017-04-29 18:14 font4.txt

    范例3从归档文件中提取文本或文件夹

    [root@cloucentos6 home]# tar --help | grep '-x\,'

      -x, --extract, --get       从归档中解出文件

    [root@cloucentos6 home]# tar --help | grep '-C\,'

      -C, --directory=DIR        改变至目录 DIR

    [root@cloucentos6 home]# tar -xf font.tar

    [root@cloucentos6 home]# ls

    font1.txt  font2.txt  font3.txt  font4.txt  font.tar  lost+found  test.sh

    [root@cloucentos6 home]# tar -xf font.tar -C /mnt

    [root@cloucentos6 home]# ls /mnt/

    font1.txt  font2.txt  font3.txt  font4.txt

    [root@cloucentos6 home]# tar -tvf font.tar

    -rw-r--r-- root/root       884 2017-04-29 18:27 font1.txt

    -rw-r--r-- root/root       884 2017-04-29 18:27 font2.txt

    -rw-r--r-- root/root       884 2017-04-29 18:27 font3.txt

    -rw-r--r-- root/root       884 2017-04-29 18:27 font4.txt

    [root@cloucentos6 home]# tar -xvf font.tar font2.txt

    font2.txt

    范例4两个tar归档文件进行合并

    [root@cloucentos6 home]# tar --help | grep "-A\,"

      -A, --catenate, --concatenate   追加 tar 文件至归档

    [root@cloucentos6 home]# tar -cf test01.tar font1.txt font2.txt

    [root@cloucentos6 home]# tar -cf test02.tar font3.txt font4.txt

    [root@cloucentos6 home]# tar -Af test01.tar test02.tar    #把test02.tar文件合并到test01.tar

    范例5从归档文件中删除文件

    [root@cloucentos6 home]# tar --help | grep "-delete"

      --delete               从归档(非磁带!)中删除

    [root@cloucentos6 home]# tar -tf test01.tar

    font1.txt

    font2.txt

    font3.txt

    font4.txt

    [root@cloucentos6 home]# tar --delete --file test01.tar font2.txt

    [root@cloucentos6 home]# tar -tf test01.tar

    font1.txt

    font3.txt

    font4.txt

    范例6tar 命令把文件进行归档和压缩,-z对.gz压缩格式,-j对bz2压缩格式

    [root@cloucentos6 home]# tar -zcvf file.tar.gz font1.txt font3.txt font4.txt

    font1.txt

    font3.txt

    font4.txt

    [root@cloucentos6 home]# ls -l | head -n 3

    -rw-r--r--. 1 root root   589  4月 29 22:59 file.tar.gz

    -rw-r--r--. 1 root root    45  4月 29 22:56 font1.txt

     

    范例7tar命令解压归档文件

    [root@cloucentos6 home]# tar -zxvf  file.tar.gz

    font1.txt

    font3.txt

    font4.txt

    8.2gzip压缩与gunzip解压

    gzip只能够压缩单个文件,而无法对目录和多个文件进行归档,因些需要tar进行归档后再使用gzip进行压缩,生成压缩格式后缀为.gz

    范例1gzip默认会删除原文件生成一个压缩文件test01.tar.gz

    [root@cloucentos6 home]# gzip font1.txt

    [root@cloucentos6 home]# ls -l

    -rw-r--r--. 1 root root   401  4月 29 18:27 font1.txt.gz

    -rw-r--r--. 1 root root   884  4月 29 18:27 font3.txt

    范例2gunzip解压会删除原压缩文件解压出文件

    [root@cloucentos6 home]# gunzip font1.txt.gz

    [root@cloucentos6 home]# ls -l font1.txt

    -rw-r--r--. 1 root root 884  4月 29 18:27 font1.txt

    范例3gzip  -l 选项查看压缩文件属性信息

    [root@cloucentos6 home]# gzip -l font1.txt.gz

             compressed        uncompressed  ratio uncompressed_name

                    401                 884  58.7% font1.txt

    范例4将font4.txt内容输入,gzip打包输出重写向到test.gz

    [root@cloucentos6 home]# cat font4.txt | gzip -c > test.gz

    范例5先使用tar进行归档再进行gzip压缩

    [root@cloucentos6 home]# tar -cvf file.tar font3.txt font4.txt

    font3.txt

    font4.txt

    [root@cloucentos6 home]# gzip file.tar

    [root@cloucentos6 home]# ls -l file.tar.gz

    -rw-r--r--. 1 root root 515  4月 29 23:35 file.tar.gz

    范例6先gzip进行解压.gz再使用tar解压tar

    [root@cloucentos6 home]# ls -l file.tar.gz

    -rw-r--r--. 1 root root 515  4月 29 23:35 file.tar.gz

    [root@cloucentos6 home]# gunzip file.tar.gz

    [root@cloucentos6 home]# ls -l file.tar

    -rw-r--r--. 1 root root 10240  4月 29 23:35 file.tar

    [root@cloucentos6 home]# tar -xvf file.tar

    font3.txt

    font4.txt

    8.3bzip2压缩与bunzip2解压

    Bunzip2是另一种与bzip类似的压缩技术,压缩格式后缀.bz2

     

    范例1bzip2默认会删除原文件生成一个压缩文件font3.txt.bz2

    [root@cloucentos6 home]# bzip2 font3.txt

    [root@cloucentos6 home]# ls -l font3.txt.bz2

    -rw-r--r--. 1 root root 462  4月 29 18:27 font3.txt.bz2

    范例2bunzip2解压会删除原压缩文件解压出文件

    [root@cloucentos6 home]# bunzip2 font3.txt.bz2

    [root@cloucentos6 home]# ls -l font3.txt

    -rw-r--r--. 1 root root 884  4月 29 18:27 font3.txt

    范例3将font3.txt内容输入,bzip2打包输出重写向到test.bz2

    [root@cloucentos6 home]# cat font3.txt | bzip2 -c > test.bz2

    范例4先使用tar进行归档再进行bzip2压缩

    [root@cloucentos6 home]# tar -cvf file.tar font3.txt

    [root@cloucentos6 home]# ls -l file.tar

    -rw-r--r--. 1 root root 10240  4月 30 00:16 file.tar

    [root@cloucentos6 home]# bzip2 file.tar

    [root@cloucentos6 home]# ls -l file.tar.bz2

    -rw-r--r--. 1 root root 545  4月 30 00:16 file.tar.bz2

    范例5先bzip2进行解压.bz2再使用tar解压tar

    [root@cloucentos6 home]# ls -l file.tar.bz2

    -rw-r--r--. 1 root root 545  4月 30 00:16 file.tar.bz2

    [root@cloucentos6 home]# bunzip2 file.tar.bz2

    [root@cloucentos6 home]# ls -l file.tar

    -rw-r--r--. 1 root root 10240  4月 30 00:16 file.tar

    [root@cloucentos6 home]# tar -xvf file.tar

    font3.txt

    8.4zip归档和压缩

    zip作为一种流行的压缩格式,在gzip或bzip2使用没那么广泛,但在因特网上的文件通常都采用这种格式。

    范例1zip压缩不会删除原文件, -r 选项对目录和文件进行递归压缩。

    [root@cloucentos6 home]# zip file.zip font3.txt

      adding: font3.txt (deflated 58%)

    [root@cloucentos6 home]# zip -r file.zip file/

      adding: file/ (stored 0%)

      adding: file/file.zip (stored 0%)

      adding: file/abc/ (stored 0%)

      adding: file/abc/file.zip (stored 0%)

    [root@cloucentos6 home]# ls -l file.zip

    -rw-r--r--. 1 root root 1704  4月 30 00:46 file.zip

    范例2unzip从zip文件中提取内容,完成之后不会删除zip压缩

    [root@cloucentos6 home]#unzip file.zip

    Archive:  file.zip

       creating: file/

     extracting: file/file.zip          

       creating: file/abc/

     extracting: file/abc/file.zip

    范例3 -u 选项更新压缩归档文件内容, -d 删除压缩归档文件中的删除内容

    [root@cloucentos6 home]# zip file.zip -u font4.txt    #更新

      adding: font4.txt (deflated 58%)

    [root@cloucentos6 mnt]# zip -d file.zip /file/abc/file.zip #删除指定压缩归档文件目录下的文件

    deleting: file/abc/file.zip

    8.5、加密工具与散列

    范例1gpg加密和解密文本,它采用密钥签名技术保护文件内容。

    [root@cloucentos6 home]# gpg -c font4.txt     #提示输入加密密码

    [root@cloucentos6 home]# ls -l font4.txt.gpg

    -rw-r--r--. 1 root root 429  4月 30 01:36 font4.txt.gpg

    [root@cloucentos6 home]# gpg font4.txt.gpg    #输入解密密码进行解密

    gpg: 3DES 加密过的数据

    can't connect to `/root/.gnupg/S.gpg-agent': 没有那个文件或目录

    agpg: 以 1 个密码加密

    gpg: 警告:报文未受到完整的保护

    范例2base64是一组类似编码方案,它通过将ASCII字符转换成以64为基数的形式来用ASCII字符串描述二进制数据。Base64可以用来对编码和解码base64字符串。

    [root@cloucentos6 home]# cat file.txt

    abc123

    abc123

    abc123

    [root@cloucentos6 home]# base64 file.txt > outputfile.txt   #加密生成outputfile.txt加密文件

    [root@cloucentos6 home]# cat outputfile.txt

    YWJjMTIzCmFiYzEyMwphYmMxMjMK

    [root@cloucentos6 home]# base64 -d outputfile.txt > jiemifile.txt #解密outputfile.txt生成jimifile.txt解密文件

    [root@cloucentos6 home]# cat jiemifile.txt

    abc123

    abc123

    abc123

    8.6raync备份系统快照

    rsync 可以对位于不同位置的文件和目录进行备份,它可以借助差异计算以及压缩技术来最小化数据传输量。rsync还还支持网络数据传输,rsync还会比较源端和目的端的文件,只有文件有更新时才进行复制。

    范例1将源目录复制到目的端 ,  -a 选项归档, -v 显示细节信息或进度

    标注:路径格式而言,如果/bin末尾使用/,rsync会将/bin/目录中的所有文件复制至目的端。如果/bin没有使用 / ,rsync会将/bin目录本身复制至目的端。

    [root@cloucentos6 home]# rsync -av  /bin/  /home/file  #将/bin/目录中的文件复制到/home/file目录下

    [root@cloucentos6 home]# rsync -av  /bin  /home/  #将/bin目录复制到/home/目录下

    [root@cloucentos6 home]# rsync  –av  /bin/  root@192.168.10.10:/home/file  #将/bin/目录复制到远程主机/home/file目录下

    范例2--exclude 排除不在归档范围内的部分文件

    [root@cloucentos6 home]# rsync -av /bin /home/ --exclude “*.txt”

    8.7dd命令克隆磁盘

      dd命令是同硬盘和分区打交道时,我们可能需要创建所有分区的副本或备份,而不仅仅是复制内容(不仅是各个硬盘分区,而且还包括引志记录、分区表等信息)。这时,我们就可以使用dd命令,它能够用于克隆任何类型的磁盘,如果硬盘、闪存、CD、DVD等。

    dd格式如下:

    dd  if=source  of=target  bs=block_size  count=count

    其中:

    if  代表输入文件或输入设备路径

    of  代表目标文件或目标设备路径

    bs  代表块大小(通常以2的幂数形式给出,如果512 、1024、2048等)。Count是需要复制的块数。

    需要复制的字节总数=块大小 * count

    通过指定count,我们可以限制从输入文件复制至目标的字节数。如果不指定count,dd命令会对输入文件进行复制,直到遇见文件结束标志(EOF)为止。

    范例1备份sda1分区,使用备份恢复分区。

    [root@cloucentos6 home]# df -h

    文件系统               容量  已用  可用 已用%% 挂载点

    /dev/sda2              97G  4.5G   87G   5% /

    tmpfs                 996M  112K  996M   1% /dev/shm

    /dev/sda1             485M   30M  430M   7% /boot

    /dev/sda3              58G  188M   55G   1% /home

    /dev/sr0              4.0G  4.0G     0 100% /media/CentOS_6.0_Final

    [root@cloucentos6 home]# dd if=/dev/sda1 of=/home/sda1_boot.img   #备份分区

    记录了1024000+0 的读入

    记录了1024000+0 的写出

    524288000字节(524 MB)已复制,35.5123 秒,14.8 MB/秒

    [root@cloucentos6 home]# ls -l sda1_boot.img

    -rw-r--r--. 1 root root 524288000  5月  3 17:31 sda1_boot.img

    [root@cloucentos6 home]# dd if=/home/sda1_boot.img of=/dev/sda1   #恢复分区

    记录了1024000+0 的读入

    记录了1024000+0 的写出

    524288000字节(524 MB)已复制,9.14704 秒,57.3 MB/秒

    范例2永久删除一个分区中所有数据,使用dd向该分区中写入0傎,/dev/zero是一个字符设备,它总是会返回字符 ’’

    [root@cloucentos6 home]# dd if=/dev/zero of=/dev/sda1

    范例3在容量相同的硬盘间进行克隆,/dev/sda是原数据端,/dev/sdb目的数据端。

    [root@cloucentos6 home]# dd if=/dev/sda of=/dev/sdb

    [root@cloucentos6 home]# dd if=/dev/cdrom of=cdrom.iso   #制作CD ROM的镜像(ISO文件)

  • 相关阅读:
    【LeetCode】048. Rotate Image
    【LeetCode】036. Valid Sudoku
    【LeetCode】060. Permutation Sequence
    【LeetCode】001. Two Sum
    【LeetCode】128. Longest Consecutive Sequence
    【LeetCode】081. Search in Rotated Sorted Array II
    【LeetCode】033. Search in Rotated Sorted Array
    顺时针打印矩阵
    矩形覆盖
    二维数组中的查找
  • 原文地址:https://www.cnblogs.com/zoulongbin/p/6890982.html
Copyright © 2011-2022 走看看