rsync+innotify实现实时同步
Rsyncserver:IP:192.168.5.129 /rsync CentOS release 6.9
Rsyncclient:IP:192.168.5.131 /rsync CentOS release 6.9
一、主服务器(server端,我这里是nginx)
其中主服务器需要安装rsync与inotify,主服务器作为server,向备份服务器client传输文件
1、安装rsync
1. [root@centos-1 ~]# cd /usr/local/src/
2. [root@centos-1 src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
3. [root@centos-1 src]# tar zxvf rsync-3.0.9.tar.gz
4. [root@centos-1 src]# cd rsync-3.0.9
5. [root@centos-1 rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
6. [root@centos-1 rsync-3.0.9]# make && make install
2、建立密码认证文件
1. [root@centos-1 rsync-3.0.9]# cd /usr/local/rsync/
2. [root@centos-1 rsync]# echo "redhat" >/usr/local/rsync/rsync.passwd
其中rsync-pwd可以自己设置密码,rsync.passwd名字也可以自己设置
1. [root@centos-1 rsync]# chmod 600 rsync.passwd
无论是为了安全,还是为了避免出现以下错误,密码文件都需要给600权限
1. password file must not be other-accessible
2. continuing without password file
3、安装inotify
1. [root@centos-1 rsync]# cd /usr/local/src/
2. [root@centos-1 src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
3. [root@centos-1 src]# tar zxvf inotify-tools-3.14.tar.gz
4. [root@centos-1 src]# cd inotify-tools-3.14
5. [root@centos-1 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
6. [root@centos-1 inotify-tools-3.14]# make && make install
4、创建rsync复制脚本
此项功能主要是将server端的目录/rsync里的内容,如果修改了(无论是添加、修改、删除文件)能够通过inotify监控到,并通过rsync实时的同步给client的/rsync里,下面是通过shell脚本实现的。
[root@centos-1 rsync]# vim rsync.sh
#!/bin/bash
host=192.168.5.131
src=/rsync/
des=web
user=root
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
最好把日志文件放在其他目录不然不经常提醒同步目录。
其中host是client的ip,src是server端要实时监控的目录,des是认证的模块名,需要与client一致,user是建立密码文件里的认证用户。
把这个脚本命名为rsync.sh,放到监控的目录里,比如我的就放到/tmp下面,并给予764权限
[root@centos-1 tmp]# chmod 764 rsync.sh
然后运行这个脚本
[root@centos-1 tmp]# sh /tmp/rsync.sh &
请记住,只有在备份服务器client端的rsync安装并启动rsync之后,在启动rsync.sh脚本,否则有时候会满屏出现:
rsync: failed to connect to 192.168.10.221: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
我们还可以把rsync.sh脚本加入到开机启动项里
[root@centos-1 tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local
二、备份服务器(client,我这里为nginx-backup)
1、安装rsync(备份服务器只安装rsync)
[root@centos-2 ~]# cd /usr/src/
[root@centos-2 src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[root@centos-2 src]# tar zxvf rsync-3.0.9.tar.gz
[root@centos-2 src]# cd rsync-3.0.9
[root@centos-2 rsync-3.0.9]# ./configure --prefix=/usr/local/rsync
[root@centos-2 rsync-3.0.9]# make
[root@centos-2 rsync-3.0.9]# make install
2、建立用户与密码认证文件
[root@centos-2 rsync-3.0.9]# echo "webuser:rsync-pwd" > /usr/local/rsync/rsync.passwd
请记住,在server端建立的密码文件,只有密码,没有用户名;而在备份服务端client里建立的密码文件,用户名与密码都有。
[root@centos-2 rsync]# chmod 600 rsync.passwd
需要给密码文件600权限
3、建立rsync配置文件
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web]
path = /rsync/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.5.129
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/rsync.passwd
其中web是server服务端里的认证模块名称,需要与主服务器里的一致,以上的配置我的自己服务器里的配置,以供参考。
把配置文件命名为rsync.conf,放到/usr/local/rsync/目录里
启动rsync
[root@centos-2 rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf
也可以安装xintd来接管一类网络服务在后台启动:接管了rsync
如果出现以下问题:
/usr/local/rsync/bin/rsync: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory,
可以采用下面方法解决
[root@centos-2 rsync]# whereis libiconv.so.2
libiconv.so: /usr/local/lib/libiconv.so.2 /usr/local/lib/libiconv.so
找到所需模块所在的目录,然后把此目录添加到/etc/ld.so.conf里,并更新库文件
[root@centos-2 rsync]# echo "/usr/local/lib/" >> /etc/ld.so.conf
[root@centos-2 rsync]# ldconfig
我们可以把rsync脚本加入到开机启动项里
[root@centos-2 rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf" >> /etc/rc.local
现在rsync与inotify在server端安装完成,rsync在备份服务器client端也安装完成
测试:
1、现在服务端创建1.txt到4.txt文件看看client端是否同步更新:
[root@centos-1 rsync]# ls
rsync.sh
[root@centos-1 rsync]# touch {1..4}.txt
[root@centos-1 rsync]# sending incremental file list
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=4/6)
2.txt
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=3/6)
3.txt
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=2/6)
4.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=1/6)
sent 246 bytes received 87 bytes 666.00 bytes/sec
total size is 397 speedup is 1.19
sending incremental file list
sent 99 bytes received 8 bytes 71.33 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
sending incremental file list
sent 99 bytes received 8 bytes 214.00 bytes/sec
total size is 397 speedup is 3.71
[root@centos-2 rsync]# ls
1.txt 2.txt 3.txt 4.txt rsync.sh
2、测试server删除1.txt文件client是否会删除同步/rsync目录:
[root@centos-1 rsync]# rm -rf 1.txt
[root@centos-1 rsync]# sending incremental file list
./
deleting 1.txt
sent 88 bytes received 11 bytes 198.00 bytes/sec
total size is 397 speedup is 4.01
[root@centos-2 rsync]# ls
2.txt 3.txt 4.txt rsync.sh
3、在server端2.txt文件里面写入文件测试是不是会同步
转载于:https://blog.51cto.com/legehappy/1965479
rsync+innotify实现实时同步相关推荐
- rsync+inotify实现实时同步案例详解
rsync+inotify实现实时同步案例详解 转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也 ...
- MySQL :rsync+sersync文件实时同步
文章目录 1. rsync是什么 rsync+sersync文件实时同步 一:环境 二:备份服务器操作 三:数据源服务器操作 安装rsync客户端软件 安装sersync工具,实时触发rsync进行同 ...
- Rsync+inotify实现实时同步
如果有一天去接受面试:面试官问你一个1t的文件需要进行传输,问题用何种方法.假如你答了scp或者ftp.那么你挂的几率是很大的 一.inotify介绍 inotify-tools有两个命令 //ino ...
- rsync + inotify 实现实时同步
rsync + inotify 实现实时同步 rsync 简介 Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.并且可以不进行改变原有数据的属性信息,实现 ...
- 使用rsync实现数据实时同步备份--实战
实战3: 配置rsync 服务器及需要备份的目录,不使用系统用户需要自己创建自己的配置文件: /etc/rsyncd.conf ; 创建备份帐户,最后把rsync 以daemon方式运行. 配置文件: ...
- rsync+inotify实现实时同步案例--转
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
- rsync同步操作 inotify实时同步 cobbler装机平台 DNS主从结构
目录 rsync同步操作 rsync基本使用 rsync+ssh同步:远程同步 实时同步 inotify实时同步 inotifywait监控 cobbler装机平台 cobbler简介 基本概念: 装 ...
- 【Rsync + inotify】 实时同步远程服务器目录文件
文章目录 服务端 安装.配置 rsync 客户端 安装rsync 使用rsysnc 实现 实时同步 如何排错??? 服务端 安装.配置 rsync centos: # yum install rsyn ...
- rsync+inotify实现实时同步案例
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输.如果文件数 ...
最新文章
- Python 常见优化技巧,让你的程序溜溜的跑起来!
- curl 请求没反应_理解Redis的反应堆模式
- 事编计算机网络管理专技如何评级,事业单位管理和专技岗位有什么区别?哪个有前途?...
- js正则表达式之人民币匹配
- 如何开启linux的dhcp服务器,怎么开启DHCP服务器啊
- (58)UART外设驱动用户发送模块(三)(第12天)
- JS 逻辑中断(二)
- js如何获取php中的变量的类型,js获取变量的类型
- 23_多易教育之《yiee数据运营系统》OLAP平台-整体概述篇
- set-cookie无法存入到浏览器cookie问题汇总
- Android App赞赏功能,微信公众号赞赏功能升级:作者可直接收到赞赏, iOS安卓均可用...
- 免费网站(视频,PPT,静态图片,动态图片,壁纸)
- java protected用法_深入理解Java的protected修饰符
- Win10怎么设置每天自动开机
- 微信大数据挑战赛方案总结
- 《云计算架构技术与实践》连载(2):1.2 云计算的发展趋势
- mysql英文版数据库备份方法_mysql数据库太大了如何备份与还原
- google学术爬虫
- Java实体类映射工具MapStruct
- 一篇关于串口的经典文章