Linux基础_合并,归档,压缩,dump,编辑器

一,文件合并

# 使用cat命令来进行文件的纵向合并

>:覆盖目标文件中的内容

# 创建file1.txt
[root@localhost ~]# touch file1.txt
[root@localhost ~]# echo '88888888' > file1.txt 
[root@localhost ~]# cat file1.txt 
88888888

# 创建file2.txt
[root@localhost ~]# touch file2.txt
[root@localhost ~]# echo '99999999' > file2.txt 
[root@localhost ~]# cat file2.txt 
99999999

# 将file1.txt中的内容覆盖到file2.txt中
[root@localhost ~]# cat file1.txt > file2.txt 
[root@localhost ~]# cat file1.txt 
88888888
[root@localhost ~]# cat file2.txt 
88888888

>>:追加到目标文件的尾部

# 创建文件fule3.txt
[root@localhost ~]# touch file3.txt
[root@localhost ~]# echo '33333333' > file3.txt 
[root@localhost ~]# cat file3.txt 
33333333

# 创建文件file4.txt
[root@localhost ~]# touch file4.txt
[root@localhost ~]# echo '44444444' > file4.txt 
[root@localhost ~]# cat file4.txt 
44444444

# 将文件追加到目标文件的尾部
[root@localhost ~]# cat file3.txt  >> file4.txt 
[root@localhost ~]# cat file4.txt 
44444444
33333333

...

二,压缩

 将比较大的文件通过文件压缩技术后,可以将其的磁盘使用量降低。

文件压缩的原理:在计算机中最长使用的单位为字节byte,但是计算机中最小的单位为bit,1byte=8bit,当计算机中存储一个数字1时,就会以8个二进制位来进行存储,每个位就相当于一个空格,如果记录的是数字1,则其二进制表示位 0000 0001,其中有7个bit位为0,也就是为“空”,可以将其“空”的空间释放出来,这就是一种压缩技术;另一种比如你存储100个1时,在计算机上并不会真的存储100个1,而是会以记录的方式“100个1”进行存储

1:Linux种常见的压缩命令:*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2

2:用何种进行压缩也最好用何种进行解压缩

*.Z      # compress 程序压缩的档案
*.bz2    # bzip2 程序压缩的档案
*.gz     # gzip 程序压缩的档案
*.tar    # tar 程序打包的数据,并没有压缩过
*.tar.gz # tar 程序打包的档案,其中并且经过 gzip 的压缩

# compress

