------同步源----------发起端---------

   192.168.1.1    192.168.1.2

关闭iptables:iptables -F

         /etc/init.d/iptables save

1:确定备份源

[root@localhost ~]# mkdir /www

[root@localhost ~]# touch /www/source{1..100}.html

[root@localhost ~]# rpm -qa | grep rsync

[root@localhost ~]# 提示没有可以官网下载,或者yum -y install rsync

创建备份账号文件↓

[root@localhost ~]# vim /etc/user.db

添加:

benet:redhat

[root@localhost ~]# chmod 600 /etc/user.db //必须设置为600,否则客户端认证失败。

创建rsync配置文件,本身就没有,需要手动创建!

[root@localhost ~]# vim /etc/rsyncd.conf

添加

uid = nobody

gid = nobody

use chroot = yes

address = 192.168.1.1

port 873

log file = /var/log/rsyncd.log

pid file = /var/run/rsyncd.pid

hosts allow = 192.168.1.0/24

[www]

path = /www

comment = backup

read only = yes

dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z

auth users = benet

secrets file = /etc/user.db

启动(停止)rsync

[root@localhost ~]# rsync --daemon

最好使用:rsync --daemon --config=/etc/rsyncd.conf

[root@localhost ~]# netstat -anput | grep 873

显示有rsync进程在运行

[root@localhost ~]# kill -9 PID号 或者killall -s QUIT rsync  后者柔和退出,前者暴力退出

注意:如果重启失败,先删除pid文件,然后再启动。

[root@localhost ~]# rm -rf /var/run/rsyncd.pid

[root@localhost ~]# rsync --daemon --config=/etc/rsyncd.conf

[root@localhost ~]# netstat -anput | grep rsync

[root@localhost ~]# 

开始验证:

(1)本地验证:(类似复制)

[root@localhost ~]# rsync -rl /etc/httpd/ /tmp/

(2)发起端验证:(把同步源的重要文件同步到发起端,作为备份)

发起端操作↓

[root@localhost ~]# mkdir /www

[root@localhost ~]# rsync -avz [email protected]::www /www/

增量备份:

[root@localhost ~]# rsync -avz --delete [email protected]::www /www/

OK,下行同步(类似于下载)我们已经完成了,接下来我们开始做上传↓

[root@localhost ~]# touch /www/fqd1

[root@localhost ~]# rsync -avz --delete /www/* [email protected]::www

我们看到,新创建的fqd1文件上传成功至同步源中

注意:做如上操作之前

1:同步源中的rsyncd.conf要把read only 改写成 no; //意思开放写入权限

2:同步源中/www/的权限改为757 chmod 757 /www/    //意思给其他用户7的权限


接下来我们做计划任务来自动远程同步

在发起端进行如下操作:↓

[root@localhost ~]# vim /etc/benet.password

redhat

[root@localhost ~]# chmod 600 /etc/benet.password

[root@localhost ~]# crontab -e 

*/1 * * * * /usr/bin/rsync -avz --delete --password-file=/etc/benet.password [email protected]:www /www

[root@localhost ~]# service crond restart

[root@localhost ~]# chkconfig crond on

以上部分是不是小菜一碟,下面我们开始比较实用的部分,rsync+inotify实时同步(在发起端进行设置)

1、调整内核参数

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events

16384

[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances

1024

[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches

1048576

[root@localhost ~]# vim /etc/sysctl.conf

添加:

fs.inotify.max_queued_events = 16384 //事件队列

fs.inotify.max_user_instances = 1024 //实例数

fs.inotify.max_user_watches = 1048576 //每个实例文件数量

[root@localhost ~]# sysctl -p

2、发起端安装inotify软件

[root@localhost ~]# tar -zxvf inotify-tools-3.14.tar.gz

[root@localhost ~]# cd inotify-tools-3.14/

[root@localhost ~]# ./configure &&make &&make install

每个命令成功执行,会自动执行下一条命令,需要gcc编译环境,自己yum装吧,相信看到该篇文章的读者都是有了一定基础的!^_^

3、验证监控效果


[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html

在另一个终端上对该目录进行增删改查的操作,将会看到效果。

[root@localhost ~]# 同步源端重启下服务,

4、发起端编写触发式同步脚本

[root@localhost ~]# vim /etc/hehe.password

添加

redhat  //benet用户的密码

[root@localhost ~]# chmod 600 /etc/hehe.password

[root@localhost ~]# vim test.sh

#!/bin/bash

INOTIFY="inotifywait -mrq -e modify,create,move,delete /www/"

RSYNC="rsync -avz --delete --password-file=/etc/hehe.password /www/ [email protected]::www"

$INOTIFY | while read DIRECTORY EVENT FILE

do

if [ $(pgrep rsync | wc -l) -le 0 ]:then

$RSYNC

fi 

done

[root@localhost ~]# chmod +x test.sh

[root@localhost ~]# ./test.sh &> /dev/null &

注意,做该实验前,确保两机联通,关闭防火墙,时间同步!

上面的脚本监控了/www/目录,清在该目录下做增删改查操作,再看看同步源是否同步成功!


最后欢迎您的阅读,本人才学疏浅,如有不足之处,望各位体谅,将问题发送到本人邮箱[email protected]!同时欢迎您加入我们的技术讨论组qq群:566121592