Project requirement: Machine A (190) and Machine B (217) need a folder’s contents to be identical to each other. Both Machine A and Machine B might receive files independently and need to sync them to the other party.
Method 1: Use rsync-server with inotify for file synchronization
1. rsync-server configuration (217)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# vi /etc/xinetd.d/rsync # default: off# allows crc checksumming etc.service rsync{ disable = no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID} |
2. Specify file location
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# vi /etc/rsyncd.conf uid = rootgid = rootuse chroot = yesmax connections = 4strict modes = yessyslog facility = local5port = 873[backup]path = /home/rsync/test/ ## Folder to synchronizecomment = This is a testignore errorsread only = no ## Write permissionlist = yesauth users = rsyncsecrets file = /etc/rsync.pas ## Auth file, must also exist on the peer machinehosts allow = 192.168.1.190 ## Client IP |
3. Create rsync.pas
|
1
2
|
# vi /etc/rsync.passync:test |
4. Client sync script (190)
|
1
2
3
4
5
6
7
8
|
#vi inotify_rsync.sh#!/bin/bashSRC=/home/rsync/test/DST=rsync@192.168.1.217::backup/usr/bin/inotifywait -mrq -e modify,<del datetime="2011-11-07T01:44:44+00:00">delete</del>,create,attrib ${SRC} | while read D E Fdo/usr/bin/rsync -ahqzt --progress <del datetime="2011-11-07T01:44:44+00:00">--delete</del> --password-file=/etc/rsync.pas $SRC $DSTdone |
5. Configure script to run at startup
|
1
|
# cat "/root/inotify_rsync.sh &" >> /etc/rc.local |
6. Implementing two-way sync and its issues The above only implements syncing from machine A (190) to machine B (217). To achieve two-way sync, set up an rsync server on machine A (190) by adapting the configuration. However, delete operations cannot be performed because there is no way to determine which machine’s operations take precedence, which would cause newly created files to be deleted.
Method 2: Using NFS shared server for file synchronization
1. Configure NFS
|
1
2
3
4
5
|
#service portmap start#service nfs start#vi /etc/exports /home/rsync/test *(rw,sync,no_root_squash) ##rw: readable and writable permissions; sync: write data synchronously to storage; no_root_squash: users logging into the NFS host as ROOT will have ROOT privileges# exportfs -rv ##Reload the configuration |
2. Mount the NFS shared directory Before mounting, use SSH to connect the two machines first, so you don’t need to configure authentication. Then write it into the /etc/fstab file, and it’s done.
|
1
|
mount -t nfs 192.168.1.190:/home/rsync/test /home/rsync/test |
Final Thoughts: Method one keeps files on both machines simultaneously, offering real-time performance and security, but it is only suitable for environments where files are not deleted. Method two keeps files only on the NFS server. If combined with rsync server for scheduled backups to another location, manual recovery is possible in case of failure, which is also a good choice.
This article is from the “kras-linux” blog. Please be sure to keep this source link http://klinux.blog.51cto.com/2972664/708294