您的位置:首页 > 其它

rsync远程同步+inotify-tools自动检测笔记

2017-02-17 09:58 555 查看
构建rsync远程同步

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

192.168.1.1 192.168.1.10

1、配置IP地址并保证互通

2、确定备份源

[root@localhost ~]# mkdir /www

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

3、创建备份账号

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

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

添加:

hehe:123.com

4、创建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
第13章:rsync远程同步.txt[2015/8/5 16:31:06]
comment = hehe

read only = yes

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

auth users = hehe

secrets file = /etc/rsyncd_users.db

5、启动(终止)rsync

[root@localhost ~]# rsync --daemon

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

[root@localhost ~]# rsync --daemon

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

[root@localhost ~]# kill -9 PID号(例如:kill -9 9290)

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

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

6、验证:

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

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

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

[root@localhost ~]#mkdir /www

[root@localho
4000
st ~]#rsync -avz hehe@192.168.1.1::www /www/

或者

[root@localhost ~]#rsync -avz rsync://hehe@192.168.1.1/www /www/

增量:

[root@localhost ~]#rsync -avz --delete hehe@192.168.1.1::www /www/

注:之前学习的内容为下行同步(类似于下载)
第13章:rsync远程同步.txt[2015/8/5 16:31:06]
上行同步:(类似于上传)

[root@localhost ~]# rsync -avz --delete /www hehe@192.168.1.1::www

注意:

1、同步源中的权限:要把 read only 改写成 no;

2、同步源中/www/的权限改为757

7、设置计划任务自动远程同步

在发起端作如下操作:

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

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

[root@localhost ~]# crontab -e root

添加:

30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/hehe.password

hehe@192.168.1.1::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

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

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

[root@localhost ~]#vim /etc/sysctl.conf
第13章:rsync远程同步.txt[2015/8/5 16:31:06]
添加:

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 -C /usr/src/

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

[root@localhost inotify-tools-3.14]#./configure && make && make install

3、验证监控效果

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

在另一个终端上进行增删改查的操作。

4、编写触发式同步脚本

[root@localhost ~]# vim inotify_rsync.sh

添加:

#!/bin/bash

INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"

RSYNC_CMD="rsync -azH --delete --password-file=/etc/hehe.password /var/www/html/

hehe@192.168.1.1:/var/www/html"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE

do

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

$RSYNC_CMD

fi

done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: