Rsync+inotify数据同步及常见的问题

简介:

今天在两台服务器同步备份在用户权限上纠结了很多,主要关于这个问题网上的配置方法不一,源自rsync版本不一致。

 

Rsync 版本

[root@mail video]# rsync –version 
rsync  version 3.0.6  protocol version 30 
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others. 
Web site: rsync.samba.org 
Capabilities: 
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, 
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, 
    append, ACLs, xattrs, iconv, no symtimes

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you 
are welcome to redistribute it under certain conditions.  See the GNU 
General Public Licence for details.

服务器同步任务需求
  • 服务器A与服务器B同步备份,这里只说明服务器A同步到服务器B,服务器B还原到服务器A。
  • 考虑安全因素,使用普通用户进行同步。
  • 使用cronjob,定时同步。
错误提示

错误发生在rsync 3.0.6版本,64位 CentOS5.5 系统。

首页这篇文章主要解决的错误是以下:

 

 
  1. @ERROR: auth failed on module ***   
  2. rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]  
  3. *** 是你/etc/rsyncd.conf 中配置  

*** 是你/etc/rsyncd.conf 中配置的模块,我这里用

 

 
  1. password file must not be other-accessible   
  2. continuing without password file   
  3. Password:   
  4. @ERROR: auth failed on module ***   
  5. rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]  

 

Rsync 配置

#vi /etc/rsyncd.conf

 

 
  1. uid = nobody   
  2. gid = nobody   
  3. max connections = 4   
  4. read only = true   
  5. #hosts allow = 202.207.177.180   
  6. hosts allow = *   
  7. transfer logging = true   
  8. log format = %h %o %f %l %b   
  9. log file = /var/log/rsyncd.log   
  10. slp refresh = 300   
  11. log file = /var/log/rsyncd.log   
  12. pid file = /var/run/rsyncd.pid   
  13. lock file = /var/run/rsyncd.lock  
  14.  
  15. [web]   
  16. path = /home/admin/public_html   
  17. comment = Mirror to Hk server   
  18. read only = true   
  19. list = false   
  20. auth users = lixiphp 
  21.  
  22. [test]   
  23. path = /home/admin/domains/test   
  24. read only = false   
  25. auth users = lixiphp   
  26. secrets file = /etc/rsyncd.secrets  
配置普通用户密码

 

 
  1. [root@mail video]# vi /etc/rsyncd.secrets  

格式为: username:password

 

 
  1. rsync_user:rsyncofpass  

设置权限为只读:

 

 
  1. chmod 600 /etc/rsyncd.secrets  
首次启动rsync

 

 
  1. rsync –daemon –config=/etc/rsyncd.conf  

如果提示

 
  1. failed to create pid file /var/run/rsyncd.pid: File exists  

使用指令

 

 
  1. rm -rf /var/run/rsyncd.pid  
重启已经在运行的rsync

 

 
  1. [root@mail video]# ps -ef | grep rsync   
  2. root     27284     1  0 10:26 ?        00:00:00 rsync –daemon –config=/etc/rsyncd.conf   
  3. root     30516 29986  0 18:35 pts/3    00:00:00 grep rsync   
  4. [root@mail video]# kill -9 27284   
  5. [root@mail video]# rsync –daemon –config=/etc/rsyncd.conf  

这样服务器A配置成功!

服务器B配置

一般错误都会发生在服务器B,注意这部分的讲解!

通过CentOS yum install rsync,安装rsync服务。

在rsync安装之后,运行以下指令同步备份:

 

 
  1. rsync -vzrtopg –progress –delete –password-file=/home/admin/admin_backups/password.rsync rsync://lixiphp@203.171.237.245/test  /home/admin/admin_backups/test  

地址rsync://lixiphp@203.171.237.245/test,lixiphp为服务器A用户,203.171.237.245服务器A IP地址或者域名 test为服务器A配置模块

密码存放在/home/admin/admin_backups/password.rsync,这里存放位置,可自由安排。

password.rsync内容格式为: password

 

 
  1. rsyncofpass  

设置权限为只读:

 

 
  1. chmod 600 /home/admin/admin_backups/password.rsync  
解决错误

用户密码错误

 
  1. @ERROR: auth failed on module test   
  2. rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]  

检查服务器A存储密码文件和服务器B密码文件。

  • 服务器A密码文件 /etc/rsyncd.secrets 格式为: username:password
  • 服务器B密码文件 password.rsync 格式为:password

