第9章 文件与文件系统的压缩与打包

压缩文件的用途与技术

例如,计算机都是以byte单位来计量的,1byte占8bit。如果存储数字1,那么1byte就会空出7bit。采用一定的计算方式,压缩这些空间可以大大降低文件存储。

Linux系统常见的压缩命令

Linux下压缩文件的扩展名有*.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2

扩展名用来说明压缩所采用的指令类型

  • *.Z  compress程序压缩的文件
  • *.gz  gzip程序压缩的文件
  • *.bz2  bzip2程序压缩的文件
  • *.tar  tar打包的数据,未压缩
  • *.tar.gz  tar打包的数据,经gzip压缩
  • *.tar.bz2  tar打包的数据,经bzip2压缩

Compress

非常旧的指令,可以忽视,gzip可以解压其压缩的*.Z文件

gzip,zcat

gzip可以解压compress,zip,gzip压缩的文件

参数:

-c:将压缩数据输出到屏幕

-d:解压缩的参数

-t:检验压缩文件的一致性

-v:显示压缩比

-#:压缩等级,-1最快但压缩比最低,-9最慢但压缩比最好,默认-6

[root@localhost tmp]# ls -l ./auto.smb
-rwxr-xr-x. 1 root root 687 6月  29 21:37 ./auto.smb
//压缩 [root@localhost tmp]#
gzip -v ./auto.smb ./auto.smb: 37.1% -- replaced with ./auto.smb.gz [root@localhost tmp]# ls -l ./auto.smb.gz -rwxr-xr-x. 1 root root 459 6月 29 21:37 ./auto.smb.gz
//解压缩
[root@localhost tmp]# gzip -d ./auto.smb.g

zcat指令可以读取纯文本被压缩后的压缩文件

[root@localhost tmp]# zcat ./auto.smb.gz
#!/bin/bash

# This file must be executable to work! chmod 755!

key="$1"
opts="-fstype=cifs"

for P in /bin /sbin /usr/bin /usr/sbin
do
    if [ -x $P/smbclient ]
    then
        SMBCLIENT=$P/smbclient
        break
    fi
done

[ -x $SMBCLIENT ] || exit 1

$SMBCLIENT -gNL $key 2>/dev/null| awk -v key="$key" -v opts="$opts" -F'|' -- '
    BEGIN    { ORS=""; first=1 }
    /Disk/    {
          if (first)
            print opts; first=0
          dir = $2
          loc = $2
          # Enclose mount dir and location in quotes
          # Double quote "$" in location as it is special
          gsub(/\$$/, "\\$", loc);
          gsub(/\&/,"\\\\&",loc)
          print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
        }
    END     { if (!first) print "\n"; else exit 1 }
    '

[root@localhost tmp]# 

 

bzip2,bzcat

参数:

-c:将压缩过程的数据显示到屏幕

-d:解压缩的参数

-k:保留源文件

-z:压缩的参数

-v:显示压缩比信息

-#:压缩比,同gzip

[root@localhost tmp]# ls -l ./auto.smb
-rwxr-xr-x. 1 root root 687 6月  29 21:37 ./auto.smb
[root@localhost tmp]# bzip2 -kzv ./auto.smb
  ./auto.smb:  1.314:1,  6.090 bits/byte, 23.87% saved, 687 in, 523 out.
[root@localhost tmp]# ls -l ./auto.smb.bz2
-rwxr-xr-x. 1 root root 523 6月  29 21:37 ./auto.smb.bz2

 

使用bzcat可以查看压缩后的文件内容

打包命令:tar

将多个文件打包成一个文件,便于多个文件的压缩

tar

tar [-j|-z] [cv] [-f 新建的文件名] filename . .. //打包与压缩

tar [-j|-z] [tv] [-f 新建的文件名]   //查看文件名

tar [-j|-z] [xv] [-f 新建的文件名]

常用的指令方式如下:

压缩:tar -jcv -f filename.tar.bz2

查询:tar -jtv -f filename.tar.bz2

解压:tar -jxv -f filename.tar.bz2 -C 欲解压的目录

[root@localhost hsperfdata_wuchao]# cd /tmp
[root@localhost tmp]# mkdir ./tartest
[root@localhost tmp]# cd ./tartest
[root@localhost tartest]# cp /var/spool/mail/root .

[root@localhost tartest]# ls -l root
-rw-------. 1 root root 571540 6月  30 10:40 root
[root@localhost tartest]# cp /var/spool/mail/wuchao .

[root@localhost tartest]# ls -l ./wuchao
-rw-r-----. 1 root root 0 6月  30 10:41 ./wuchao

//压缩 [root@localhost tartest]#
tar -jcv -f tartest.tar.bz2 ./root ./wuchao ./root ./wuchao

//查看压缩文件 [root@localhost tartest]#
tar -jtv -f tartest.tar.bz2 -rw------- root/root 571540 2016-06-30 10:40 ./root -rw-r----- root/root 0 2016-06-30 10:41 ./wuchao

//解压 [root@localhost tartest]#
tar -jxv -f tartest.tar.bz2 . ./root ./wuchao

//解压到特定目录
[root@localhost tartest]# mkdir ./t
[root@localhost tartest]# tar -jxv -f ./tartest.tar.bz2 -C ./t

 完整备份工具:dump

dump

用于对文件系统的备份,对某个文件系统进行dump备份后,第二次dump时可以指定备份等级,等级0表示完整备份,等级大于0表示新备份的数据只会记录与上一次较低等级相比新增或更改的文件。

使用限制:

  1. 当备份的数据为单一文件系统:可以使用完整的dump功能,同时备份时可以使用挂载点或设备文件名来进行备份。
  2. 当备份的数据只是目录,并非单一的文件系统:比如要备份/home/someone,但该目录并非单一的文件系统,此时的备份数据都要在该目录下,且只能使用level0,不支持-u参数,即无法创建/etc/dumpdates这个level备份的时间记录文件。

dump [-Suv] [-level] [-f 备份文件]  待备份数据

dump -W

参数:

-S:列出后面待备份数据需要的磁盘空间

-u:将本次dump的时间记录到/etc/dumpdates文件中

-v:将dump的文件过程显示出来

-j:加入bzip2的支持,将数据进行压缩

-level:dump等级

-f:备份后的文件

-W:列出/etc/fstab里具有dump设置的分区是否备份过

用dump备份完整的文件系统

//查看文件系统
[root@localhost tartest]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 46G 5.8G 40G 13% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 656K 1.9G 1% /dev/shm tmpfs 1.9G 17M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/mapper/centos-home 23G 92M 23G 1% /home /dev/sda5 497M 213M 284M 43% /boot tmpfs 373M 28K 373M 1% /run/user/0 /dev/sda3 976M 2.6M 907M 1% /run/media/root/MyFile

//查看备份/dev/sda3需要的空间 [root@localhost tartest]# dump
-S /dev/sda3 53248

//进行备份,备份到/root/boot.dump文件,/run/media/root/MyFile也可以写成/dev/sda3 [root@localhost tartest]# dump
-0u -f /root/boot.dump /run/media/root/MyFile DUMP: Date of this level 0 dump: Thu Jun 30 15:42:53 2016 DUMP: Dumping /dev/sda3 (/run/media/root/MyFile) to /root/boot.dump DUMP: Label: MyFile DUMP: Writing 10 Kilobyte records DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 52 blocks. DUMP: Volume 1 started with block 1 at: Thu Jun 30 15:42:53 2016 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /root/boot.dump DUMP: Volume 1 completed at: Thu Jun 30 15:42:53 2016 DUMP: Volume 1 40 blocks (0.04MB) DUMP: 40 blocks (0.04MB) on 1 volume(s) DUMP: finished in less than a second DUMP: Date of this level 0 dump: Thu Jun 30 15:42:53 2016 DUMP: Date this dump completed: Thu Jun 30 15:42:53 2016 DUMP: Average transfer rate: 0 kB/s DUMP: DUMP IS DONE
//查看备份文件 [root@localhost tartest]#
ls -dl /run/media/root/MyFile /root/boot.dump -rw-r--r--. 1 root root 40960 6月 30 15:42 /root/boot.dump drwx------. 5 root root 4096 6月 30 15:38 /run/media/root/MyFile //查看更新记录 [root@localhost tartest]# cat /etc/dumpdates /dev/sda3 0 Thu Jun 30 15:42:53 2016 +0800

//查看有没有任何文件系统被备份过
Last dump(s) done (Dump '>' file systems):
  /dev/sda3    (/run/media/root/MyFile) Last dump: Level 0, Date Thu Jun 30 15:42:53 2016

备份非文件系统,即单一目录的方法:

//将目录/run/media/root/MyFile/folder1备份
[root@localhost tartest]# dump -0j -f /root/fw.dump /run/media/root/MyFile/folder1 DUMP: Date of this level 0 dump: Thu Jun 30 15:59:17 2016 DUMP: Dumping /dev/sda3 (/run/media/root/MyFile (dir /folder1)) to /root/fw.dump DUMP: Label: MyFile DUMP: Writing 10 Kilobyte records DUMP: Compressing output at compression level 2 (bzlib) DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 46 blocks. DUMP: Volume 1 started with block 1 at: Thu Jun 30 15:59:17 2016 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /root/fw.dump DUMP: Volume 1 completed at: Thu Jun 30 15:59:17 2016 DUMP: 40 blocks (0.04MB) on 1 volume(s) DUMP: finished in less than a second DUMP: Date of this level 0 dump: Thu Jun 30 15:59:17 2016 DUMP: Date this dump completed: Thu Jun 30 15:59:17 2016 DUMP: Average transfer rate: 0 kB/s DUMP: Wrote 40kB uncompressed, 11kB compressed, 3.637:1 DUMP: DUMP IS DONE [root@localhost tartest]#

restore

备份后的恢复

restore -t [-f dumpfile] [-h]  用来查看dump文件

restore -C [-f dumpfile] [-D 挂载点]  用来比较dump文件与实际文件

restore -i [-f dumpfile]  进入互动模式

restore -r [-f dumpfile]  还原整个文件系统

-h:查看完整备份数据的inode和文件系统的label信息


用restore查看dump后的备份数据内容

[root@localhost tartest]# restore -t -f /root/boot.dump
Dump   date: Thu Jun 30 15:42:53 2016
Dumped from: the epoch
Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3
Label: MyFile
         2    .
        11    ./lost+found
        12    ./Untitled Folder
      8193    ./Untitled Folder 2
        13    ./Untitled Folder 2/Untitled Folder
[root@localhost tartest]#

比较差异并还原整个系统

//移除原文件系统的一个文件夹
[root@localhost /]# rm -rf /run/media/root/MyFile/folder1

//查看文件系统和备份文件之间的差异
[root@localhost /]# restore -C -f /root/boot.dump
Dump   date: Thu Jun 30 15:42:53 2016
Dumped from: the epoch
Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3
Label: MyFile
filesys = /run/media/root/MyFile
restore: unable to stat ./Untitled Folder: No such file or directory
Some files were modified!  1 compare errors
[root@localhost /]# 

还原文件系统:首先创建新的文件系统

[root@localhost /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

.
.
. Device Boot Start End Blocks Id System
/dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT /dev/sda2 206848 403138559 201465856 7 HPFS/NTFS/exFAT /dev/sda3 403138560 405235711 1048576 83 Linux /dev/sda4 473042944 625141759 76049408 5 Extended /dev/sda5 473044992 474068991 512000 83 Linux /dev/sda6 474071040 625141759 75535360 8e Linux LVM Command (m for help): d Partition number (1-6, default 6): 2 Partition 2 is deleted Command (m for help): p
.
.
.
Device Boot Start End Blocks Id System /dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT /dev/sda3 403138560 405235711 1048576 83 Linux /dev/sda4 473042944 625141759 76049408 5 Extended /dev/sda5 473044992 474068991 512000 83 Linux /dev/sda6 474071040 625141759 75535360 8e Linux LVM Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): p Selected partition 2 First sector (206848-625142447, default 206848): Using default value 206848 Last sector, +sectors or +size{K,M,G} (206848-403138559, default 403138559): +2014M Partition 2 of type Linux and of size 2 GiB is set .
.
.
Device Boot Start End Blocks Id System /dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT /dev/sda2 206848 4331519 2062336 83 Linux /dev/sda3 403138560 405235711 1048576 83 Linux /dev/sda4 473042944 625141759 76049408 5 Extended /dev/sda5 473044992 474068991 512000 83 Linux /dev/sda6 474071040 625141759 75535360 8e Linux LVM Command (m for help): w The partition table has been altered! [root@localhost /]# partprobe [root@localhost /]# mkfs -t ext3 /dev/sda2 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 129024 inodes, 515584 blocks 25779 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=528482304 16 block groups 32768 blocks per group, 32768 fragments per group 8064 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost /]# mount /dev/sda2 /mnt

cd到新建的文件系统的挂载点

//cd到新建的文件系统的挂载点(重要)
[root@localhost /]# cd /mnt
//将备份文件还原到当前文件系统 [root@localhost mnt]# restore
-r -f /root/fw.dump Dump tape is compressed. ./lost+found: (inode 11) not found on tape ./Untitled Folder 2: (inode 8193) not found on tape [root@localhost mnt]#

仅还原部分文件的互动模式

//查看备份文件
[root@localhost mnt]# restore -t -f /root/boot.dump Dump date: Thu Jun 30 15:42:53 2016 Dumped from: the epoch Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3 Label: MyFile 2 . 11 ./lost+found 12 ./Untitled Folder 8193 ./Untitled Folder 2 13 ./Untitled Folder 2/Untitled Folder

//进入restore互动模式,此时的所有操作都在备份文件内部操作 [root@localhost mnt]# restore
-i -f /root/boot.dump
//输入help 查看帮助 restore
> help Available commands are: ls [arg] - list directory cd arg - change directory pwd - print current directory add [arg] - add `arg' to list of files to be extracted delete [arg] - delete `arg' from list of files to be extracted extract - extract requested files setmodes - set modes of requested directories quit - immediately exit program what - list dump header information verbose - toggle verbose flag (useful with ``ls'') prompt - toggle the prompt display help or `?' - print this list If no `arg' is supplied, the current directory is used
//查看备份文件列表
restore > ls .: Untitled Folder/ Untitled Folder 2/ lost+found/
//进入到文件夹Untitled Folder 2 restore
> cd 'Untitled Folder 2'
//查看但前目录名
restore > pwd /Untitled Folder 2

//查看当前目录下的文件列表
restore > ls 'Untitled Folder' ./Untitled Folder 2/Untitled Folder:
//将Untitled Folder目录添加到解压缩列表 restore
> add 'Untitled Folder'

//查看当前目录,要被解压的文件前面会有一个*号
restore > ls ./Untitled Folder 2: *Untitled Folder/
//解压 restore
> extract You have not read any volumes yet. Unless you know which volume your file(s) are on you should start with the last volume and work towards the first. Specify next volume # (none if no more volumes): 1 set owner/mode for '.'? [yn] y restore > q
//查看解压后的文件 [root@localhost mnt]#
ls 'Untitled Folder 2' Untitled Folder [root@localhost mnt]#

 

光盘写入功能

光盘很少用,手头没有可供刻录的光盘,暂且不学。

其他的压缩与备份工具

暂且跳过

 

 

 

 

 

 

 

 

posted @ 2016-06-30 17:15  且听风吟-wuchao  阅读(522)  评论(0编辑  收藏  举报