Real-time synchronization of Linux files

Source: Internet
Author: User
Tags inotify rsync

Reference Blog: https://www.cnblogs.com/MacoLee/p/5633650.html

First, the file synchronization is very simple
Server: Passively receive transmitted data.
Client: Proactively provide data to the server
Installation ideas: Both the server and the client need to install rsync software. Because the client I understand here is to provide the data to the server, so the client needs to monitor the file changes in real time, where the client needs to install the INotify software.
Installation Demo idea: here follow the conventional idea. After installing the Rsync software on the server, edit the relevant configuration file and start rsync. After the client installs the Rsync software, no action is required, only the relevant synchronization commands are executed.
Second, install rsync software
1, two kinds of installation methods: (1), directly using the Yum command installation (I use)
Yum-y Install Rsync
(2), using the compilation installation
[Email protected] ~]# cd/usr/local/src/
[Email protected] src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[Email protected] src]# tar zxvf rsync-3.0.9.tar.gz
[Email protected] src]# CD rsync-3.0.9
[Email protected] rsync-3.0.9]#/configure--prefix=/usr/local/rsync
[[email protected] rsync-3.0.9]# make
[[email protected] rsync-3.0.9]# make install
2. Set Password authentication files and configuration files
(1), establish user and password authentication file (compile and install location)
(Modify the configuration file only on the server)
[Email protected] rsync-3.0.9]# echo "web:123" >/USR/LOCAL/RSYNC/RSYNCD.PASSWD
(configuration file modifications are made only on the client)
[Email protected] rsync-3.0.9]# echo "123" >/USR/LOCAL/RSYNC/RSYNC.PASSWD
Set permissions to read-only
[Email protected] rsync-3.0.9]# Cd/usr/local/rsync
[Email protected] rsync]# chmod rsyncd.passwd

(2), establish rsync configuration file (only on the server to do configuration file modification)
Configuration file differences caused by different installation methods:
<1>, when installing using the Yum command directly
[Email protected] rsync]# vi/etc/rsyncd.conf
#/etc/rsyncd:configuration file for rsync daemon mode
# See rsyncd.conf Mans page for more options.
UID = root
GID = root
Use chroot = no
Max connections = 200
Timeout = 1000
Transfer logging = yes
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsyncd.lock
Log file =/var/log/rsyncd.log
Log format =%t%a%m%f%b
Port = 873
Ignore errors
#read only = yes
#address = 192.168.248.136

[web]
Path =/Home
Read Only = False
Write only = yes
Hosts allow = 192.168.248.138 192.168.248.136
Hosts deny = *
List = False
Auth users = Web
Secrets file =/usr/local/rsync/rsyncd.passwd
#read only = yes
#exclude = test/#不同步的目录
<2>, using a compiled installation (example)
[Email protected] rsync]# vi/usr/local/rsync/rsyncd.conf
UID = root
GID = root
Use chroot = no
Max connections = 4
Strict modes = yes
Hosts allow = 192.168.1.101 #可以空格, allowing multiple
Port = 873
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
Log file =/var/log/rsyncd.log

[Test]
Path =/home/test
Ignore errors
Read Only = False
List = False
Auth users = Backup
Secrets file =/usr/local/rsync/rsyncd.passwd

PS: note Here to modify read only to False, because the file is to be synchronized to the module, must be writable


(3), start the rsync server as a daemon (client does not need to start, that is, the client does not modify the configuration file)
<1> when installing with the Yum command
Rsync--daemon
<2>, using compile-time installation
Rsync--daemon--config=/usr/local/rsync/rsyncd.conf

(4), join the boot from the boot
echo "/usr/local/rsync/bin/rsync--daemon--config=/usr/local/rsync/rsyncd.conf" >>/etc/rc.local

Third, install inotify software
Install INotify on the client side (that is, the end to monitor file changes).
(1), verify that rsync is installed successfully
Example 1: Execute on client, client copy file to server
/USR/BIN/RSYNC-VZRTOPG--progress--password-file=/etc/web.passwd/home/fan [email protected]::web
Example 2: Execute on client, server copy file to client
[Email protected] rsync]# RSYNC-VZRTOPG--delete--progress--password-file=/usr/local/rsync/rsync.passwd [email Protected]::test/home/test

(2), installation inotify
[Email protected] rsync]# cd/usr/local/src/
[Email protected] src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[Email protected] src]# tar zxvf inotify-tools-3.14.tar.gz
[Email protected] src]# CD inotify-tools-3.14
[Email protected] inotify-tools-3.14]#/configure--prefix=/usr/local/inotify
[[email protected] inotify-tools-3.14]# make
[[email protected] inotify-tools-3.14]# make install

(3), create rsync copy script
This feature is mainly the contents of the client directory/home/fan/, if modified (whether add, modify, delete files) can be monitored through inotify, and through rsync real-time synchronization to the server/home, the following is done through the shell script.
#!/bin/bash
host=192.168.248.136
Src=/home/fan
Des=web
User=web

/usr/local/inotify/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f%e '-e modify,delete,create,attrib $ src | While read files
Do
/USR/BIN/RSYNC-VZRTOPG--progress--delete--password-file=/etc/web.passwd $src [email protected] $host:: $des
echo "${files} was rsynced" >>/data/logs/rsync.log 2>&1
Done

Where host is the client's ip,src is a directory for real-time monitoring, DES is the authentication module name, need to be consistent with client, user is to establish the password file authentication user.

Run the script
[Email protected] rsync]# sh/tmp/rsync.sh &

Add the rsync.sh script to the boot entry
[Email protected] rsync]# echo "setsid/usr/local/rsync/rsync.sh &" >>/etc/rc.local



Real-time synchronization of Linux files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.