Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

rsync+inotify实现数据同步及常见问题

$
0
0

rsync:Remote Sync,是类Unix系统下的数据镜像备份工具。通过rsync可以解决对实时性要求不高的数据进行备份需求;例如:指定的备份文件服务器数据到指定的远端服务器,对本地磁盘定期做数据镜像等。

inotify:inotify是一种文件变化通知机制;通过inotify可以在监控文件系统中添加、删除、修改、移动等各种操作。

准备环境:

主服务器(inotify-Master) IP:172.18.42.201 从服务器(inotify-Slave) IP:172.18.42.200

一、部署inotify-Slave

1、安装rsync程序

[root@inotify-slave ~]# yum install rsync

2、创建rsync用户

[root@inotify-slave ~]# useradd -s /bin/nologin -M rsync

3、创建rsync工作模块的目录

[root@inotify-slave ~]# mkdir /mydata/ ##创建rsync工作模式的模块目录 [root@inotify-slave ~]# chown rsync.rsync /mydata/ ##更改属主、主组;使rsync用户有权限更改数据 [root@inotify-slave ~]# ls -ld /mydata/

drwxr-xr-x 2 rsync rsync 6 May 19 21:02 /mydata/

4、配置虚拟用户的密码文件/etc/rsync.password

[root@inotify-slave ~]# vim /etc/rsync.password

linuxidc:linux ##“linuxidc”为虚拟用户用户名;“linux”为虚拟用户密码

[root@inotify-slave ~]# chmod 600 /etc/rsync.password ##为密码文件增加安全性 [root@inotify-slave ~]# ll /etc/rsync.password

-rw------- 1 root root 12 May 18 21:54 /etc/rsync.password

5、配置rsync配置文件/etc/rsyncd.conf

[root@inotify-slave ~]# vim /etc/rsyncd.conf

uid = rsync

gid = rsync

user chroot = no

max connections = 200

timeout = 300

read only = no

[rsync] ##定义模块名,名字可随意

path = /mydata ##指定rsync用户的模块目录

auth users = linuxidc ##指定虚拟用户名

secrets file = /etc/rsync.password ##指定虚拟用户的密码文件路径

ignore errors ##表示忽略错误

6、启动rsync服务

[root@inotify-slave ~]# rsync --daemon --config=/etc/rsyncd.conf ##rsync监听在tcp协议的873端口 [root@inotify-slave ~]# ps aux | grep rsync

root 1330 0.0 0.0 114640 328 ? Ss 21:13 0:00 rsync --daemon --config=/etc/rsyncd.conf

root 1338 0.0 0.1 112644 952 pts/0 R+ 21:13 0:00 grep --color=auto rsync

二、配置inotify-master的密码文件

1、配置虚拟用户的密码文件

[root@inotify-master ~]# echo "linux" > /etc/rsync.password [root@inotify-master ~]# cat /etc/rsync.password

linux ##注意:只需要写出虚拟用户的密码;不用写出用户名

[root@inotify-master ~]# chmod 600 /etc/rsync.password [root@inotify-master ~]# ls -ld /etc/rsync.password

-rw------- 1 root root 6 May 19 21:18 /etc/rsync.password

三、通过inotify-master测试推送文件

[root@inotify-master ~]# echo "Hello Word" > linuxidc.txt [root@inotify-master ~]# rsync -avz linuxidc.txt lweim@172.18.42.200::rsync --password-file=/etc/rsync.password

sending incremental file list

linuxidc.txt

sent 81 bytes received 27 bytes 216.00 bytes/sec

total size is 11 speedup is 0.10

四、检查inotify-slave的工作模块目录

[root@inotify-slave ~]# ll /mydata/

total 4

-rw-r--r-- 1 rsync rsync 11 May 19 21:21 linuxidc.txt

[root@inotify-slave ~]# cat /mydata/linuxidc.txt

Hello Word ##推送成功

五、部署rsync-master

1、通过inofity源码包编译安装

[root@inotify-master ~]# tar -xf inotify-tools-3.13.tar [root@inotify-master ~]# cd inotify-tools-3.13/ [root@inotify-master inotify-tools-3.13]# ./ configure --prefix=/usr/local/inotify ##指明安装路径 [root@inotify-master ~]# make -j 4 && make install

2、编写监控脚本

12345678910111213141516171819202122 [root@inotify-master ~]# vim inotify.sh

#!/bin/bash

host=172.18.42.200 ##指明inotify-slave的ip地址

src=/www/linuxidc ##本机监控的目录,可随意定义

dst=rsync ##inotify-slave的rsync用户的模块目录

user=linuxidc ##虚拟用户的用户名

passfile=/etc/rsync.password ##调用本地的密码文件

inotify_home=/usr/local/inotify ##指明inotify的安装目录

if [ ! -e "$src" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then #if [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then

echo "Check File and Folder"

exit 1

fi

${inotify_home}/bin/inotifywait -mrq -e close_write,delete,create,attrib $src | while read file

do

cd $src && rsync -arvz -P ./ --timeout=100 $user@$host::$dst --password-file=$passfile &>/dev/null

done

exit 0

六、执行inotify-master上的inotify.sh脚本

[root@inotify-master ~]# bash -x inotify.sh ##运行脚本

+ host=172.18.42.200

+ src=/web/linuxidc/

+ dst=rsync

+ user=linuxidc

+ passfile=/etc/rsync.password

+ inotify_home=/usr/local/inotify

+ '[' '!' -e /web/linuxidc/ ']' + '[' '!' -e /usr/local/inotify/bin/inotifywait ']' + '[' '!' -e /usr/bin/rsync ']' + '[' '!' -e /etc/rsync.password ']'

+ read file

+ /usr/local/inotify/bin/inotifywait -mrq -e close_write,delete,create,attrib /web/linuxidc/

七、在inotify-master本机监控的目录下创建文件

[root@inotify-master linuxidc]# pwd

/www/linuxidc

[root@inotify-master linuxidc]# touch a aa aaa aaaa [root@inotify-master linuxidc]# ll

total 0

-rw-r--r-- 1 root root 0 May 19 22:54 a

-rw-r--r-- 1 root root 0 May 19 22:54 aa

-rw-r--r-- 1 root root 0 May 19 22:54 aaa

-rw-r--r-- 1 root root 0 May 19 22:54 aaaa

八、在inotify-slave的rsync工作模块目录下查看

[root@inotify-slave mydata]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 May 19 22:54 a

-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aa

-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaa

-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaaa

[root@inotify-slave mydata]# pwd

/mydata

以下是为配置rsync时的常见问题:

问题一:

@ERROR: chroot failed

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。

问题二:

@ERROR: auth failed on module tee

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。

提供正确的用户名密码解决此问题。

问题三:

@ERROR: Unknown module ‘tee_nonexists'

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

问题1:

在client上遇到问题:

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]

遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--

password-file=/etc/rsync.pas

问题2:

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]

遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,但是

服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登陆名不可缺少

问题3:

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

@ERROR: chdir failed

rsync error: error starting client-server protocol (code 5

Viewing all articles
Browse latest Browse all 11063

Latest Images

Trending Articles