linux删除被打包压缩的原始文件在哪,linux的压缩、打包和备份还原工具

linux的压缩、打包和备份还原工具

linux系统支持的压缩命令非常过,不同的压缩命令使用的压缩技术是不同的,所以彼此之间就无法相互压缩或解压缩了。

l

linux系统中常见的压缩文件的扩展名

1、*z:

compress(已经不再流行了)程序压缩的文件

2、*.gz:

gzip程序压缩的文件

3、*.bz2:

bzip2程序压缩的文件

4、*.tar:

tar程序打包的数据,并没有压缩过

5、*.tar.gz: tar程序打包的文件,经过gzip的压缩

6、*.tar.bz2: tar程序打包的文件,经过bzip2的压缩

目前比较流行的压缩命令是gzip和bzip2,其中bzip2的压缩比更好,不过这两个命令只能针对一个文件夹进行压缩和解压缩。tar只能对多个文件进行打包,并没有压缩的功能(也可以对目录进行打包),所以很多时候是将tar命令与压缩命令gzip和bzip2结合使用。

l

linux中的压缩命令gzip与读取压缩文件内容命令zcat

gzip可以解开conmpress、zip和gzip的文件,其主要参数为:

-c:

将压缩的数据输出到屏幕上

-d: 解压缩的参数

-t:

检查压缩文件的一致性,看是不是有错误

-v:

可以显示压缩比

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

[root@baobao ~]# gzip -v

/guo/baobao

//压缩

/guo/baobao:

57.1% -- replaced with /guo/baobao.gz

原来的文件会被压缩为.gz的文件,源文件就不存在了,而且我们可以使用命令zcat查看该压缩文件的内容。此外gzip压缩的文件在windows下是可以用winrar解压缩的。

[root@baobao ~]# zcat

/guo/baobao.gz

//查看被压缩的文件内容

[root@baobao ~]# gzip -d

/guo/baobao.gz

//解压缩

l

linux中的压缩命令bzip2与读取压缩文件内容命令bzcat

这个命令可以解压名为.bz;.bz2;.tbz;.tbz2的压缩文件,其参数介绍:

-c:

将压缩的数据输出到屏幕上

-d: 解压缩的参数

-k:

保留不删除源文件

-z:

压缩的参数

-v:

可以显示压缩比

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

[root@baobao ~]# bzip2 -z /guo/guo

[root@baobao ~]# bzcat /guo/guo.bz2

[root@baobao ~]# bzip2 -d /guo/guo.bz2

l

linux中的打包命令tar

可以将多个文件或目录打包成一个大文件,然后可以通过gzip或bzip2将该大文件进行压缩。在备份领域主要备份关键性数据

1、主要参数介绍:

-c: 新建打包文件

-t: 查看打包文件中包含哪些文件名

-x: 解压缩或解打包,注意-c、-t、-x不能同时出现在一串命令当中

-j: 通过bzip2进行压缩或解压缩,此时的文件名最好为:*.tar.bz2

-z:

通过gzip进行压缩或解压缩,此时文件名最好为:*.tar.gz

-v: 在压缩或解压缩过程当中将正在处理的文件名显示出来

-f: 后面要接被处理的文件名,最好单独写一个参数

-C: 在特定目录解压缩(大写)

-P(大写): 保留绝对路径,也就是允许备份数据中含有根目录存在,不建议使用

-p:

保留备份数据的原始权限和属性(常用)

--exclude=文件1:

在压缩过程中不要讲文件1打包

2、命令tar经常用到的组合(只要记住这几个基本上就可以了)

1)

压缩:tar –jcv –f 文件名.tar.bz2 要被压缩的文件或目录

2)

查询:tar –jtv –f 文件名.tar.bz2

3)

解压缩:tar –jxv –f 文件名.tar.bz2 –C 要解压缩的目录

3、使用tar备份/etc目录

[root@baobao~]# tar -zpcv -f /root/etc.tar.gz

/etc

//“/root/etc.tar.gz被压缩到的位置及文件名称;/etc是被压缩的文件或目录”

[root@baobao ~]# tar -jpcv -f

/root/etc.tar.bz2/etc

[root@baobao ~]# ll

/root/etc*

-rw-r--r--. 1 root root 7969314

6月5 10:14 /root/etc.tar.bz2

-rw-r--r--. 1 root root 9250361

6月5 10:11 /root/etc.tar.gz

//注意:

1、通过比较可以看出压缩比,故建议用-j

2、这里的7969314和9250361分别代表7.9M和9.2M,要除以103x103

3、在备份的时候最好使用-p参数,连同文件的权限和属性一起备份

4、查看文件名以及权限和属性

[root@baobao ~]# tar -jtv -f

/root/etc.tar.bz2| more

tar: 从成员名中删除开头的“/”//这个警告是说去掉根目录,是安全的

drwxr-xr-x

root/root

0 2014-06-05 08:30 /etc/

-rw-rw-rw-

root/root

1293 2014-05-28 09:55 /etc/shadow-

-rw-rw-rw-

root/root

6300 2010-08-23 07:28 /etc/pnm2ppa.conf

-rw-rw-rw-

root/root

4922 2011-06-25 19:15 /etc/oddjobd.conf

5、将打包文件在当前目录解压缩

[root@baobao ~]# tar -jxv -f

/root/etc.tar.bz2

[root@baobao ~]#ll

drwxr-xr-x. 120 root root 12288

6月5 08:30 etc

[root@baobao ~]# rm -rf

/root/etc

//记得将这个目录删除

6、将打包文件在特定目录解压

[root@baobao ~]# tar -jxv -f /root/etc.tar.bz2

-C/tmp

[root@baobao ~]# ll /tmp

drwxr-xr-x. 120 root root 12288

6月5 08:30 etc

[root@baobao ~]# rm -rf

/tmp/etc

//记得将这个目录删除

//注意:“rm

-rf”是个很危险的命令,切记要确认好要删除的项目,否则系统会死掉的。

7、解压打包文件内的特定文件

1)先找到想要解压的那个文件名

[root@baobao~]# tar -jtv -f /root/etc.tar.bz2 |

grep "shadow"

-rw-rw-rw-root/root

1293 2014-05-28 09:55/etc/shadow-

-rw-rw-rw-root/root

1318 2014-05-30 16:15 /etc/shadow

-rw-rw-rw-root/root

808 2014-05-30 16:15/etc/gshadow

-rw-rw-rw-root/root

794 2014-05-28 09:55/etc/gshadow-

2)解压该文件

[root@baobao~]# tar -jxv -f /root/etc.tar.bz2

etc/shadow

etc/shadow

[root@baobao~]# ll etc

//注意:“etc/shadow”一定不要写成“/etc/shadow”,因为它是文件名

8、将/home目录(除aguo文件、abao目录外)压缩到/guo中

[root@baobao~]# tar -jcv -f

/guo/baobao.tar.bz2 --exclude=/home/abao*

--exclude=/home/aguo*/home

9、备份比某个时刻还要新的文件(差异备份)

1)找出比/etc/passwd还要新的文件

[root@baobao~]# find /etc/ -newer

/etc/passwd

[root@baobao~]# ll /etc/passwd

-rw-rw-rw-.1 root root 2121 5月30 16:15

/etc/passwd

2)将比/etc/passwd还要新的文件备份到/guo中,名为为etcnewer.tar.bz2

[root@baobao~]# tar -jcv -f

/guo/etcnewer.tar.bz2 --newer-mtime ="2014/05/30"/etc/*

/etc/selinux/targeted/modules/active/modules/ktalk.pp

/etc/selinux/targeted/modules/active/modules/ccs.pp

/etc/selinux/targeted/modules/active/modules/glance.pp

tar:/etc/selinux/targeted/setrans.conf:

文件未改变;未输出

tar:/etc/services: 文件未改变;未输出

tar:/etc/sestatus.conf: 文件未改变;未输出

/etc/setroubleshoot/

tar:/etc/setroubleshoot/setroubleshoot.conf:

文件未改变;未输出

//注意:

a、mtime:当文件的内容更改时(不是文件的属性或权限),更新这个时间。默认的ls显示出来的时间就是这个时间。

b、ctime:当文件的权限或属性更改时,更新这个时间。

c、atime:当文件的内容被取用时(例如读取),更新这个时间

10、将/etc整个目录一边打包一边在/tmp中解开

[root@baobaotmp]# tar -cvf - /etc |

tar-xvf -

习惯上我们把只打包不压缩的文件称为tarfile;而将打包并压缩后的文件称为tarball

11、将系统重要数据备份到/backups

[root@baobao ~]# mkdir /backups

[root@baobao ~]# chmod 700 /backups

[root@baobao ~]# ll -d /backups

drwx------. 2 root root 4096 6月5 14:49 /backups

[root@baobao~]# tar -jcv -f

/backups/wodebeifen-20140605.tar.bz2 /etc /boot /home

/root/usr/local /var

[root@baobao ~]# ll -h /backups

总用量 172M

-rw-r--r--. 1 root root 172M 6月5 14:56 wodebeifen-20140605.tar.bz2

l

完整备份工具dump

dump主要针对文件系统进行备份,此外也能够针对目录进行备份(对目录的支持有所

不足),此外还可以指定等级(等级范围是:0-9,其中等级0表示完整备份),例如第一次备份的时候使用的是level0,第二次备份的时候使用的是level1,level1与level0进行比较后只备份有差异的数据。

1、dump的主要参数介绍

-S:

列出后面的待备份数据需要多大的磁盘空间才能备份完毕

-u: 将这次备份的时间记录到/etc/dumpdateS文件中

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

-j:

使用bzip2将数据压缩,默认bzip2的压缩等级为

2

-level:

从0到9共10个等级

-f:

后面接产生的文件,可接设备文件名等

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

2、新增磁盘、分区和挂载

[root@baobao ~]# fdisk -l

Disk /dev/sdb: 21.5 GB, 21474836480

bytes

[root@baobao ~]# fdisk /dev/sdb

Command (m for help): p

DeviceBoot

Start

End

BlocksId

System

/dev/sdb1

1

6545253223+

83 Linux

/dev/sdb2655

1960

1049044583

Linux

Command (m for help): w

[root@baobao~]#parprobe

//刷新下,让磁盘划分立即生效(否则可能下面步骤看不到磁盘划分变化)

[root@baobao ~]# fdisk –l

[root@baobao ~]# mkfs.ext4

/dev/sdb1 //格式化分区

[root@baobao ~]# mkfs.ext4 /dev/sdb2

[root@baobao ~]# mkdir /guoshijituan

[root@baobao ~]# mkdir /guo

[root@baobao~]#

mount /dev/sdb1 /guo

[root@baobao ~]# mount /dev/sdb2

/guoshijituan

[root@baobao ~]# df

–k //查看挂载是否成功

文件系统1K-块

已用

可用 已用% 挂载点

/dev/mapper/VolGroup-lv_root

37051072

4579620

3058934014% /

tmpfs

957856

228957628

1% /dev/shm

/dev/sda1

495844

38045432199

9% /boot

/dev/sdb1

5170664

1413364766668

3% /guo

/dev/sdb2

10325780

1541369647124

2% /guoshijituan

[root@baobao ~]# vim

/etc/fstab

/dev/sdb1/guo

ext4

defaults,usrquota,grpquota

0 0

/dev/sdb1/guoshijituan

ext4

defaults,usrquota,grpquota

0 0

//加入quota的支持,如果仅仅是设置开机自动挂载的话“,usrquota,grpquota”quota支持部分就可以去掉

3、备份完整的文件系统

1)找出最小的文件系统

[root@baobao~]# df -h

文件系统

容量

已用 可用 已用%%

挂载点

/dev/mapper/VolGroup-lv_root

36G

4.4G30G

14% /

tmpfs

936M

224K936M

1% /dev/shm

/dev/sda1

485M

38M423M

9% /boot

/dev/sdb1

5.0G

139M4.6G

3% /guo

/dev/sdb2

9.9G

151M9.3G

2% /guoshijituan

2)测试下要备份的文件系统需要多大空间容量

[root@baobao ~]# dump -S /dev/sdb2

197632

//大约0.2M

3)完整备份(备份名为guoguo.dump)该文件系统,同时更新记录新文件

[root@baobao~]# dump -0u -f

/guo/guoguo.dump /guoshijituan

//注意:“0u”处是级别数字“0”;也可以写“/dev/sdb2”

DUMP:

Date of this level 0 dump: Mon Jun 9

11:07:01 2014

DUMP:

Dumping /dev/sdb2 (/guoshijituan) to/guo/guoguo.dump

DUMP:

Label: none

DUMP:

Writing 10 Kilobyte records

DUMP:

mapping (Pass I) [regular files]

DUMP:

mapping (Pass II) [directories]

DUMP:

estimated 193 blocks.

DUMP:

Volume 1 started with block 1 at:

MonJun 9 11:07:02 2014

DUMP:

dumping (Pass III) [directories]

DUMP:

dumping (Pass IV) [regular files]

DUMP:

Closing /guo/guoguo.dump

DUMP:

Volume 1 completed at: Mon Jun 9

11:07:02 2014

DUMP:

Volume 1 180 blocks (0.18MB)

DUMP:

180 blocks (0.18MB) on 1 volume(s)

DUMP:

finished in less than a second

DUMP:

Date of this level 0 dump: Mon Jun 9

11:07:01 2014

DUMP:

Date this dump completed: Mon

Jun9 11:07:02 2014

DUMP:

Average transfer rate: 0 kB/s

DUMP:

DUMP IS DONE

4)查看记录

[root@baobao~]# ll /guo/guoguo.dump

/etc/dumpdates

-rw-rw-r--.1 root

disk

43 6月9 11:07 /etc/dumpdates

-rw-r--r--.1 root root 184320

6月9

11:07 /guo/guoguo.dump

4、dump的差异备份

1)查看有无dump过

的文件系统数据

[root@baobao ~]# dump -W

Last dump(s) done (Dump '>' file

systems):

>

/dev/mapper/VolGroup-lv_root

(

/)Last dump: never

> /dev/sda1 (/boot) Last dump:

never

/dev/sdb1

( /guo)Last dump: never

/dev/sdb2 (/guoshijituan)Last dump: Level 0, Date Mon Jun 9

11:07:01 2014

2)在/guoshijituan下建立差异数据

3)新建差异备份文件

[root@baobao~]# dump -1u -f

/guo/guoguo.dump /guoshijituan

DUMP:

Date of this level 1 dump: Mon Jun 9

11:28:01 2014

DUMP:

Date of last level 0 dump: Mon Jun 9

11:07:01 2014

DUMP:

Dumping /dev/sdb2 (/guoshijituan) to/guo/guoguo.dump

DUMP:

Label: none

DUMP:

Writing 10 Kilobyte records

DUMP:

mapping (Pass I) [regular files]

DUMP:

mapping (Pass II) [directories]

DUMP:

estimated 184 blocks.

DUMP:

Volume 1 started with block 1 at:

MonJun 9 11:28:02 2014

DUMP:

dumping (Pass III) [directories]

DUMP:

dumping (Pass IV) [regular files]

DUMP:

Closing /guo/guoguo.dump

DUMP:

Volume 1 completed at: Mon Jun 9

11:28:02 2014

DUMP:

Volume 1 180 blocks (0.18MB)

DUMP:

180 blocks (0.18MB) on 1 volume(s)

DUMP:

finished in less than a second

DUMP:

Date of this level 1 dump: Mon Jun 9

11:28:01 2014

DUMP:

Date this dump completed: Mon

Jun9 11:28:02 2014

DUMP:

Average transfer rate: 0 kB/s

DUMP:

DUMP IS DONE

4)查看备份文件

[root@baobao~]# ll

/guo/guo*

-rw-r--r--.1 root root 184320

6月9

11:28 /guo/guoguo.dump

//要理解“差异文件的意思”,因为源文件没有变更所以大小不变

5)查看备份记录

[root@baobao~]# dump -W

Lastdump(s) done (Dump '>' file

systems):

>/dev/mapper/VolGroup-lv_root

(

/) Last dump: never

>/dev/sda1

( /boot) Last dump: never

/dev/sdb1

( /guo) Last dump: never

/dev/sdb2(/guoshijituan)

Last dump: Level 1,Date Mon Jun 9 11:28:01 2014

6)修改文件内容后再次备份

[root@baobao~]# dump -2u -f

/guo/guoguo.dump /guoshijituan

DUMP:

Date of this level 2 dump: Mon Jun 9

11:35:07 2014

DUMP:

Date of last level 1 dump: Mon Jun 9

11:28:01 2014

DUMP:

Dumping /dev/sdb2 (/guoshijituan) to/guo/guoguo.dump

DUMP:

Label: none

DUMP:

Writing 10 Kilobyte records

DUMP:

mapping (Pass I) [regular files]

DUMP:

mapping (Pass II) [directories]

DUMP:

estimated 188 blocks.

DUMP:

Volume 1 started with block 1 at:

MonJun 9 11:35:08 2014

DUMP:

dumping (Pass III) [directories]

DUMP:

dumping (Pass IV) [regular files]

DUMP:

Closing /guo/guoguo.dump

DUMP:

Volume 1 completed at: Mon Jun 9

11:35:08 2014

DUMP:

Volume 1 190 blocks (0.19MB)

DUMP:

190 blocks (0.19MB) on 1 volume(s)

DUMP:

finished in less than a second

DUMP:

Date of this level 2 dump: Mon Jun 9

11:35:07 2014

DUMP:

Date this dump completed: Mon

Jun9 11:35:08 2014

DUMP:

Average transfer rate: 0 kB/s

DUMP:

DUMP IS DONE

[root@baobao~]# ll

/guo/guo*

-rw-r--r--.1 root root 194560

6月9

11:35 /guo/guoguo.dump

[root@baobao~]# dump -W

Lastdump(s) done (Dump '>' file

systems):

>/dev/mapper/VolGroup-lv_root

(

/) Last dump: never

>/dev/sda1

( /boot) Last dump: never

/dev/sdb1

( /guo) Last dump: never

/dev/sdb2(/guoshijituan)

Last dump: Level 2,Date Mon Jun 9 11:35:07 2014

5、dump备份后的恢复命令restore

1)

restore命令的四种模式介绍

-t:

查看dump过的备份文件中含有什么重要数据

-C:

将dump内的数据跟实际文件系统做比较,将会列出dump文件内有记录的且与目前文件系统不一样的文件

-i:

进入互动模式,可以仅还原部分文件(dump目录还原)

-r:

整个文件系统还原模式,针对文件系统的dump备份

2)

restore命令的参数介绍

-h:

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

-f:

后面接要处理的dump文件

-D:

与-C搭配使用,查出后面接的挂载点与dump内有不同的文件

3)

查看dump后备份数据内容

[root@baobao ~]# restore -t -f

/guo/guoguo.dump

Dump

date:Mon Jun 9 11:35:07

2014

Dumped from: Mon

Jun9 11:28:01 2014

Level 2 dump of /guoshijituan

onbaobao.com:/dev/sdb2

Label: none

2 .

13 ./guobao~

16 ./guobao

4)

比较差异并还原整个文件系统

[root@baobao ~]# restore -C -f

/guo/guoguo.dump

Dump

date:Mon Jun 9 11:35:07

2014

Dumped from: Mon

Jun9 11:28:01 2014

Level 2 dump of /guoshijituan

onbaobao.com:/dev/sdb2

Label: none

filesys = /guoshijituan

./guobao~: tape and disk copies are

different

./guobao: tape and disk copies are

different

Some files were modified(修改的意思)! 2

compare errors

[root@baobao]# restore –r -f

/guo/guoguo.dump

//还原

l

压缩备份工具dd

主要备份整个分区或整个磁盘,经常会用到该工具

1、参数介绍

ddif=input file of=output file bs=block size

count=number

if:

就是input文件,也可以是设备

of: 就是output文件,也可以是设备

bs: 规划的一个块的大小,默认是512字节(一个扇区的大小)

count: 多少块的意思

2、文件的备份

[root@baobao~]# dd if=/guo/chengcheng

of=/guoshijituan/wo/baobao.back

//将/guo/chengcheng备份到/guoshijituan/wo,文件名baobao.back

记录了0+1 的读入

记录了0+1 的写出

17字节(17 B)已复制,4.3491e-05

秒,391 kB/秒

3、备份分区

[root@baobao~]# dd if=/dev/sdb2

of=/guo/beifen.back

记录了20343017+0 的读入

记录了20343016+0 的写出

10415624192字节(10 GB)已复制,267.516

秒,38.9 MB/秒

4、将备份还原

[root@baobao~]# dd if=/guo/beifen.back

of=/dev/sdb2

l

压缩备份工具cpio

cpio可以备份任何东西(包括设备和设备文件),不过需要配合find使用,它也是备份还原系统数据的重要工具

1、cpio的语法

1)备份用到的参数

-o:将数据复制输出到文件或设备上

-B:让默认的块可以增加到512字节(加快大文件的存储)

2)还原用到的参数

-i:

将数据从文件或设备上复制到系统中

-d:

自动新建目录,使用cpio备份的数据不一定在同一目录,所以要让cpio还原时可以新建目录

-u:

自动将较新的文件覆盖较旧的文件

-t:配合-i可以查看cpio新建的文件或设备的内容

3)共享参数

-v:存储过程显示文件名

-c:一种较新的方式存储

4)经常用到的参数组合

cpio –ovcB:

备份

cpio –ivcdu:

还原

cpio –ivct:

查看

2、cpio备份实例

1)找出/guo下的所有文件

[root@baobao ~]# find /guo -print

/guo

/guo/guoguo.dump

/guo/lost+found

2)进行备份与查看

[root@baobao ~]# find /guo |cpio -ocvB

>/bao/guo.cpio

/guo

/guo/guoguo.dump

/guo/lost+found

39 块

[root@baobao ~]# ll -h /bao/guo.cpio

-rw-r--r--. 1 root root 195K 6月10 09:19

/bao/guo.cpio

3)将刚刚备份的/bao/guo.cpio解压到原目录下

[root@baobao~]# cpio -idvc <

/bao/guo.cpio

/guo

/guo/guoguo.dump

/guo/lost+found

381 块

//cpio可以将系统的数据完整的备份到磁带机上如:

备份:find

/目录或文件 | cpio –ocvB > /dev/st0

还原:cpio –idvc <

/dev/st0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值