in here The introduction of rsync combined with inotify configuration file real-time synchronization method, with the need for a friend reference. About the software I will not introduce more, please refer to the degree Niang!
Note : 1. At the time of configuration, the master server and the backup server are clearly divided.
2. When using someone else's script, note the name of the profile referenced in your script.
3. Turn off the firewall, test the manual push first, and then test the real-time synchronization after the push is successful.
First, the deployment environment
Server IP Address
|
Host Name |
Server System |
192.168.1.13 Primary Server |
13server |
Centos 6.5 X64 |
192.168.1.14 Backup Server |
localhost |
Centos 6.5 X64 |
Second, the backup server installed Rsync
2.1 Because I am here is two newly installed server so directly installs, did not go to check above has no residual rsync's file. Rsync installation package I downloaded from the official website, to see which version you like, provide an official website address http://rsync.samba.org/.
#cd/usr/local/src
#tar-ZXVF rsync-3.0.9.tar.gz//Extract Files
#cd rsync-3.0.9//Enter the file directory
#./configure--prefix=/usr/local/rsync
/The primary role is to configure the software to be installed to check whether the current environment meets the dependencies to install the software
#make//compile (if you have an error, please carefully check if there is a missing library file, you can use me this)
(yum-y install make gcc-c++ cmake bison-devel ncurses-devel gcc \
autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel*)
#make Install//installation
2.2 Create a new rsync user and module catalog and change its user group
[Email protected]]# useradd rsync-s/sbin/nologin-m #添加rsync用户
[Email protected]]# grep rsync/etc/passwd
Rsync:x:2004:2004::/home/rsync:/sbin/nologin
[[Email protected]]# mkdir/data/backup #创建rsync Module directory for daemon working mode
[Email protected]]# ll-d/data/backup/
Drwxr-xr-x. 2 rsync rsync 4096 August 13:06/data/backup/
[Email protected]]# chown rsync.rsync/data/backup/#更改模块目录的用户组
[Email protected]]# ll-d/data/backup/
Drwxr-xr-x. 2 rsync rsync 4096 August 13:06/data/backup/
2.3 Writing the rsync daemon configuration file/etc/rsyncd.conf
[Email protected]/]# cat/etc/rsyncd.conf
# #rsyncd. conf start##
#工作中指定用户 (requires a specified user)
UID = rsync
GID = rsync
#相当于黑洞. Error targeting
Use chroot = no
#有多少个客户端同时传文件
Max connections = 200
#超时时间
Timeout = 300
#进程号文件
PID file =/var/run/rsyncd.pid
#日志文件
Lock file =/var/run/rsync.lock
#日志文件
Log file =/var/log/rsyncd.log
#模块开始
#这个模块对应的是推送目录
#模块名称随便起
[Backup]
#需要同步的目录
Path =/data/backup/
#表示出现错误忽略错误
Ignore errors
#表示网络权限可写 (Local control is truly writable)
Read Only = False
#这里设置IP或让不让同步
List = False
#指定允许的网段
Hosts allow = 192.168.1.0/24
#拒绝链接的地址, the following represents a link that is not rejected.
Hosts Deny = 0.0.0.0/32
#不要动的东西 (default)
#虚拟用户
Auth users = Rsync_backup
#虚拟用户的密码文件
Secrets file =/etc/rsync.password
#配置文件的结尾
#rsync_config_______________end
2.4 Configuring the virtual user's password file
[Email protected]/]# echo "Rsync_backup:[email protected]" >/etc/rsync.password
[Email protected]/]# Cat/etc/rsync.password
Rsync_backup:[email protected] #注: Rsync_backup for virtual user, [email protected] for this virtual user's password
[[email protected]/]# chmod 600/etc/rsync.password #为密码文件提权 for added security
[Email protected]/]# Ll/etc/rsync.password
-RW-------. 1 root root 22 August 19:27/etc/rsync.password
2.5 Starting Rsync
[[email protected] /]# rsync --daemon #启动rsync服务
[[email protected] /]# ps -ef |grep rsync
Root 9871 1 0 Aug10? 00:00:00 rsync--daemon
Root 10532 10500 0 13:30 pts/0 00:00:00 grep rsync
[Email protected] backup]# NETSTAT-LNUTP |grep rsync
TCP 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9871/rsync
TCP 0 0::: 873:::* LISTEN 9871/rsync
Third, the primary server deployment
3.1 Test push via [email protected]
[Email protected] Configure password file, test push
[[email protected] ~]# echo "[email protected]" >/etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
[email protected] #注意:这里只要写密码即可,切记。
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 8月 22 20:32 /etc/rsync.password
[[email protected] ~]# echo "hello leesir">test.txt
[[email protected] ~]# cat test.txt
hello leesir
[[email protected] ~]# rsync -avz test.txt [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
test.txt
sent 82 bytes received 27 bytes 72.67 bytes/sec
total size is 13 speedup is 0.12
Backup Server check:
[[email protected] /]# ll /data/backup/
总用量 4
-rw-r--r--. 1 rsync rsync 13 4月 22 14:34 test.txt
[[email protected]3server /]# cat /data/backup/test.txt
hello leesir
3.2 See if the current system supports inotify
[Email protected] work]# ll/proc/sys/fs/inotify/
Total dosage 0
-rw-r--r--1 root root 0 August 13:35 max_queued_events
-rw-r--r--1 root root 0 August 13:35 max_user_instances
-rw-r--r--1 root root 0 August 13:35 max_user_watches
#显示这三个文件则证明支持.
Expand:
/proc/sys/fs/inotify/max_queued_evnets
Represents the maximum value assigned to the number of events that can be queued in inotify instance when calling Inotify_init, an event that exceeds this value is discarded, but the In_q_overflow event is triggered.
/proc/sys/fs/inotify/max_user_instances
Represents the maximum number of inotify instatnces that each real user ID can create.
/proc/sys/fs/inotify/max_user_watches
Represents the maximum number of directories that can be monitored per inotify instatnces. If you monitor a large number of files, you need to increase the size of this value appropriately, depending on the situation.
Example: Echo 30000000 >/proc/sys/fs/inotify/max_user_watches
3.3 Download Install inotify source Package
[[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下载inotify源码包
..................................
[Email protected] ~]# tar zxf inotify-tools-3.14.tar.gz
[[email protected] ~]# cd inotify-tools-3.14
[[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14 #配置inotify,并指定安装路径为/usr/local/inotify-3.14
................................
[[[email protected] ~] inotify-tools-3.14]# make && make install
................................
3.4
INotify inotifywait Command Common parameters detailed
[Email protected] inotify-tools-3.14]# cd/usr/local/inotify-3.14/
[Email protected] inotify-3.14]#/bin/inotifywait--help
-r|--recursive Watch directories recursively. #递归查询目录
-q|--quiet print (only print events). #打印监控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait would exit after one event is received. #始终保持事件监听状态
--excludei <pattern> like--exclude but case insensitive. #排除文件或目录时, case insensitive.
--TIMEFMT <fmt> strftime-compatible format string for use with%T in--format string. #指定时间输出的格式
--format <fmt> Print using a specified printf-like format string; Read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event <event1> [-e|--event <event2> ...] Listen for specific event (s). If omitted, all events is listened for. #通过此参数可以指定需要监控的事件 as follows:
Events:
Access file or directory contents were read #文件或目录被读取.
Modify file or directory contents were written #文件或目录内容被修改.
attrib file or directory attributes changed #文件或目录属性被改变.
Close file or directory closed, regardless of read/write mode #文件或目录封闭, regardless of read/write modes.
Open file or directory opened #文件或目录被打开.
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录.
Move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录.
Create file or directory created within watched directory #文件或目录被创建在当前目录
Delete file or directory deleted within watched directory #文件或目录被删除
Unmount file system containing file or directory unmounted #文件系统被卸载
3.5 Writing a monitoring script and loading it into the background execution
#!/bin/bash
src=/data/backup/#本地监控的目录
Des=backup #13server的rsync服务的模块名
host=192.168.1.13 #备份服务器的ip地址
/usr/local/bin/inotifywait (#这个就要主要到你的这个可执行文件放在哪个位置) -mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f '-E Modify,delete,create,attrib $SRC | While read files
Do
For HostIP in $host
Do
RSYNC-VZRTOPG--delete--progress --password-file=/etc/rsync.password $src rsync_backup@ $hostip:: $des #注意红色字体部分, change the name according to your own configuration
Done
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
Done
3.6 Start Nohup
Nohup/bin/bash//usr/local/inotify/inotify.sh & #将脚本加入后台执行Note your own script path
echo "Nohup/bin/bash//usr/local/inotify/inotify.sh &" >>/etc/rc.local #加入开机启动
Four Tests and partial fault phenomena
Question one:
@ERROR: Chroot failed
Rsync Error:error starting Client-server Protocol (code 5) at MAIN.C (1522) [receiver=3.0.3]
Reason:
The server-side directory does not exist or has no permissions, creating a directory and correcting permissions resolves the issue.
Question two:
@ERROR: Auth failed on module backup
Rsync Error:error starting Client-server Protocol (code 5) at MAIN.C (1522) [receiver=3.0.3]
Reason:
The server side of the module (backup) needs to verify the user name password, but the client does not provide the correct user name password, authentication failed.
Provide the correct user name password to resolve this issue.
Question three:
@ERROR: Unknown module ' backup '
rsync Error:error starting Client-server Protocol (code 5) at MAIN.C (1522) [receiver=3.0.3]
Reason:
The specified module does not exist on the server. Provide the correct module name or modify the server side to the module you want to solve the problem.
If you have any questions, please contact [email protected], more content in the debris forum www.debris.cn, Welcome to visit the Guide!!!
This article is from the "Debris Memory" blog, please be sure to keep this source http://debris.blog.51cto.com/1614432/1683627
Rsync+inotify for real-time synchronization