1:compress压缩命令的安装过程

  1 [root@localhost ~]# yum install ncompress
  2 已加载插件:fastestmirror, refresh-packagekit, security
  3 设置安装进程
  4 Loading mirror speeds from cached hostfile
  5 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os&infra=stock error was
  6 14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
  7 Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=x86_64 error was
  8 14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.fedoraproject.org'"
  9 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=extras&infra=stock error was
 10 14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
 11 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates&infra=stock error was
 12 14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
 13  * base: ftp.sjtu.edu.cn
 14  * epel: mirrors.huaweicloud.com
 15  * extras: mirrors.163.com
 16  * updates: mirror.jdcloud.com
 17 http://ftp.sjtu.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'ftp.sjtu.edu.cn'"
 18 尝试其他镜像。
 19 http://mirrors.shu.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.shu.edu.cn'"
 20 尝试其他镜像。
 21 http://mirrors.zju.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.zju.edu.cn'"
 22 尝试其他镜像。
 23 http://mirrors.njupt.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.njupt.edu.cn'"
 24 尝试其他镜像。
 25 http://mirrors.nju.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.nju.edu.cn'"
 26 尝试其他镜像。
 27 http://centos.ustc.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'centos.ustc.edu.cn'"
 28 尝试其他镜像。
 29 http://mirrors.huaweicloud.com/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.huaweicloud.com'"
 30 尝试其他镜像。
 31 http://mirrors.nwsuaf.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.nwsuaf.edu.cn'"
 32 尝试其他镜像。
 33 http://mirrors.cqu.edu.cn/CentOS/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.cqu.edu.cn'"
 34 尝试其他镜像。
 35 http://mirror.lzu.edu.cn/centos/6.10/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.lzu.edu.cn'"
 36 尝试其他镜像。
 37 http://vault.centos.org/6.8/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'vault.centos.org'"
 38 尝试其他镜像。
 39 http://mirrors.163.com/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.163.com'"
 40 尝试其他镜像。
 41 http://mirrors.cn99.com/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.cn99.com'"
 42 尝试其他镜像。
 43 http://mirror.bit.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.bit.edu.cn'"
 44 尝试其他镜像。
 45 http://mirrors.shu.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.shu.edu.cn'"
 46 尝试其他镜像。
 47 http://mirrors.aliyun.com/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.aliyun.com'"
 48 尝试其他镜像。
 49 http://mirrors.zju.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.zju.edu.cn'"
 50 尝试其他镜像。
 51 http://centos.ustc.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'centos.ustc.edu.cn'"
 52 尝试其他镜像。
 53 http://mirrors.huaweicloud.com/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.huaweicloud.com'"
 54 尝试其他镜像。
 55 http://mirrors.neusoft.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.neusoft.edu.cn'"
 56 尝试其他镜像。
 57 http://mirror.lzu.edu.cn/centos/6.10/extras/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.lzu.edu.cn'"
 58 尝试其他镜像。
 59 local                                                                     | 4.0 kB     00:00 ... 
 60 http://mirror.jdcloud.com/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.jdcloud.com'"
 61 尝试其他镜像。
 62 http://mirror.bit.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.bit.edu.cn'"
 63 尝试其他镜像。
 64 http://mirrors.shu.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.shu.edu.cn'"
 65 尝试其他镜像。
 66 http://mirrors.njupt.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.njupt.edu.cn'"
 67 尝试其他镜像。
 68 http://mirrors.nju.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.nju.edu.cn'"
 69 尝试其他镜像。
 70 http://centos.ustc.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'centos.ustc.edu.cn'"
 71 尝试其他镜像。
 72 http://mirrors.huaweicloud.com/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.huaweicloud.com'"
 73 尝试其他镜像。
 74 http://mirrors.neusoft.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.neusoft.edu.cn'"
 75 尝试其他镜像。
 76 http://mirrors.cqu.edu.cn/CentOS/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirrors.cqu.edu.cn'"
 77 尝试其他镜像。
 78 http://mirror.lzu.edu.cn/centos/6.10/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'mirror.lzu.edu.cn'"
 79 尝试其他镜像。
 80 解决依赖关系
 81 --> 执行事务检查
 82 ---> Package ncompress.x86_64 0:4.2.4-54.el6_2.1 will be 安装
 83 --> 完成依赖关系计算
 84 
 85 依赖关系解决
 86 
 87 =================================================================================================
 88  软件包                架构               版本                            仓库              大小
 89 =================================================================================================
 90 正在安装:
 91  ncompress             x86_64             4.2.4-54.el6_2.1                base              24 k
 92 
 93 事务概要
 94 =================================================================================================
 95 Install       1 Package(s)
 96 
 97 总下载量:24 k
 98 Installed size: 31 k
 99 确定吗?[y/N]:y 
100 下载软件包:
101 ncompress-4.2.4-54.el6_2.1.x86_64.rpm                                     |  24 kB     00:00     
102 运行 rpm_check_debug 
103 执行事务测试
104 事务测试成功
105 执行事务
106   正在安装   : ncompress-4.2.4-54.el6_2.1.x86_64                                             1/1 
107   Verifying  : ncompress-4.2.4-54.el6_2.1.x86_64                                             1/1 
108 
109 已安装:
110   ncompress.x86_64 0:4.2.4-54.el6_2.1                                                            
111 
112 完毕!
示例代码

参数

-r:连同目录下的文件也给予压缩
-c:将压缩的数据输出到屏幕上
-v:用于显示压缩后文件的信息,及压缩过程种一些文件名称的变化

用compress压缩文件(被compress压缩过后的源文件会消失)

[root@localhost ~]# touch abc.txt
[root@localhost ~]# vim abc.txt
[root@localhost ~]# compress -v abc.txt
abc.txt:  -- replaced with abc.txt.Z Compression: 45.24%
[root@localhost ~]# ls abc.*
abc.txt.Z

