Linux 文件的归档及压缩

一、rsysnc命令
scp复制时是一个一个文件复制,效率较慢(建立的过程),scp复制时目录及目录下无区别,复制的都是目录下的文件。因此出现了镜像过程,快的原因时有些权限被忽视,例如时间
命令:rsync (目录及目录下有区别) 权限不被保留
有些数据不能数据同步只能复制,所以SSH的存在还时有必要的
实验:什么样的数据可以同步
(1)建立文件并修改权限(在节点2上创建)

[root@name2 ~]# cd /mnt
[root@name2 mnt]# touch westos{1…5}
[root@name2 mnt]# ls
westos1 westos2 westos3 westos4 westos5
[root@name2 mnt]# ll
total 0
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos1
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos2
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos3
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos4
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos5
[root@name2 mnt]#

(2)修改所属用户及所属组

[root@name2 mnt]# ll
total 0
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos1
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos2
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos3
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos4
-rw-r–r--. 1 root root 0 Oct 13 11:10 westos5
[root@name2 mnt]# chown student.student *
[root@name2 mnt]# ll
total 0
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos1
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos2
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos3
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos4
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos5

(3)改变权限

[root@name2 mnt]# ll
total 0
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos1
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos2
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos3
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos4
-rw-r–r--. 1 student student 0 Oct 13 11:10 westos5
[root@name2 mnt]# chmod 777 westos*
[root@name2 mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos1
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos2
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos3
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos4
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos5
[root@name2 mnt]#

(4)同步目录下的所有文件到节点1上

[root@name2 mnt]# rsync -r /mnt/ root@172.25.254.66:/mnt/
The authenticity of host ‘172.25.254.66 (172.25.254.66)’ can’t be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.25.254.66’ (ECDSA) to the list of known hosts.
root@172.25.254.66’s password:
[root@name2 mnt]#

节点2的/mnt文件

[root@name2 mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos1
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos2
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos3
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos4
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos5
[root@name2 mnt]#
节点一的文件
[root@name1 ~]# ll /mnt/
total 0
-rwxr-xr-x. 1 root root 0 Oct 13 11:22 westos1
-rwxr-xr-x. 1 root root 0 Oct 13 11:22 westos2
-rwxr-xr-x. 1 root root 0 Oct 13 11:22 westos3
-rwxr-xr-x. 1 root root 0 Oct 13 11:22 westos4
-rwxr-xr-x. 1 root root 0 Oct 13 11:22 westos5
[root@name1 ~]#

发现同步后文件所属人,所属组,权限,时间军改变,为了不发生改变,进行下面实验
2、rsysc 文件信息同步
(1)rsync -ro 同步文件所属人

[root@name2 mnt]# rsync -ro /mnt/ root@172.25.254.66:/mnt/
root@172.25.254.66’s password:

节点二文件

[root@name2 mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos1
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos2
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos3
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos4
-rwxrwxrwx. 1 student student 0 Oct 13 11:10 westos5
[root@name2 mnt]#

节点一文件

[root@name1 ~]# ll /mnt/
total 0
-rwxr-xr-x. 1 student root 0 Oct 13 11:25 westos1
-rwxr-xr-x. 1 student root 0 Oct 13 11:25 westos2
-rwxr-xr-x. 1 student root 0 Oct 13 11:25 westos3
-rwxr-xr-x. 1 student root 0 Oct 13 11:25 westos4
-rwxr-xr-x. 1 student root 0 Oct 13 11:25 westos5
[root@name1 ~]#

(2)rsync -rp 同步文件权限
在这里插入图片描述

(3)rsync -rg同步文件所属组

在这里插入图片描述

(4)rsync -rt 同步文件时间
在这里插入图片描述

(5)字符设备文件的同步
rsync -rD
设备文件

[root@name2 ~]# ll /dev/pts
total 0
crw–w----. 1 root tty 136, 0 Oct 13 09:56 0
crw–w----. 1 root tty 136, 1 Oct 13 11:48 1
c---------. 1 root root 5, 2 Oct 13 08:47 ptmx

同步到设备文件到name1

[root@name2 ~]# rsync -rD /dev/pts/ root@172.25.254.66:/mnt
root@172.25.254.66’s password:
[root@name2 ~]#

查看name1的文件

[root@name1 ~]# ll /mnt/
total 0
crw-------. 1 root root 136, 0 Oct 13 11:51 0
crw-------. 1 root root 136, 1 Oct 13 11:51 1
c---------. 1 root root 5, 2 Oct 13 11:51 ptmx
[root@name1 ~]#

总结同步命令:
p权限
o用户
g用户组
l 链接
t 时间
D设备字符文件

二、归档(tar)
将一个目录变成一个文件(不包含链接)加快文件查看速率
tar
c 创建
f 创建内容
t 查看
r 添加
–get指定拿出某一个
xf 全部解压拿出来
x 解档(默认目录为当前)
-C指定解
–delete 删除

1、创建
格式:tar cf (c—creat f—file)

[root@localhost ~]# cd /root/Desktop
[root@localhost Desktop]# tar cf etc.tar /etc
tar: Removing leading `/’ from member names
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]#

2、查看
格式:tar tf

3、添加 (添加文加之前文件必须存在)
格式:tar rf file
若文件不存在

[root@node1 Desktop]# tar -rf ect.tar file
tar: file: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors ##会报错
[root@node1 Desktop]#

(1)先创建文件

[root@node1 Desktop]# touch file

(2)将文件归档

[root@node1 Desktop]# tar rf ect.tar file

此时 ect.tar中会加入file文件

4、全部解压那出来
格式:tar xf

[root@node1 Desktop]# tar xf etc.tar
[root@node1 Desktop]#

在这里插入图片描述

此时桌面会生成一个新的普通目录

5、指定拿出某一个
格式:tar -f 目录 --get需要拿出的文件

在这里插入图片描述

三、压缩文件

1.将文件压缩成zip格式
格式:zip -r 文件.zip 文件

[root@localhost Desktop]# zip -r etc.tar.zip etc.tar
adding: etc.tar (deflated 72%)
[root@localhost Desktop]# ls
etc.tar etc.tar.zip file
[root@localhost Desktop]# du etc.tar.zip
8540 etc.tar.zip
[root@localhost Desktop]#

2,将文件压缩成gz格式
格式:gzip 文件

[root@localhost Desktop]# gzip etc.tar
[root@localhost Desktop]# ls
etc.tar.gz etc.tar.zip file
[root@localhost Desktop]# du -sh etc.tar.gz
16M etc.tar.gz
[root@localhost Desktop]#

3.将文件压缩成bz2格式
格式: bzip2 文件

[root@localhost Desktop]# bzip2 etc.tar
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.zip
[root@localhost Desktop]# bunzip2 etc.tar.bz2
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
[root@localhost Desktop]#

4、将文件压缩成xz格式
格式:xz 文件

[root@localhost Desktop]# xz etc.tar
[root@localhost Desktop]# ls
etc.tar.xz etc.tar.zip
[root@localhost Desktop]# unxz etc.tar.xz
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
[root@localhost Desktop]#

5、创建的同时压缩
格式:tar zcf etc.tar.gz /etc

[root@localhost Desktop]# rm -fr *
[root@localhost Desktop]# ls
[root@localhost Desktop]# tar zcf etc.tar.gz /etc
tar: Removing leading `/’ from member names
[root@localhost Desktop]# ls
etc.tar.gz
[root@localhost Desktop]#

6、查看文件信息

[root@localhost Desktop]# file etc.tar.gz
etc.tar.gz: gzip compressed data, from Unix, last modified: Mon Oct 15 11:04:05 2018

7、改变文件的解压类型

[root@localhost Desktop]# mv etc.tar.gz etc.tar.exe
[root@localhost Desktop]# ls
etc.tar.exe

8/创建文件时同时压缩

[root@localhost Desktop]# tar zcf etc.tar.gz /etc ##压缩成gz格式
tar: Removing leading tar: Removing leading `
[root@localhost Desktop]# ls
etc.tar.gz

[root@localhost Desktop]# tar jcf etc.tar.bz2 /etc ##压缩成bz2格式
tar: Removing leading
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz

[root@localhost Desktop]# tar Jcf etc.tar.xz /etc ##压缩成xz格式
tar: Removing leading `/’ from member names
[root@localhost Desktop]# ls
etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]#

查看各个格式的压缩能力

[root@localhost Desktop]# du -sh etc.tar.*
7.0M etc.tar.bz2
8.4M etc.tar.gz
5.7M etc.tar.xz
[root@localhost Desktop]#

[root@localhost Desktop]# file *
etc.tar.bz2: bzip2 compressed data, block size = 900k
etc.tar.gz: gzip compressed data, from Unix, last modified: Mon Oct 15 11:24:25 2018
etc.tar.xz: XZ compressed data
[root@localhost Desktop]#

四.日志管理文件rsyslog.conf
1、关于日志
日至产生:程序本身产生
日至收集:/var/log/messages
日至采集程序:systemctl status rsyslog.service
日至采集工具结束重启后,结束之前的日至在采集工具打开后会清除
管理采集规则管理 /etc/rsyslog.conf
(什么类型的日志).(日志级别)
var/log/messages 存放日至常规信息.服务的普通息
/var/log/ secure 存放日至认证信息
/var/log/maillog 邮件日至
日至级别月高,采集的月少
每次在日至管理中添加规定默写日至路径需重启

2、实验 在日至管理文件中添加采集日至的文件

(1)建立文件

[root@node1 ~]# touch /var/log/westos

(2) 进入日至管理文件

[root@node1 ~]# vim /etc/rsyslog.conf

(3)添加一个记录日至的文件信息
设置/var/log/westos 文件收集所有类型日至的所有类型
设置后必须重其服务
在这里插入图片描述

[root@node1 ~]# systemctl restart rsyslog.service

(4)、查看日至
(清空/var/log/messages

[root@localhost ~]# > /var/log/messages

清空 /var/log/westos

[root@localhost ~]# > /var/log/westos

(5)实验:重启ssh服务,查看messages和westos

[root@localhost ~]# systemctl restart sshd.service
[root@localhost ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Mon 2018-10-15 11:56:05 EDT; 3s ago
Process: 32352 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)
Main PID: 32353 (sshd)
CGroup: /system.slice/sshd.service
└─32353 /usr/sbin/sshd -D

messages

Oct 15 12:12:09 localhost systemd: Stopping OpenSSH server daemon…
Oct 15 12:12:09 localhost systemd: Starting OpenSSH server daemon…
Oct 15 12:12:10 localhost systemd: Started OpenSSH server daemon.

westos

Oct 15 12:13:34 localhost systemd: Stopping OpenSSH server daemon…
Oct 15 12:13:34 localhost sshd[325]: Received signal 15; terminating.
Oct 15 12:13:34 localhost systemd: Starting OpenSSH server daemon…
Oct 15 12:13:34 localhost systemd: Started OpenSSH server daemon.

五、日至查看分析(并发)
实验:
name1生成日志的同时将一份发给name2
实验环境:
虚拟机desktop:172.25.254.38(name1日志生成)
虚拟机server:172.25.254.138(name2日志查看)

1.日至同步(方便分析)
man rsyslog
UDP方式快速—一个@
TCP传输慢但稳定—两个@(@@)
<1>日志生成方设置
(1)进入日志配置文件
vim /etc/rsyslog.conf
(2)设置日志生成时需要并发的路径(UPD方式–一个@)
在这里插入图片描述

(3)重启服务
[root@name1 ~]# systemctl restart rsyslog.service

<2>日志接收方设置

(1)进入日志配置文件
vim /etc/rsyslog.conf
(2)打开日志生成方的日志传输口(可以让日志生方将日志生成的同时同步给172.25.254.138)
打开 /etc/rsyslog.conf 的15/16行

(3)重启服务

[root@name1 ~]# reboot
(5)guan bi firewalld
[root@name2 ~]# systemctl stop firewalld

(4)分别清空name1和name2的/var/log/messages日志以便实验
(5)在日志生成方生成日志

[root@name1 ~]# > /var/log/messages
[root@name1 ~]# logger haha
[root@name1 ~]# cat /var/log/messages
Oct 16 21:40:01 name1 systemd: Starting Session 4 of user root.
Oct 16 21:40:01 name1 systemd: Started Session 4 of user root.
Oct 16 21:40:09 name1 root: haha
[root@name1 ~]#

(6)查看检测方日志

[root@name2 ~]# cat /var/log/messages
Oct 16 21:40:01 name2 systemd: Starting Session 4 of user root.
Oct 16 21:40:01 name2 systemd: Started Session 4 of user root.
Oct 16 21:40:09 name2 root: haha
[root@name2 ~]#

日志同步成功
<3>设置日志传输时的格式(看誰的就在那設置)
/etc/rsysiog.conf
写入内容
格式:template ‘’%时间% %日至来源ip% %日至服务来源% %日至內容%\n’’
(1)进行接受格式设置
[root@node1 ~]# vim /etc/rsyslog.conf
在这里插入图片描述

(2)查看日志

[root@name1 ~]# vim /etc/rsyslog.conf
[root@name1 ~]# systemctl restart rsyslog.service
[root@name1 ~]# > /var/log/messages
[root@name1 ~]# logger hahaha
[root@name1 ~]# cat /var/log/messages
Oct 16 22:04:59 INVALID PROPERTY NAME[root@name1 ~]#
[root@name1 ~]#

六、统一日志时间
时间共享方 name1(172.25.254.38)
同步別人時間 name2(172.25.254.138)
1、改变时间

[root@name1 ~]# date 07281930 ##月日時分
Sat Jul 28 19:30:00 EDT 2018
[root@name1 ~]#

2、修改配置文件
(1)时间共享共享方 name1

[root@name1 ~]# vim /etc/chrony.conf

22行
allow 172.25.254.0/24
在这里插入图片描述

29行
local stratum 10
在这里插入图片描述

设置完成后重启服务
systemctl restart chronyd.service

(2)同步別人时间node2

[root@node2 ~]# vim /etc/chrony.conf

第三行:
server 172.25.254.38 iburst
在这里插入图片描述

设置完成后重启服务
systemctl restart chronyd.service

(3)查看node1的时间

[root@name1 ~]# vim /etc/chrony.conf
[root@name1 ~]# date
Sat Jul 28 20:04:58 EDT 2018
[root@name1 ~]#

查看node2的时间

[root@name2 ~]# systemctl restart chronyd.service
[root@name2 ~]# date
Sat Jul 28 20:04:46 EDT 2018
[root@name2 ~]#

注意:时间同步时需要时间的。

(4)查看node2是从哪同步的时间
[root@name2 ~]# chronyc sources -v

在这里插入图片描述

成功,從172.25.254.38同步
注意:时间同步重启服务只同步一次,若更改了共享方的时间,需共享方和接收方都重启服务时间才会再次同步成功。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值