使命需求
1.对NFS服务器上的静态资源实时备份(inotify+rsync)
主机列表
- # 外网地址 内网地址 主机名
- 192.168.122.207 172.16.1.207 web-test-209
- 192.168.122.231 172.16.1.231 nfs-test-231
- 192.168.122.241 172.16.1.241 rsync-test-241
复制代码 架构图
开始实操
1.在rsync-test-241服务器上搭建rsync
1.1.安装rsync
- [root@rsync-test-241 ~]# yum install rsync -y
复制代码 1.2.修改设置文件/etc/rsyncd.conf
- [root@rsync-test-241 ~]# vim /etc/rsyncd.conf
- [root@rsync-test-241 ~]# cat /etc/rsyncd.conf
- uid = rsync
- gid = rsync
- fake super = yes
- use chroot = no
- max connections = 200
- 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 = 172.16.1.0/24
- hosts deny = 0.0.0.0/32
- auth users = rsync_backup
- secrets file = /etc/rsync.password
- [backup-nfs]
- comment = This is nfs backup!
- path = /backup/
复制代码 设置文件解读
设置参数参数阐明uid = rsync指定rsync服务运行的时间,向磁盘进行读取和写入操作的操作者gid = rsync指定rsync服务运行的时间,向磁盘进行读取和写入操作的操作者use chroot = no进行数据同步存储时,安全相干参数,默认内网进行数据同步,可以关闭max connections = 200定义向备份服务器进行数据存储的并发连接数timeout = 300定义与备份服务器建立的网络连接,在多长时间没有数据传输时,就释放连接pid file = /var/run/rsyncd.pid服务程序运行时,会将进程的pid信息存储到一个指定的pid文件中lock file = /var/run/rsync.lock定义锁文件,重要用于配合max connections 参数,当达到最大连接就禁止继续访问log file = /var/log/rsyncd.log定义服务的日记文件生存路径信息[backup]指定备份目录的模块名称信息path = /backup指定数据进行备份的目录信息ignore errors在进行数据备份传输过程过程中,忽略一些I/O产生的传输错误read only = false设置对备份的目录的具有读写权限,即将只读模式进行关闭list = false确认是否可以将服务设置的模块信息,在客户端可以查看表现hosts allow = 172.16.1.0/24设置备份目录答应进行网络数据备份的主机地址或网段信息,即设置白名单hosts deny = 0.0.0.0/32设置备份目录禁止进行网络数据备份的主机地址或网段信息,即设置黑名单auth users = rsync_backup指定访问备份数据目录的认证用户信息,为虚制定义的用户,不需要进行创建secrets file = /etc/rsync.password设置访问备份数据目录进行认证用户的密码文件信息,会在文件中设置认证用户密码信息[backup]指定模块名称,便于日后维护path=/backup在当前模块中,Daemon利用的文件系统或目录,注意目录权限和设置文件权限不停,防止读写出问题#exclude=清除文件或目录,相对路径[ftp]还可以添加其他模块1.3.根据设置文件里定义的信息,创建用户,文件等
创建用户- [root@rsync-test-241 ~]# useradd rsync -s /sbin/nologin -M
- [root@rsync-test-241 ~]# id rsync
- uid=1000(rsync) gid=1000(rsync) 组=1000(rsync)
复制代码 创建目录,修改属性- [root@rsync-test-241 ~]# mkdir /backup
- [root@rsync-test-241 ~]# chown -R rsync.rsync /backup/
- [root@rsync-test-241 ~]# ls -ld /backup/
- drwxr-xr-x 2 rsync rsync 6 11月 15 16:19 /backup/
复制代码 创建认证文件,授权- [root@rsync-test-241 ~]# echo "rsync_backup:mima666" > /etc/rsync.password
- [root@rsync-test-241 ~]# cat /etc/rsync.password
- rsync_backup:mima666
- [root@rsync-test-241 ~]# chmod 600 /etc/rsync.password
- [root@rsync-test-241 ~]# ll /etc/rsync.password
- -rw------- 1 root root 21 11月 15 16:22 /etc/rsync.password
复制代码 1.4.启动rsync服务,开机自启
- [root@rsync-test-241 ~]# systemctl start rsyncd
- [root@rsync-test-241 ~]# systemctl enable rsyncd
- Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
复制代码 1.5.检查rsync
- [root@rsync-test-241 ~]# systemctl status rsyncd
- ● rsyncd.service - fast remote file copy program daemon
- Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
- Active: active (running) since 五 2024-11-15 16:24:12 CST; 41s ago
- Main PID: 2411 (rsync)
- CGroup: /system.slice/rsyncd.service
- └─2411 /usr/bin/rsync --daemon --no-detach
- 11月 15 16:24:12 rsync-test-241 systemd[1]: Started fast remote file copy program daemon.
- [root@rsync-test-241 ~]#
- [root@rsync-test-241 ~]# ps -ef|grep rsync
- root 2411 1 0 16:24 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
- root 2450 1928 0 16:26 pts/1 00:00:00 grep --color=auto rsync
- [root@rsync-test-241 ~]# netstat -tnlp | grep rsync
- tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2411/rsync
- tcp6 0 0 :::873 :::* LISTEN 2411/rsync
- [root@rsync-test-241 ~]#
复制代码 2.在nfs-test-231服务器上运行rsync
2.1安装rsync
- [root@nfs-test-231 ~]# yum install rsync -y
复制代码 2.2.创建密码文件,只写密码即可
- [root@nfs-test-231 ~]# echo 'mima666' > /etc/rsync.password
- [root@nfs-test-231 ~]# cat /etc/rsync.password
- mima666
复制代码 2.3.必须要给密码文件授权,去掉other的权限,否则rsync会报错
- [root@nfs-test-231 ~]# chmod 600 /etc/rsync.password
- [root@nfs-test-231 ~]# ll /etc/rsync.password
- -rw------- 1 root root 8 11月 15 16:34 /etc/rsync.password
复制代码 2.4.测试rsync数据同步是否精确
client > server 、数据推送- [root@nfs-test-231 ~]# rsync -avzP network_init.sh rsync_backup@172.16.1.241::backup-nfs --password-file=/etc/rsync.password
- sending incremental file list
- network_init.sh
- 496 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)
- sent 381 bytes received 35 bytes 277.33 bytes/sec
- total size is 496 speedup is 1.19
复制代码- -avzP
- -a 保持文件原有属性
- -v 显示传输细节情况
- -z 对传输数据压缩传输
- -P 显示文件传输的进度信息
复制代码- 也可以直接使用密码变量,进行同步
- tail -1 /etc/bashrc
- export RSYNC_PASSWORD=chaoge
复制代码 验证rsync服务端接收到了文件- [root@rsync-test-241 ~]# cd /backup/
- [root@rsync-test-241 backup]# ls
- network_init.sh
- [root@rsync-test-241 backup]#
复制代码 3.在nfs-test-231服务器上摆设inotify
3.1.安装inotify-tools
- [root@nfs-test-231 ~]# yum install inotify-tools -y
复制代码 3.2.编写脚本,完成数据同步
检测共享文件夹,只要有了数据变化,立即触发rsync备份
[root@nfs-test-231 ~]# vim rsync_nginx.sh
[root@nfs-test-231 ~]# cat rsync_nginx.sh- #!/bin/bash
- /usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /nfs-web-share/ | while read line
- do
- rsync -a --delete /nfs-web-share/ rsync_backup@172.16.1.241::backup-nfs --password-file=/etc/rsync.password
- echo "`date +%F\ %T`出现事件$line" >> /var/log/rsync.log 2>&1
- done
- [root@nfs-test-231 ~]#
复制代码 3.3.执行脚本,放入后台运行
- [root@nfs-test-231 ~]# bash rsync_nginx.sh &
- [1] 13040
- [root@nfs-test-231 ~]# jobs
- [1]+ 运行中 bash rsync_nginx.sh &
- [root@nfs-test-231 ~]#
复制代码 4.验证同步情况
4.1.在web-test-207服务器上更新文件
- [root@web-test-207 ~]# cd /usr/share/nginx/html/
- [root@web-test-207 html]# vim index.html
- [root@web-test-207 html]# cat index.html
- <meta charset=utf8> 这是一个网页
- hello world
- <h1>其实我来自nfs服务端</h1>
- <h2>哈哈哈,你好inotify</h2>
- [root@web-test-207 html]#
复制代码 4.2.在nfs-test-231上查看同步日记
- [root@nfs-test-231 ~]# tail -f /var/log/rsync.log
- 2024-11-15 17:28:11出现事件/nfs-web-share/ MODIFY index.html
- 2024-11-15 17:28:11出现事件/nfs-web-share/ MODIFY index.html
- 2024-11-15 17:28:12出现事件/nfs-web-share/ ATTRIB index.html
复制代码 4.3.在rsync-test-241上查看是否同步
- [root@rsync-test-241 backup]# pwd
- /backup
- [root@rsync-test-241 backup]# ll
- 总用量 4
- -rw-r--r-- 1 rsync rsync 123 11月 15 17:28 index.html
- [root@rsync-test-241 backup]# cat index.html
- <meta charset=utf8> 这是一个网页
- hello world
- <h1>其实我来自nfs服务端</h1>
- <h2>哈哈哈,你好inotify</h2>
- [root@rsync-test-241 backup]#
复制代码- 如果过程里出现了失败,大部分原因是
- 1.防火墙未关
- 2.nfs配置权限不对
- 3.网络不通
- 其它情况看报错信息排查
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |