实验环境:
1.两台虚拟机,同一网卡2.inotify源码包文件
模拟要求:
1,再网站建立初期进行手工同步2,编写定期同步,每天23:30分数据同步
3,结合inotify+rsync,实时同步
一,上行同步:客户端同步服务器内容
1.服务器,客户端关闭防火墙,slinux,配置地址保证两台虚拟机ping通(服务器:192.168.1.1,客户端:192.168.1.2)
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# ifconfig ens33 192.168.1.1
2.模拟http环境
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# echo 'hello world' >> /var/www/html/1.html
[root@localhost ~]# echo 'hello world' >> /var/www/html/2.html
3.修改/etc/rsyncd.conf配置文件
[root@localhost ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes #禁锢在源目录
address = 192.168.1.1 #监听地址
port 873 #监听端口
# max connections = 4
log file = /var/log/rsyncd.log #日志文件
pid file = /var/run/rsyncd.pid #存放进程ID文件位置
hosts allow = 192.168.1.0/24 #允许访问客户端地址
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
[aaa] #共享模块名
path = /var/www/html #源目录的实际目录
comment = aaa #提示
read only = yes #是否只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #同步是不
再压缩文件
auth users = myx #授权用户
secrets file = /etc/rsyncd_users.db #存放账户信息文件
4.创建备份账户的数据文件设置权限
[root@localhost ~]# echo "myx:123456" > /etc/rsyncd_users.db
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db
[root@localhost ~]# ll /etc/rsyncd_users.db
-rw-------. 1 root root 11 7月 20 09:31 /etc/rsyncd_users.db
5.启动服务
[root@localhost ~]# rsync --daemon
6.客户机模拟http环境测试同步
rsync备份工具:
选项:
-a:归档模式-v:显示同步过程详细
-z:在传输文件时压缩
-H:保留硬链接文件
--delete:删除目标位置有原位置没有的文件
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# rsync -avzH --delete myx@192.168.1.1::aaa /var/www/html/
Password:
receiving incremental file list
./
1.html
2.html
sent 93 bytes received 235 bytes 93.71 bytes/sec
total size is 24 speedup is 0.07
[root@localhost ~]#
7.查看结果
[root@localhost ~]# ll /var/www/html/
总用量 8
-rw-r--r--. 1 root root 12 7月 20 09:42 1.html
-rw-r--r--. 1 root root 12 7月 20 09:42 2.html
二,计划同步
1.创建密码文件并分配权限
[root@localhost ~]# echo "123456" > /etc/server.pass
[root@localhost ~]# chmod 600 /etc/server.pass
2.使用crontab进行计划同步
[root@localhost ~]# crontab -e
30 23 * * * /usr/bin/rsync -avzh --delete --password-file=/etc/server.pass myx@192.168.1.1::aaa /var/www/html/
三,下行同步:服务器同步客户端内容
1.调整inotify参数
[root@localhost ~]# vim /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.inotify.max_queued_events=16384
fs.inotify.max_user_instances=1024
fs.inotify.max_user_watches=1048576
:wq
[root@localhost ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost ~]#
2.安装inotify-tools
[root@localhost ~]# tar zxf /mnt/inotify-tools-3.14.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure && make && make install
3.测试监控文件,观察变化
inotify监控,汇总改动情况
选项:
-e:指定监控那些事件-m:持续监控
-r:表示递归整个目录
-q:简化输出信息
inotifywait -mrq -e modify,create,move,delete /var/www/html
4.为服务器目录同步赋予写入权限和rsync配置文件
[root@localhost ~]# chmod a+w /var/www/html/
[root@localhost ~]# vim /etc/rsyncd.conf
read only = no
[root@localhost ~]# killall rsync
[root@localhost ~]# rsync --daemon
5.编写自动同步脚本
[root@localhost ~]# vim inotify.sh
#!/bin/bash
inotify="inotifywait -mrq -e modify,create,move,delete /var/www/html"
rsync="rsync -avzh --delete --password-file=/etc/server.pass /var/www/html/ myx@192.168.1.1::aaa"
$inotify | while read DIRECTORY EVENT FILE
do
$rsync
done
[root@localhost ~]# . inotify.sh
6.进行测试
在客户端创建文件,查看服务器是否同步
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# touch 233.txt
此实验到此结束,如果有问题请大家留言,我会尽快改正,此实验需要有一定基础