Using rsync+inotify-tools to achieve file synchronization under ubuntu

    In the previous article , I talked about how to use rsync and inotify-tools under ubuntu to synchronize files between two ubuntu servers. better now

Step one, assuming that there are four servers A1, A2, A3, and A4, their synchronization folder is set to /data, when the file on any one of them changes, it can automatically

Sync to other machines. This is the subject of this article.

    First, you need to set up each machine to be an rsync server, and each machine will be a rync slave and run inotify-tools. Therefore, each machine

There must be two rsync key files on the device, one as a host with a user name: password format key file, and the other as a slave with only a secret key file.

code key file. Let the IPs of the four machines be:

A1: 192.168.1.10       
A2: 192.168.1.20     
A3: 192.168.1.30     
A4:192.168.1.40  

A1's /etc/rsyncd.conf file:

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
 pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[ftp]

	comment = public archive
	path = /data
	use chroot = no
#	max connections=10
	lock file = /var/lock/rsyncd
# the default for read only is yes...
	read only = no
	list = yes
	uid = root
	gid = root
#	exclude =
#	exclude from =
#	include =
#	include from =
	auth users = lzj
	secrets file = /etc/rsyncd.secrets
	strict modes = yes
	hosts allow = 192.168.1.20 192.168.1.30 192.168.1.40
#	hosts deny =
	ignore errors = yes
	ignore nonreadable = yes
	transfer logging = no
#	log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
	timeout = 600
	refuse options = checksum dry-run
	dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

A1's /etc/rsyncd.secrets file:

lzj: 123

A1's /etc/rsync.pass file:

123

The difference between the configuration files of the other three machines and A1 is that the host allow of the configuration file is modified to the corresponding IPs of the other three machines.

A1's rsyn.sh file:

src=/data
dst[0][email protected]::ftp
dst[1][email protected]::ftp
dst[2][email protected]::ftp
 
/usr/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
        do
       		for i in ${dst[@]}
		do
		rsync -vzrtopg --delete  --progress --password-file=/etc/rsync.pass $src $i
		echo $i
		done
        done
exit 0

Let the rsyn.sh script run in the background:

chmod +x rsync.sh
nohup bash rsyn.sh &


   


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326017762&siteId=291194637