Rsync+Inotify for data synchronization-Protect Data Security

Rsync is UNIX and UNIX-Like platform is a powerful data mirroring backup software , it is not like FTP or other file transfer services such as the need to carry out a full backup , Rsync can be based on the changes in the data to carry out differential backup , thus reducing the data traffic , improve work efficiency . You can use it for local data or remote data replication, Rsync can use SSH secure tunneling for encrypted data transfer, Rsync server-side definition of the source data, Rsync client will only be changed in the source data from the server will be actually copied to the local data, if the source data is deleted on the server, the client data will be deleted to ensure that data is synchronized between hosts. Rsync uses port TCP-873.
Inotify is a Linux feature that monitors file system operations such as reads, writes, and creates Inotify is responsive, simple to use, and much more efficient than the busy polling of a cron task This chapter learns how to integrate inotify into your application and discovers a set of command-line tools that can be used to further automate system governance.
Rsync File Synchronization
Rsync server configuration
1. The server first installs the Rsync package and creates a /common directory to copy in some configuration files for testing.

[root@localhost ~]# yum install -y rsync
[root@localhost ~]# mkdir -p /common
[root@localhost ~]# cp -a /etc/* /common

  1. Edit Rsync's main configuration file to override the following, which specifies the files and directories we need to share.

[root@localhost ~]# vim /etc/rsyncd.conf
use chroot=yes # Disable the user's home directory.
address=127.0.0.1 #Specify the local IP address.
port=873 #Specify the default port
log file=/var/log/rsync.log #Specify where to save the log file.
pid file=/var/syncd.pid #Specify where to save the PID file.

[pub] #Declare the directory name (i.e. the share name below)
comment=hello lyshark #describe the message
path=/common #Directory to sync to
read only=yes #Read only
dont compress= .gz .tgz *.bz2 #Specify that these files are not to be compressed.
auth users=lyshark #User name and password for using sync
secrets file=/etc/rsyncd_users.db #Store password files for virtual users.

  1. Create the password file, in the above configuration we specified the password configuration file location as /etc/rsyncd_users.db, edit this configuration file and write the following content.

[root@localhost ~]# vim /etc/rsyncd_users.db

lyshark:123123 #format:username:password
admin:123123

  1. Give the server-side password file the minimum runtime permissions, and start the rsync daemon.

[root@localhost ~]# chmod 600 /etc/rsyncd_users.db #Give permissions to the server password file and start the rsync daemon.
[root@localhost ~]# rsync --daemon #Start the service.
[root@localhost ~]# netstat -antp |grep “:873” #Check the port, whether it is opened successfully.

Rsync Client Configuration
1. First of all we should create a client receiving location, used to receive the transfer file, here I will be consistent with the server.

[root@localhost ~]# mkdir -p /common

  1. Declare a global variable, Rsync authentication first search for the variable in the password, here will be written to the configuration file to realize the boot.

[root@localhost ~]# export RSYNC_PASSWORD=123123 #Set the authentication password.
[root@localhost ~]# echo “export RSYNC_PASSWORD=123123” >> /etc/profile #Write the password to the configuration file.

  1. After all the above steps are completed, we can use the following command to verify the synchronization.

[root@localhost ~]# rsync -avz virtual username@IP address::share name data storage location #Sync from server to local machine
[root@localhost ~]# rsync -avz --delete virtual username@server IP address::share name data storage location #Synchronize from the server to the local machine and keep the data exactly as it was on the server.

[root@localhost ~]# rsync -avz local file root@server IP:/save location #Send my file to each other
[root@localhost ~]# rsync -avz root@server IP:/private file location /save location #Download the other file to me
[root@localhost ~]# rsync -avz [email protected]::pub /client/rsync
each synchronization data need to manually enter the command is a very troublesome thing, as an operations and maintenance personnel, we need a more intelligent processing mechanism, which can be considered to use Shell scripts to solve such problems, the following rsync_back.sh script can be achieved by synchronizing the task of the data, we can add him to the scheduled tasks to achieve the automated synchronization of data with.

! /bin/bash

export RSYNC_PASSWORD=123123
src=common
dest=/common
server=192.168.1.10
user=lyshark

[! -d dest ] && mkdirdest
rsync -avz --delete {server}::dest/${date +%Y%m%d}
Rsync+Inotify Bidirectional Synchronization
Rsync + Inotify can be achieved on one server data updates another immediately synchronized to achieve the basic one-way hot backup here there is a, the preconditions master server to slave server to establish SSH key pair authentication, and the following steps master and slave servers need to do it again.

  1. Download Inotify tools, and compile and install inotify-tools

[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@localhost ~]# tar -xzvf inotify-tools-3.13.tar.gz
[root@localhost ~]# cd inotify-tools-3.13/
[root@localhost ~]# . /configure
[root@localhost ~]# make && make install

  1. Optimize kernel parameters (optional)

[root@localhost ~]# vim /etc/sysctl.conf

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p
3 Enable the monitoring module (test)
[root@localhost ~]# inotifywait -mrq -e create,delete /tmp #tmp is the directory to be monitored.

  1. Monitoring script, if the server data is updated, automatically synchronize the data to the client using the Rsync command

! /bin/bash

date="inotifywait -mrq -e create,delete /local directory”
sync="rsync -avz --delete /local directory/ other username@other ip:/sync to other which directory”

sync
done

Privacy    |    Terms of use