rsync+inotify服务器同步解决方案.

先来一张图说明公司的服务器间数据同步方案

由于采用了程序写日志和推数据,有些时候会出现,日志给同步过去了,但是数据却没有给同步过去,当然这只是极个别的现象;但有时候排查某个没有通过过来的文件或者目录的话也是一件比较痛苦的事情,特别是当文件级别达到几百上千甚至更多时。

为此,老大让出一个解决方案,最好是实时的数据同步。因此根据本公司的实际情况,同步的文件体积不大、数量不是太多(一台服务器每天也就是几百个文件)的情况,提出了如下的解决方案,欢迎大家支出缺点和建议。

下图是解决方案拓扑图,红色虚线作为分隔,本文仅介绍红线左边的配置实例,具体的拓展可留作大家自行实验。

一、rsync服务器端配置和使用1、查看服务器内核版本是否高于2.6.13(为后续安装inotify-tools做准备)[root@localhost ~]# cat /etc/redhat-releaseCentOS release 6.4 (Final)[root@localhost ~]#[root@localhost ~]# uname -r2.6.32-358.el6.x86_642、查看内核对inotify API的支持[root@localhost ~]# ls /proc/sys/fs/inotify/ -l总用量 0-rw-r–r– 1 root root 0 3月 13 15:44 max_queued_events-rw-r–r– 1 root root 0 3月 13 15:44 max_user_instances-rw-r–r– 1 root root 0 3月 13 15:44 max_user_watches开始部署rsync服务器端

3、首先确认rsync软件是否安装[root@localhost ~]# rpm -qa rsync(默认已安装)rsync-3.0.6-9.el6.x86_643.0相对于2.0有很多改进,3.0是边比对边同步,2.0是对比完之后再同步4、安装的话可采用yum,也可采用源码编译安装yum -y install rsync5、安装完成之后默认是没有配置文件的,需要根据实际的情况自己创建vim /etc/rsyncd.conf##########rsyncd.conf start###############uid = rsyncgid = rsyncuse chroot = nomax connections = 200timeout = 300pid file = /var/run/rsyncd.pidlock file = /var/run/rsyncd.locklog file = /var/log/rsyncd.log[test]path=/test/ignore errosread only = falselist = falsehosts allow=192.168.137.0/24hosts deny=*auth users = rsync_backupsecrets file = /etc/rsync.password##########rsyncd.conf end##################配置文件的相关说明uid = rsync赋予相应的用户角色gid = rsyncuse chroot = nomax connections = 200多少人并发往服务器上传数据timeout = 300服务器会话超时时间pid file = /var/run/rsyncd.pid 进程启动后,进程号存放路径lock file = /var/run/rsyncd.lock 服务启动、停止会用到锁文件log file = /var/log/rsyncd.log 日志[test]模块,其名称可以任意写,不过一般是以路径命名,最好见名意path=/test/ rsync服务端的一个路径,客户端存放或者从客户端下载ignore errosread only = false 只读为假,可往此目录上传文件,否则只能下载文件list = false列表为假,列表可见hosts allow=192.168.137.0/24 允许谁来连接hosts deny=*auth users = rsync_backup 授权用户(虚拟用户)secrets file = /etc/rsync.password 上面虚拟用户的密码文件6、windows下的文件传到UNIX里面最好格式化一下,避免出现不必要的格式错误,导致配置文件混乱;尤其是脚本[root@localhost ~]# dos2unix /etc/rsyncd.confdos2unix: converting file /etc/rsyncd.conf to UNIX format …7、创建对外共享的目录[root@localhost ~]# mkdir -pv /testmkdir: 已创建目录 “/test”8、创建系统用户,不创建家目录[root@localhost ~]# useradd rsync -s /sbin/nologin -M9、授权共享目录(授权为配置文件中uid参数对应的属主和属组)[root@localhost ~]# chown -R rsync.rsync /test/10、写在配置文件里,以用户名:密码的格式,并以明文存放注:密码文件可以随意指定[root@localhost ~]# echo “rsync_backup:redhat” >>/etc/rsync.password11、修改密码文件的权限,使得其他用户无权查看[root@localhost ~]# chmod 600 /etc/rsync.password12、启动服务[root@localhost test]# rsync –daemon –config=/etc/rsyncd.conf[root@localhost ~]#[root@localhost ~]# lsof -i tcp:873COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMErsync 1968 root 4u IPv4 391690t0 TCP *:rsync (LISTEN)rsync 1968 root 5u IPv6 391700t0 TCP *:rsync (LISTEN)[root@localhost ~]#[root@localhost ~]# ps -ef |grep rsyncroot19681 0 16:33 ?00:00:00 rsync –daemonroot1971 1641 0 16:34 pts/0 00:00:00 grep rsync[root@localhost ~]#[root@localhost ~]#[root@localhost ~]# whereis rsyncrsync: /usr/bin/rsync /etc/rsync.password /usr/share/man/man1/rsync.1.gz[root@localhost ~]#[root@localhost ~]# echo “/usr/bin/rsync” >> /etc/rc.local客户端配置(只需要两步即可)1、设置密码,必须同服务器端密码配置文件一样[root@localhost ~]# echo “redhat” >> /etc/rsync.password[root@localhost ~]# cat /etc/rsync.passwordredhat2、设置密码文件权限,其他用户无权查看[root@localhost ~]# chmod 600 /etc/rsync.password客户端测试1、下载(取)同步rsync服务器端的test模块下的所有文件到本机的test目录下[root@localhost test]# rsync -avz rsync_backup@192.168.137.3::test –password-file=/etc/rsync.password /test/receiving incremental file list./1.txt2.txt3.txt4.txt5.6.txtsent 160 bytes received 368 bytes 1056.00 bytes/sectotal size is 47 speedup is 0.09再次测试:[root@localhost test]# rsync -avz rsync_backup@192.168.137.3::test –password-file=/etc/rsync.password /test/receiving incremental file list./1.tar.gzsent 84 bytes received 397 bytes 962.00 bytes/sectotal size is 187 speedup is 0.392、拉(上传)[root@localhost test]# rsync -avz /test/test.tar.gz root@192.168.137.3:/testroot@192.168.137.3’s password:sending incremental file listtest.tar.gzsent 220 bytes received 31 bytes 21.83 bytes/sectotal size is 140 speedup is 0.56rsync客户端推送遇到问题:[root@localhost test]# rsync -avz /etc/issue /etc/fstab rsync_backup@192.168.137.3::test –password-file=/etc/rsync.passwordrsync: failed to connect to 192.168.137.3: No route to host (113)rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6][root@localhost test]# rsync -avz /etc/issue rsync_backup@192.168.137.3::test –password-file=/etc/rsync.passwordrsync: failed to connect to 192.168.137.3: No route to host (113)rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]解决方法:rsync服务端防火墙设置,开放873端口或者关闭防火墙

生活中最基本的技巧是交流,最可依赖的品质是耐心,

rsync+inotify服务器同步解决方案.

相关文章:

你感兴趣的文章:

标签云: