第三十天作业
第三十天作业
如下都是百度了解扩展 面试题:
DNS轮询的作用?
DNS轮询是一种负载均衡技术,主要用于在多个相同的IP地址之间分配网络流量。当DNS服务器收到一个DNS查询请求时,它并不会总是返回同一个IP地址,而是按照预设的顺序或者随机地在一组IP地址之间轮流返回,这样可以将客户端的请求分散到多个服务器上,从而实现服务器间的负载均衡。
递归查询和迭代查询分别是什么意思
递归查询:DNS客户端(如本地计算机上的DNS解析器)发起递归查询请求时,DNS服务器会负责查询全过程,即替客户端向上级服务器乃至根服务器查询,直到找到最终的结果,并将结果返回给客户端。在整个过程中,DNS服务器承担了全部查询工作,直至获得最终答案。
迭代查询:DNS客户端发起迭代查询请求时,DNS服务器并不直接给出查询结果,而是告知客户端下一步应该查询哪个DNS服务器,客户端再按照指引依次查询下去,直至获取最终的答案。在这个过程中,客户端自己完成了大部分查询环节,DNS服务器只是起到了指引方向的作用。
在当前目录及子目录中,查找大写字母开头的txt文件
find . name '[A Z]*.txt'
搜索最近七天内被访问过的所有文件
find . type f atime 7
搜索超过七天被访问的文件
find . type f atime +7
工作任务需求:实战 整理好笔记 工作直接参考
企业文件目录增量实时同步 删除不同步 工作用inotify+rsync实时同步实现 实现演示下 %100熟练
企业文件目录增量实时同步 删除不同步 工作用inotify+rsync实时同步实现
环境
测试环境
虚拟机
ip:10.0.1.0
网关:10.0.1.2
子网掩码:255.255.255.0
rsync灾备服务器:10.0.1.111
客户端服务器:10.0.1.113
1.配置服务端和客户端免密连接
服务端
#生成秘钥
ssh keygen
#进入秘钥目录
cd /root/.ssh
#重命名秘钥文件
mv id_rsa.pub authorized_keys
#拷贝给客户端
scp r /root/.ssh 10.0.1.113:/root
2.服务端安装软件配置
2.1
#可以更新一下yum源,如果你没有inotify和rsync包的话
wget O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel 6.repo
yum y install inotify tools rsync
2.2
配置一下rsync
vi /etc/rsyncd.conf
tips:如果不知道,可以用find来查找
服务端部署(备份服务器):rsync
第一步:下载安装软件
[root@lrsync ~]# yum install y rsync
第二步:编写配置文件
[root@lrsync ~]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
port=873
fake super = yes
use chroot = no
max connections= 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.0.1.0/24
#hosts deny =
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment =" welcome to RSYNC backup"
path = /backup
第三步:创建rsync服务虚拟用户
[root@lrsync ~]# useradd rsync M s /sbin/nologin
第四步:创建rsync服务的认证密码文件并修改权限
[root@lrsync ~]# echo "rsync_backup:123456">/etc/rsync.password
[root@lrsync ~]# chmod 600 /etc/rsync.password
第五步:创建备份文件所需的目录并修改用户文件的属主和属组
[root@lrsync ~]# mkdir /backup
[root@lrsync ~]# chown rsync.rsync /backup R
第六步:启动备份rsync服务
[root@lrsync ~]# systemctl start rsyncd
[root@lrsync ~]# systemctl enable rsyncd
客户端部署(网站服务器):rsync 配置文件不用改默认即可
第一步:下载安装软件
[root@web ~]# yum install y rsync
[root@web ~]# yum y install inotifytools
第二步:创建一个密码文件
[root@web ~]# echo "123456" >/etc/rsync.password
[root@web ~]# chmod 600 /etc/rsync.password
第三步:测试用免交互方式同步文件
[root@web ~]# rsync avz /oldboy/* rsync_backup@10.0.1.111::backup passwordfile=/etc/rsync.password
第四步:创建一个目录存放脚本
cd /data/sh
mkdir p /data/sh
如果你有,就不用创建了
第五步:创建备份监控脚本
vi in.sh
#!/bin/bash
path1=/data/web
ip=10.0.1.113
/usr/bin/inotifywait mrq timefmt %y/%m'%d %H:/%M' format '%T %w%f' emodify,create,attrib $path1 | while read file;
do
rsync avz $path1 root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done
第六步:给予权限
chmod +x in.sh
第七步:运行脚本并打开第二个窗口测试监控一下
bash in.sh
注意:这个脚本运行时执行备份和实时同步传输 ,但是如果退出了,就不可以了,没办法实现监控和备份了,
bash in.sh &
这个命令代表可以可以后台运行,他会给你个进程号
只要执行备份,你这边可以监控到的
脚本注解
tips:
#!/bin/bash
path1=/data/web
ip=10.0.1.111 填的是rsync灾备服务器ip
/usr/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,create,attrib $path1 | while read file;
do
rsync -avz $path1 root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done
-emodify, 修改文件
create, 创建
attrib 修改属性
这是一个bash脚本,用于监控/data/web目录的变化(包括文件修改、创建、属性变化),并在变化发生时通过rsync命令将目录内容同步到远程服务器的/backup目录,并将同步的日志记录到/var/log/rsync.log文件中。脚本分解说明如下:
1. 设置变量:
path1:指定监控的本地目录为/data/web。
ip:指定远程服务器的IP地址为10.0.1.113。
2. 使用/usr/bin/inotifywait命令:
m:持续监控模式,保持程序运行直到被手动中断。
r:递归监控指定目录下的所有子目录。
q:安静模式,减少不必要的输出。
timefmt:定义时间格式。
format:指定输出格式,包含时间戳、监控目录层级和文件名。
e modify,create,attrib:监控文件的修改、创建和属性变化事件。
3. inotifywait的输出被读取到while循环中,每次循环处理一个发生变化的文件。
4. 在while循环内部:
使用rsync命令将/data/web目录及其内容同步到远程服务器10.0.1.113的/backup目录下。
使用echo命令将同步动作记录到日志文件/var/log/rsync.log中,同时通过2>&1将标准错误输出重定向到标准输出,确保所有日志信息都被写入日志文件。
这个脚本可以帮助实时监测并同步本地服务器/data/web目录的变更到远程服务器上。
rsync配置文件注解
tips:
这段配置是Rsync服务器(rsyncd)的配置文件片段,通常保存在 /etc/rsyncd.conf 中。以下是对各项配置的解释:
uid = rsync :设置rsync守护进程运行时的用户ID为rsync。
gid = rsync :设置rsync守护进程运行时的组ID为rsync。
port = 873 :指定rsync服务监听的TCP端口号,默认为873。
fake super = yes :让rsync模拟超级用户权限,以便在chroot环境下进行某些操作。
use chroot = no :禁止在处理每一个模块时切换到一个独立的根目录(chroot jail),这对于某些高级用途可能不适用。
max connections = 200 :设置rsync服务器最大并发连接数为200。
timeout = 300 :设置超时时间为300秒,超过这个时间的无活动连接将被断开。
pid file = /var/run/rsyncd.pid :指定rsync守护进程PID文件的位置。
lock file = /var/run/rsync.lock :指定rsync锁定文件的位置,用于多进程同步。
log file = /var/log/rsyncd.log :设置rsync服务器的日志文件路径。
ignore errors :忽略文件传输过程中的一些错误。
read only = false :设置rsync模块是否为只读,此处设置为可读写。
list = false :禁止列出模块信息。
hosts allow = 10.0.1.0/24 :允许来自10.0.1.0/24子网的客户端连接。
auth users = rsync_backup :定义允许连接的认证用户列表,此处为rsync_backup。
secrets file = /etc/rsync.password :设置密码文件,其中包含允许连接的用户名及其密码。
[backup] :定义一个名为"backup"的模块。
comment = "welcome to RSYNC backup" :为"backup"模块添加注释信息。
path = /backup :指定"backup"模块对应的本地文件系统路径为"/backup"。
总结:这是一个配置了rsync服务器允许来自10.0.1.0/24子网的客户端以用户rsync_backup的身份通过密码认证,访问本地"/backup"目录的配置文件。
具体实操演示
客户端
ip:10.0.1.113
mkdir p /data/web
cd /data
mkdir web
以上是我自己没有这个目录,你如果有的话,就不需要了,生产环境中应根据自己的需求更换目录的
rsync avz /data/web rsync_backup@10.0.1.111::backup password file=/etc/rsync.password 不需要密码
或者
rsync avz /etc/hosts rsync_backup@10.0.1.111::backup 需要密码
这里的密码是你配置的虚拟用户的密码
下载inotify tools
wget O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel 6.repo
yum y install inotify tools rsync
创建一个目录或者你可以找个地方存放脚本
cd /data/sh
mkdir p /data/sh
这里我有就不创建了
vi in.sh
#!/bin/bash
path1=/data/web
ip=10.0.1.113
/usr/bin/inotifywait mrq timefmt %y/%m'%d %H:/%M' format '%T %w%f' emodify,create,attrib $path1 | while read file;
do
rsync avz $path1 root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done
给予执行权限
chmod +x in.sh
bash in.sh
代码注解
#!/bin/bash
path1=/data/web
ip=10.0.1.111 填的是rsync灾备服务器ip
/usr/bin/inotifywait mrq timefmt %y/%m'%d %H:/%M' format '%T %w%f' emodify,create,attrib $path1 | while read file;
do
rsync avz $path1 root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done
emodify, 修改文件
create, 创建
attrib 修改属性
自己也可以定义一下删除,这里我们是备份并监控
delete操作
服务端
[root@web1 backup]# ls
data hosts passwd web www_2024 03 20.tar.gz
此时服务端应是已经配置过的服务端 因为它是灾备服务器 rsync都应该已经安装过来了
主要我们是在客户端写个脚本,监控他实时备份的过程
监控其他客户端的过程,实时的过程
效果测试:
客户端
此时你再打开一个窗口,开第二个窗口
用touch新建文件就可以了
再去看脚本的运行监控情况
[root@web02 ~]# cd /data/sh/
[root@web02 sh]# ls
backup.sh in.sh
[root@web02 sh]# bash in.sh
sending incremental file list
web/
web/1
web/10
web/100
web/11
web/12
web/13
web/14
web/15
web/16
web/17
web/18
web/19
web/2
web/20
web/21
web/22
web/23
web/24
web/25
web/26
web/27
web/28
web/29
web/3
web/30
web/31
web/32
web/33
web/34
web/35
web/36
web/37
web/38
web/39
web/4
web/40
web/41
web/42
web/43
web/44
web/45
web/46
web/47
web/48
web/49
web/5
web/50
web/51
web/52
web/53
web/54
web/55
web/56
web/57
web/58
web/59
web/6
web/60
web/61
web/62
web/63
web/64
web/65
web/66
web/67
web/68
web/69
web/7
web/70
web/71
web/72
web/73
web/74
web/75
web/76
web/77
web/78
web/79
web/8
web/80
web/81
web/82
web/83
web/84
web/85
web/86
web/87
web/88
web/89
web/9
web/90
web/91
web/92
web/93
web/94
web/95
web/96
web/97
web/98
web/99
web/{1.10}.txt
sent 5,039 bytes received 1,939 bytes 13,956.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,396 bytes received 17 bytes 2,826.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
web/
web/1.txt
web/10.txt
web/2.txt
web/3.txt
web/4.txt
web/5.txt
web/6.txt
web/7.txt
web/8.txt
web/9.txt
sent 1,938 bytes received 210 bytes 4,296.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,575 bytes received 17 bytes 3,184.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 1,058.67 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,575 bytes received 17 bytes 3,184.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,575 bytes received 17 bytes 3,184.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,571 bytes received 17 bytes 3,176.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,575 bytes received 17 bytes 3,184.00 bytes/sec
total size is 0 speedup is 0.00
服务端
[root@web1 ~]# cd /backup/web/
[root@web1 web]# ls
[root@web1 web]# ls
1 14 20 28 34 40 48 54 60 68 74 80 88 94
10 15 21 29 35 41 49 55 61 69 75 81 89 95
100 16 22 2.txt 36 42 4.txt 56 62 6.txt 76 82 8.txt 96
10.txt 17 23 3 37 43 5 57 63 7 77 83 9 97
11 18 24 30 38 44 50 58 64 70 78 84 90 98
{1.10}.txt 19 25 31 39 45 51 59 65 71 79 85 91 99
12 1.txt 26 32 3.txt 46 52 5.txt 66 72 7.txt 86 92 9.txt
13 2 27 33 4 47 53 6 67 73 8 87 93
注意:这个脚本运行时执行备份和实时同步传输 ,但是如果退出了,就不可以了,没办法实现监控和备份了,
bash in.sh &
[root@web02 sh]# bash in.sh &
[1] 3177
这个命令代表可以可以后台运行,他会给你个进程号
只要执行备份,你这边可以监控到的
演示效果
这两个客户端是一个哦,我分两个窗口打开的
客户端1
[root@web02 web]# touch nnwwqq{1..2}.txt
客户端2
[root@web02 sh]# bash in.sh &
[1] 3177
[root@web02 sh]# sending incremental file list
web/
web/nnwwqq1.txt
web/nnwwqq2.txt
sent 1,869 bytes received 58 bytes 3,854.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 1,790 bytes received 17 bytes 3,614.00 bytes/sec
total size is 0 speedup is 0.00
服务器端
[root@web1 web]# ls
1 17 26 35 44 53 62 71 80 9 9.txt nwq8.txt
10 18 27 36 45 54 63 72 81 90 nnwwqq1.txt nwq9.txt
100 19 28 37 46 55 64 73 82 91 nnwwqq2.txt
10.txt 1.txt 29 38 47 56 65 74 83 92 nwq10.txt
11 2 2.txt 39 48 57 66 75 84 93 nwq1.txt
{1.10}.txt 20 3 3.txt 49 58 67 76 85 94 nwq2.txt
12 21 30 4 4.txt 59 68 77 86 95 nwq3.txt
13 22 31 40 5 5.txt 69 78 87 96 nwq4.txt
14 23 32 41 50 6 6.txt 79 88 97 nwq5.txt
15 24 33 42 51 60 7 7.txt 89 98 nwq6.txt
16 25 34 43 52 61 70 8 8.txt 99 nwq7.txt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南