Host name, IP:
Server 172.31.82.184
Client 172.31.82.185
Demand:
1, server Side "/data/server" as the client side "/data/client" Backup directory;
2, the realization of the function is the client side of the directory to do delete permission change operation, server can maintain real-time synchronization;
3. Turn off firewall and SELinux
Service Iptables Stop
Setenforce 0
First, configure the server side
1. Install rsync software
Yum Install-y rsync-y
2. Create rsync.conf configuration file, default that file does not exist
Vim/etc/rsyncd.conf
UID = Rootgid = Rootusechroot = Nomax Connections = 20timeout = 600pid File =/var/run/rsyncd.pidlock File =/var/run/rsyn C.locklog file =/var/log/rsyncd.log[web_log] # #此处是定义的模块名, and "Path =/data/server/" under Server for path =/data /server/ignore Errorsread = falsewriteonly = Falselist = falsehosts Allow = *auth users = backusersecrets File =/etc /rsync.password
3. Create a backup directory
Mkdir/data/server-p
4. Create rsync user name and password file
echo "backuser:123" >>/etc/rsync.password
5, for/etc/rsync.password authorization for 600
chmod 600/etc/rsync.password
6. Start the Rsync service and add boot automatically
/usr/bin/rsync--daemon &
echo "/usr/bin/rsync--daemon" >>/etc/rc.local
Second, configure the client side
1. Install rsync
Yum Install Rsync-y
2, set the rsync client password file, the client only need to set the Rsync synchronization password can not set the user name
echo "123" >/etc/rsync.password
3. Set the password file permissions to 600
chmod 600/etc/rsync.password
Configure Inotyfi
1. Install the base compilation package
Yum install-y gcc Lrzsz
Tar zxvf inotify-tools-3.14.tar.gz &&cd inotify-tools-3.14 &&./configure &&make && Make install
2. Create Client Side Synchronization directory
Mkdir-p/data/client
3. Test whether files can be synchronized on client side
Uploading files
RSYNC-VZRTOPG--progress/data/client/[Email protected]::server--password-file=/etc/rsync.password
Download file
RSYNC-VZRTOPG--progress [Email protected]::server/opt/--password-file=/etc/rsync.password
4, write a script implementation when the client side "/data/client" directory file changes, let the server node synchronize client data
#!/bin/bashsrc=/data/client/# #注意路径des1 =server # #注意路径host1 =172.31.82.184 # #server端ipuser1 =backuser # #同步数据使用的用户 /usr/local/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f '-emodify,delete,create,attrib $src | While read FILEDO/USR/BIN/RSYNC-VZRTOPG--delete--progress $src [email protected] $host 1:: $des 1--password-file=/etc/ Rsync.passwordecho ' Date +%m.%d.%h%. M ' "${files} was rsynced" >>/var/log/rsync.log 2>&1done
5. Execute permissions for script
chmod +x/root/inotify.sh
6. Script execution in the background
/root/inotify.sh &
7. Add the script to the system self-boot file
echo "/root/inotify.sh" >>/etc/rc.local
8. Add files to client side to see if there is synchronization on server side
mkdir 11 22 33 44
Test Pass
Third, exclude files and directories that you do not want to sync
1, single file exclusion: For example I do not want to sync/opt/aa.php files, directly use--exclude "aa.php"
Multiple files and directories excluded--exclude-from= "/usr/local/src/exclude.list"
Script syntax:
vim/root/inotify.sh
#!/bin/bash
src=/data/client/
Des1=server
host1=172.31.82.184
User1=backuser
/usr/local/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f '-emodify,delete,create,attrib $src | While read file
Do
/USR/BIN/RSYNC-VZRTOPG--delete--progress --exclude-from= "/usr/local/src/exclude.list" $src [email Protected] $host 1:: $des 1--password-file=/etc/rsync.password
Echo ' Date +%m.%d.%h%. M ' "${files} was rsynced" >>/var/log/rsync.log 2>&1
Done
2, the script will also need to create a file corresponding to the script, the following is not synchronized to the server node files and directories:
Vim /usr/local/src/exclude.list
Exclude
11
22
33
test.php
3. Kill script process running in background
Ps-elf |pgrep Inotify|xargs kill-9
4. Start the synchronization script
Sh/root/inotify.sh &
5. Set the upper limit of the watchs associated with each inotify instance, or you will get an error if you transfer too many files.
echo 30000000 >/proc/sys/fs/inotify/max_user_watches
Attention:
After the script is modified, you need to restart the background script:
Ps-elf |pgrep Inotify|xargs kill-9
Sh/root/inotify.sh &
This article is from the "david0512" blog, make sure to keep this source http://gjr0512.blog.51cto.com/6518687/1758117
CentOS 6.5 rsync+inotify Real-time synchronization