文件权限错误

 

 
  1. password file must not be other-accessible   
  2. continuing without password file   
  3. Password:   
  4. @ERROR: auth failed on module ***   
  5. rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]  

检查服务器A存储密码文件和服务器B密码文件。

  • 服务器A密码文件 /etc/rsyncd.secrets 权限为600: chmod 600 /etc/rsyncd.secrets
  • 服务器B密码文件 password.rsync 权限为600:chmod 600 password.rsync
定时任务

 

 
  1. [root@hk admin_backups]# vi backup.sh  

内容如下:

 
  1. #/bin/sh   
  2. rsync -vzrtopg –progress –delete –password-rsyncfile=/home/admin/admin_backups/password.rsync ://lixiphp@203.171.237 

添加定时任务:

 

 
  1. [root@hk admin_backups]# crontab –e  

添加以下内容:

 

 
  1. */1 * * * * /home/admin/admin_backups/backup.sh > /dev/null 2>&1  

每个一分钟从服务器A同步到服务器B!

服务器B向下备份到服务器A

 

 
  1. rsync -vzrtopg –progress –delete –password-file=/home/admin/admin_backups/password.rsync  /home/admin/admin_backups/test rsync://lixiphp@203.171.237.245/test  

请确保服务器A同步用户lixiphp,对模块test所在目录有读、写、执行的权限

 

补充:rsync+inotify 实时同步

A服务器实时同步到B服务器做法:

B服务器部署rsync服务端,B服务器配置rsync客户端和编译安装inotify

实时同步原理:

inotify监测和同步脚本:

 

 
  1. #!/bin/bash   
  2. host=192.168.1.1  
  3.  
  4. src=/wwwroot/www/jythonscript/   
  5. dst=jythonscript   
  6. user=rsync   
  7. /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src \   
  8. | while read files   
  9.        do   
  10.               /usr/local/bin/rsync -vzrtopg --delete --progress  $src  $user@$host::$dst     --password-file=/etc/rsyncd.password   
  11.                  echo "${files} was rsynced" >>/tmp/rsync.log 2>&1   
  12.        done  

inotifywait监测文件是否发生变化,如果有变化就执行rsync,同步相应的文件!

 
 

本文转自    geekwolf   51CTO博客,原文链接:http://blog.51cto.com/linuxgeek/997906

相关文章
|
9月前
|
算法 Linux
Linux系统【文件传输】rsync命令 – 远程数据同步工具
rsync命令来自于英文词组“remote sync”的缩写,其功能是用于远程数据同步。rsync命令能够基于网络(含局域网和互联网)快速的实现多台主机间的文件同步工作,并与scp或ftp发送完整文件不同,rsync有独立的文件内容差异算法,会在传送前对两个文件进行比较,只传送两者内容间的差异部分,因此速度更快。
149 2
|
11月前
|
安全 Unix Linux
【Linux网络服务】Rsync数据同步
【Linux网络服务】Rsync数据同步
|
监控 应用服务中间件 开发工具
Centos7.4 搭建rsync+inotify 实现数据同步
简介 起因 因公司全部迁云,配置ECS时需要将Web集群之间配置 数据同步 以及 会话保持,接下来配置在 linux Centos7.4版本的系统操作。 优点 使用rsync工具和inotify机制相结合,可以实现触发式部署(实时同步),只要原始(主)位置的文档发生变幻,则立即启用增量推送操作,否则处于静态等待状态,这样以来,就避免了分布式集群部署过程复杂的问题。
1794 0
|
监控 应用服务中间件 开发工具
Centos7.4 搭建rsync+inotify 实现数据同步
简介 起因 因公司全部迁云,配置ECS时需要将Web集群之间配置 数据同步 以及 会话保持,接下来配置在 linux Centos7.4版本的系统操作。 优点 使用rsync工具和inotify机制相结合,可以实现触发式部署(实时同步),只要原始(主)位置的文档发生变幻,则立即启用增量推送操作,否则处于静态等待状态,这样以来,就避免了分布式集群部署过程复杂的问题。
1226 0
|
应用服务中间件 nginx Perl
|
21天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
66 0
|
1月前
|
canal 关系型数据库 MySQL
四种常用的 MySQL 数据同步 ES 的方法
【2月更文挑战第16天】
168 2
四种常用的 MySQL 数据同步 ES 的方法

热门文章

最新文章