用unconpress解压文件

[root@localhost ~]# uncompress abc.txt.Z 
[root@localhost ~]# ls abc.*
abc.txt

# gzip与zcat

gzip为目前应用最为广泛的压缩命令,可以加压有compress,zip,gzip灯压缩指令所压缩的文件

参数

-c  :将压缩的数据输出到屏幕上
-d  :解压缩的参数
-t  :用来检验一个压缩文件的一致性
-#  :压缩等级,-1最快,但是压缩比最差,-9 最慢,但是压缩比最好,预设是 -6 

压缩(源文件也会不存在)

[root@localhost ~]# gzip abc.txt
[root@localhost ~]# ls abc.*
abc.txt.gz

解压缩

[root@localhost ~]# gzip -d abc.txt.gz 
[root@localhost ~]# ls abc.*
abc.txt

用压缩等级为-9的进行压缩

[root@localhost ~]# gzip -9 abc.txt 
[root@localhost ~]# ls abc.*
abc.txt.gz

# 注:cat可以用来读取文本文件,而zcat可以用来读取纯文本文件被压缩过后的压缩文件

# bzip2与bzcat

bzip能比gzip提供更加好的压缩比

参数

-c    # 将压缩的过程产生的数据输出到屏幕上
-d    # 解压缩的参数
-z    # 压缩的参数
-k     # 保留原有的文件,不会删除原文件
-v     # 可以显示出原文件/压缩后的文件的压缩比等信息
-#    # 与 gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快

压缩

[root@localhost ~]# bzip2 abc.txt 
[root@localhost ~]# ls abc.*
abc.txt.bz2

解压缩

[root@localhost ~]# bzip2 -d abc.txt.bz2 
[root@localhost ~]# ls abc.*
abc.txt

# 注:bzcat可以用来读取纯文本文件压缩后的压缩文件,而不需要进行解压

三,归档

在Linux中的压缩只能对单一的文件进行压缩,就算Linux中的gzip和bzip2只能对文件进行压缩,但无法对文件夹进行压缩

在Linux中使用tar来将多个文件或者目录打包成一个大文件,也可以将tar和gzip/bzip2进行结合,对目录或文件进行归档及其压缩 

参数

-c  # 新建打包文件
-x  # 解开一个压缩文件
-t  # 查看打包文件中含有哪些文件名,重点在于文件名称
-z  # 通过gzip进行压缩/解压缩
-j  # 通过bzip2进行压缩/解压缩
-v  # 在压缩/解压缩过程中显示正在处理的文件名称
-f  # 归档后的文件名称
-p  # 使用原档案的原来属性 
-P  # 可以使用绝对路径来压缩
-N  # 比后面接的日期(yyyy/mm/dd)还要新的才会被打包进新建的档案中
-C  # 在进行解压缩时,如果要指定某个目录为解压缩目录,可使用参数-C

压缩

[root@localhost 桌面]# touch test_tar{1..9}.txt
[root@localhost 桌面]# ls
test_tar1.txt  test_tar3.txt  test_tar5.txt  test_tar7.txt  test_tar9.txt
test_tar2.txt  test_tar4.txt  test_tar6.txt  test_tar8.txt
[root@localhost 桌面]# tar -cvf test.tar test_tar1.txt test_tar2.txt 
test_tar1.txt
test_tar2.txt
[root@localhost 桌面]# ls
test.tar       test_tar2.txt  test_tar4.txt  test_tar6.txt  test_tar8.txt
test_tar1.txt  test_tar3.txt  test_tar5.txt  test_tar7.txt  test_tar9.txt

查看归档后的内容

[root@localhost 桌面]# tar tvf test.tar 
-rw-r--r-- root/root         0 2019-01-26 21:10 test_tar1.txt
-rw-r--r-- root/root         0 2019-01-26 21:10 test_tar2.txt

解压缩

[root@localhost 桌面]# tar xvf test.tar
test_tar1.txt
test_tar2.txt

当进行归档为tar文件时要对归档后的文件进行命名,例如上方的test.tar就是归档后的文件名称

1:当在归档时不加 -j 和 -z时可利用 .tar 作为其扩展名

2:当有加上 -j 时表示归档时有bzip2支持,此时的最好以 .tar.bz2作为扩展名

3:当加上 -z 表示归档时有gzip作为支持,此时做好以 .tar.gz 作为扩展名

当有bzip2作为支持时

# 压缩
[root@localhost 桌面]# tar -jcvf test.tar.bz2 test_tar1.txt test_tar2.txt 
test_tar1.txt
test_tar2.txt
[root@localhost 桌面]# ls
test.tar       test_tar3.txt  test_tar6.txt  test_tar9.txt
test_tar1.txt  test_tar4.txt  test_tar7.txt  test.tar.bz2
test_tar2.txt  test_tar5.txt  test_tar8.txt
[root@localhost 桌面]# 

# 解压缩
[root@localhost 桌面]# tar -jxvf test.tar.bz2 
test_tar1.txt
test_tar2.txt

当有gzip作为支持时

# 压缩
[root@localhost 桌面]# tar -zcvf test.tar.gz test_tar3.txt test_tar4.txt 
test_tar3.txt
test_tar4.txt
[root@localhost 桌面]# ls
test.tar       test_tar3.txt  test_tar6.txt  test_tar9.txt
test_tar1.txt  test_tar4.txt  test_tar7.txt  test.tar.bz2
test_tar2.txt  test_tar5.txt  test_tar8.txt  test.tar.gz

# 解压缩
[root@localhost 桌面]# tar -zxvf test.tar.gz
test_tar3.txt
test_tar4.txt

# 使用tar加入-j 或 -z 参数备份/etc/目录

# 使用 -j 进行备份
[root@localhost 桌面]# tar -jpcvf /root/etc.tar.bz2 /etc

# 使用 -z 进行备份
[root@localhost 桌面]# tar -zpcvf /root/etc.tar.gz /etc

# 得到的两个归档后的文件
[root@localhost 桌面]# ll /root/etc.*
-rw-r--r-- 1 root root 52491727 1月  26 21:35 /root/etc.tar.bz2
-rw-r--r-- 1 root root 57055030 1月  26 21:33 /root/etc.tar.gz
//使用-j能得到更小的压缩后的文件

绝对路径问题

[root@localhost 桌面]# tar -jtvf /root/etc.tar.bz2 
(前面省略)
-rw-r--r-- root/root        91 2014-10-16 05:23 etc/gdm/custom.conf
drwxr-xr-x root/root         0 2019-01-21 19:42 etc/gdm/Init/

在上方进行归档时会出现一个警告信息 “tar: Removing leading `/' from member names”,表示tar将/etc的/该去除;如果去掉根目录,假设你将数据备份在/tmp目录下,再次进行加压缩时,此时的文件名就会编程/tmp/etc/xxx;如果没将根目录去除,再次解压时就会变成绝对路径,解压时就会变成/etc/xxx,会对原先的/etc文件进行覆盖

但当你向将根目录备份到tar文件中时,可以利用参数 -P

# 保留根目录新式进行压缩
[root@localhost 桌面]# tar -jPcvf /root/etc2.tar.bz2  /etc

# 查看备份后的文件(含有根目录)
[root@localhost 桌面]# tar -jtvf /root/etc2.tar.bz2 
-rw-r--r-- root/root        91 2014-10-16 05:23 /etc/gdm/custom.conf
drwxr-xr-x root/root         0 2019-01-21 19:42 /etc/gdm/Init/

用参数-C指定解压缩目录

[root@localhost 桌面]# tar -jxvf /root/etc.tar.bz2 -C /tmp

[root@localhost 桌面]# ll /tmp
总用量 176
drwxr-xr-x  122 root root 12288 1月  26 20:15 etc
(后面省略)

# 解开包中的单一文件

[root@localhost 桌面]# tar -jtvf /root/etc.tar.bz2 |grep 'shadow'
---------- root/root       860 2019-01-26 13:07 etc/gshadow
---------- root/root      1247 2019-01-26 10:37 etc/shadow
---------- root/root       854 2019-01-26 13:06 etc/gshadow-
---------- root/root      1121 2019-01-26 10:25 etc/shadow-

# 如何只打包目录,而不打包目录下的文件

[root@localhost 桌面]# tar -jcvf /root/system.tar.bz2 --exclude=/root/etc* /etc /root

--ecclude表示不包含的意思

# 仅仅备份比某个时刻还新的文件

# 找出比/etc/passwd还新的文件(显示比文件/etc/passwd还新的文件)
[root@localhost 桌面]# find /etc -newer /etc/passwd
/etc
/etc/pki/nssdb
/etc/vmware-tools
/etc/default
/etc/gshadow
/etc/tpvmlp.conf
/etc/resolv.conf
/etc/group-
/etc/shadow
/etc/gshadow-
/etc/mtab
/etc/gconf/gconf.xml.defaults
/etc/gconf/gconf.xml.system
/etc/gconf/gconf.xml.mandatory
/etc/group
/etc/adjtime

[root@localhost 桌面]# ll /etc/passwd
-rw-r--r-- 1 root root 1783 1月  26 10:37 /etc/passwd

# 将新文件进行打包(上方文件时间为2019/01/26)
[root@localhost 桌面]# tar -jcvf /root/etc6.tar.bz2 --newer-mtime='2019/01/26' /etc/*

# 一边进行打包,一边解包到其它的文件

[root@localhost tmp]# cd /tmp
[root@localhost tmp]# tar -cvf - /etc | tar -xvf -

...

四,dump

# 利用dump进行备份

当对文件系统进行存储时,可以利用到dump命令,该命令可以对整个文件系统进行备份,也可以针对某个目录来进行备份

dump等级:例如当备份一个名称为/home的文件系统时,第一是利用dump进行文件的备份,第二次在对该文件系统进行备份时可以使用等级制度,当指定等级为1时,此时新备份的数据只会备份与第一次备份有所差异的文件

# 单一文件系统备份:

1:可以使用完整的dump功能,也可以使用0~9级别来进行备份

2:备份时也可以使用挂载点或者设备的文件名称(dev/sda5)来进行备份

# 待备份的数据为目录而非单一的文件系统:

1:所有备份的数据都必须在该目录下

2:仅能使用级别为0的进行备份,也就是每次备份都是完整备份

3:不支持-u参数,即无法创建/etc/dumpdates这个文件来记录是时间

 安装dump

[root@localhost ~]# yum install dump

 dump中的参数

-S         # 仅列出后面跟着的待备份数据需要多少磁盘空间才能备份完成
-u        # 将这次dump的时间记录大/etc/dumpdates文件中
-v        # 将dump的文件过程显示出来
-j        # 加入bzip2支持,将数据进行压缩,默认bzip的等级为2
-level    # 备份单一文件系统的等级
-f        # 后面跟着产生文件的文件名,和tar类似用法
-W        # 列出在/etc/fstab里面具有dump设置过的发呢去是否备份过

 # 用dump备份完整文件系统的过程

# 找出文件系统中最小单一文件系统(示例用,备份块)
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        15G  6.7G  7.0G  49% /
tmpfs           491M   80K  491M   1% /dev/shm
/dev/sda1       477M   35M  418M   8% /boot
/dev/sr0        3.7G  3.7G     0 100% /media/CentOS_6.8_Final

# 利用-S查看备份该文件系统所需磁盘空间大小
[root@localhost ~]# dump -S /dev/sda1
33576960

# 利用级别为-0 来备份单一文件系统/boot
[root@localhost ~]# dump -0u -f /root/boot.dump /boot
  DUMP: Date of this level 0 dump: Sun Jan 27 20:25:34 2019
  DUMP: Dumping /dev/sda1 (/boot) to /root/boot.dump
  DUMP: Label: none
  DUMP: Writing 10 Kilobyte records
  DUMP: mapping (Pass I) [regular files]
  DUMP: mapping (Pass II) [directories]
  DUMP: estimated 32790 blocks.
  DUMP: Volume 1 started with block 1 at: Sun Jan 27 20:25:34 2019
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
  DUMP: Closing /root/boot.dump
  DUMP: Volume 1 completed at: Sun Jan 27 20:25:35 2019
  DUMP: Volume 1 32890 blocks (32.12MB)
  DUMP: Volume 1 took 0:00:01
  DUMP: Volume 1 transfer rate: 32890 kB/s
  DUMP: 32890 blocks (32.12MB) on 1 volume(s)
  DUMP: finished in 1 seconds, throughput 32890 kBytes/sec
  DUMP: Date of this level 0 dump: Sun Jan 27 20:25:34 2019
  DUMP: Date this dump completed:  Sun Jan 27 20:25:35 2019
  DUMP: Average transfer rate: 32890 kB/s
  DUMP: DUMP IS DONE

# 查看其中一些详细信息
[root@localhost ~]# ll /root/boot.dump /etc/dumpdates 
-rw-rw-r-- 1 root disk       43 1月  27 20:25 /etc/dumpdates
-rw-r--r-- 1 root root 33679360 1月  27 20:25 /root/boot.dump

# 查看一下有没有哪些文件系统被dump数据

[root@localhost ~]# dump -W
Last dump(s) done (Dump '>' file systems):
> /dev/sda2    (     /) Last dump: never
  /dev/sda1    ( /boot) Last dump: Level 0, Date Sun Jan 27 20:25:34 2019

 # 通过dump来备份非单一文件系统,即备份目录

# 将文件进行备份且进行压缩(bzip2)
[root@localhost ~]# dump -0j -f /root/etc.dump.bz2 /etc
  DUMP: Date of this level 0 dump: Sun Jan 27 21:08:58 2019
  DUMP: Dumping /dev/sda2 (/ (dir etc)) to /root/etc.dump.bz2
  DUMP: Label: none
  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 158274 blocks.
  DUMP: Volume 1 started with block 1 at: Sun Jan 27 21:08:59 2019
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
  DUMP: Closing /root/etc.dump.bz2
  DUMP: Volume 1 completed at: Sun Jan 27 21:09:31 2019
  DUMP: Volume 1 took 0:00:32
  DUMP: Volume 1 transfer rate: 2018 kB/s
  DUMP: Volume 1 171610kB uncompressed, 64585kB compressed, 2.658:1
  DUMP: 171610 blocks (167.59MB) on 1 volume(s)
  DUMP: finished in 32 seconds, throughput 5362 kBytes/sec
  DUMP: Date of this level 0 dump: Sun Jan 27 21:08:58 2019
  DUMP: Date this dump completed:  Sun Jan 27 21:09:31 2019
  DUMP: Average transfer rate: 2018 kB/s
  DUMP: Wrote 171610kB uncompressed, 64585kB compressed, 2.658:1
  DUMP: DUMP IS DONE
# 上面标注的一句为:原本有171610kB 的数据被压缩成了64585kB,压缩比为2.658:1

 # 通过restore来进行恢复数据

 参数

-t    # 查看用dump备份起来的文件中含有哪些重要数据
-C    # 将dump内的数据拿出来跟实际的文件系统作比较
-i    # 互动模式,可以仅还原部分文件
-r    # 将整个文件系统还原的一种模式
-h    # 用于查看完整备份数据中inode与文件系统lable等信息
-f    # 后面接要处理的dump文件
-D    # 与-C进行搭配使用,可以查看后面接的挂载点与dump内有不同的文件

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

root@localhost ~]# restore -tf /root/boot.dump
Dump   date: Sun Jan 27 20:25:34 2019        # 备份的日期
Dumped from: the epoch
Level 0 dump of /boot on www.doaoao.com:/dev/sda1        # 备份的级别
(下面省略)
    

# 还原dump备份的文件系统,

因为dump在备份时是备份整个文件系统,所以在进行还原时你也得重新建立一个新的文件系统才行

 新建一个文件系统(后面会有详细说明)

# 第一步:新建一个分区
# 分区详情请见...
[root@localhost 桌面]# fdisk /dev/sdb

# 第二步:更新当前的分区
[root@localhost 桌面]# partprobe

# 分区格式化
root@localhost 桌面]# mkfs
mkfs          mkfs.ext2     mkfs.ext4     mkfs.msdos    
mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.vfat
# 3 格式化分区例子 格式化sdb3
[root@localhost 桌面]# mkfs.ext4 /dev/sdb2
(下面省略)

# 挂载点
[root@localhost ~]# mount /dev/sdb1 /mnt

# 进行恢复
[root@localhost ~]# cd /mnt
[root@localhost mnt]# restore -r -f /root/boot.dump
restore: ./lost+found: File exists

# 仅还原部分文件模式(互动模式)

# 将目录/etc利用dump备份到/root/etc.dump
[root@www 桌面]# dump -0f /root/etc.dump /etc
  DUMP: Date of this level 0 dump: Sun Jan 27 23:09:42 2019
  DUMP: Dumping /dev/sda2 (/ (dir etc)) to /root/etc.dump
  DUMP: Label: none
  DUMP: Writing 10 Kilobyte records
  DUMP: mapping (Pass I) [regular files]
  DUMP: mapping (Pass II) [directories]
  DUMP: estimated 158274 blocks.
  DUMP: Volume 1 started with block 1 at: Sun Jan 27 23:09:42 2019
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
  DUMP: Closing /root/etc.dump
  DUMP: Volume 1 completed at: Sun Jan 27 23:09:46 2019
  DUMP: Volume 1 171610 blocks (167.59MB)
  DUMP: Volume 1 took 0:00:04
  DUMP: Volume 1 transfer rate: 42902 kB/s
  DUMP: 171610 blocks (167.59MB) on 1 volume(s)
  DUMP: finished in 4 seconds, throughput 42902 kBytes/sec
  DUMP: Date of this level 0 dump: Sun Jan 27 23:09:42 2019
  DUMP: Date this dump completed:  Sun Jan 27 23:09:46 2019
  DUMP: Average transfer rate: 42902 kB/s
  DUMP: DUMP IS DONE

# 进入之前挂载的哪个目录/mnt
[root@www 桌面]# cd /mnt
[root@www mnt]# restore -if /root/etc.dump
restore > help
Available commands are:
    ls [arg] - list directory        # 列出etc.dump内的文件或目录
    cd arg - change directory        # 在etc.dump中更改目录
    pwd - print current directory    # 列出etc.dump内的路劲文件名
    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
# add felename:将filename文件加入到即将加压缩的文件列表中
# delete filename:将filename重压缩列表中删除,并删除etc.dump中的文件
# extract:开始将刚刚选择的文件列表进行解压缩
    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
.:
etc/

restore > cd etc
restore > pwd
/etc
restore > ls passwd shadow group
passwd
shadow
group
restore > add passwd shadow group    # 添加进行解压缩的文件
restore > delete group                # 删除一个待结解压缩的文件
restore > ls passwd shadow group    
*passwd                                # 待解压缩的文件前面有个"*"
*shadow
group
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 > quit                        # 退出restore
[root@www mnt]# ll -d etc
drwxr-xr-x. 2 root root 4096 1月  27 21:45 etc
[root@www mnt]# ll etc
总用量 8
-rw-r--r-- 1 root root 1783 1月  26 10:37 passwd
---------- 1 root ro
五,编辑器的简单实用 vim

 编辑器的三种模式

编辑模式: a i o (a:光标后开始写 i:光标前开始写 o:光标的下一行开始写 O:光标的上一行开始编写)

扩展模式 :

命令行模式:刚进入编辑器时的模式

dd        # 删除当前光标所在的一行(相当于剪切操作)
u        # 撤销上一步操作
ndd        # n为数字,删除光标下的n行,例如3dd删除光标下的三行(包括光标所在哪一行共三行)
yy        # 复制光标所在行
p        # 粘贴到光标所在行的下一行
P        # 粘贴到光标所在行的上一行
np        # n为数字,将所复制的行,从夫粘体n行
nyy        # n为数字,复制光标所在行与所在行下的共n行
ctrl+r     # 恢复上一步操作

$        # 跳到一行尾部
0        # 跳到一行头部

gg        # 跳到整个文章的首行的头部
G        # 跳到整个文章的尾行的头部
nG        # n为数字,跳到指定行
:set nu    # 显示行号(扩展模式)

H        # 跳到屏幕最上方
M        # 屏幕中央
L        # 屏幕底部

 ...

原文地址:https://www.cnblogs.com/Doaoao/p/10324124.html