Rsync + inotify enables data synchronization from time to time and cross-system data synchronization.

Source: Internet
Author: User
Tags inotify rsync scp command

1. Structure of the rsync client and server:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/53/wKioL1P4UDSBT-mBAAEbRkRVC10368.jpg "Title =" rsync ..jpg "alt =" wKioL1P4UDSBT-mBAAEbRkRVC10368.jpg "/>

During routine maintenance, data backup is a relatively basic one. Rsync solves tedious data synchronization from time to time and does not need to manually specify the changed data, but is automatically detected by rsync, when data inconsistency is found, backup is started or pushed to other rsync hosts. Inotify is a monitoring tool for Linux monitoring file changes. It can work better with rsync. The operating systems of many companies may be inconsistent. The Windows Host added above has implemented pulling data from the Linux host to the Windows host.

 

Ii. Rsync introduction.

1.) rsync Introduction: Rsync is a data synchronization tool that synchronizes data from time to time based on data changes. It is similar to the CP command and SCP command, and has almost the same Command Options.


2) features of rsync: images can be used to store the entire directory tree or file system, with a considerable transmission rate. It supports secure data transmission through SSH and anonymous data transmission.


3.) rsync mode:

Shell mode, similar to the CP command, also known as local file transfer mode.

The remote shell mode is similar to the SCP command. You can use the SSH protocol to host the remote transmission process.

List mode: the content in the data source is displayed in the list mode. You can also use it remotely.

Service Mode. At this time, Rsync is a daemon that can receive client data synchronization.


4.) rsync Command Options:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/51/wKiom1P4T5SBF6RdAAHaVgFvsRk154.jpg "Title =" rsynccommand. jpg "alt =" wkiom1p4t5sbf6rdaahavgfvsrk154.jpg "/>


5.) rsync mode. Rsync is installed by default.

Rsync remote shell mode, local data transmission to remote.

# Rsync-r -- stats-e SSH -- Progress/etc/PAM. d [email protected]:/backup/data # lspam. d # Note/PAM. d And/PAM. d/The gap during transmission. # Rsync-r -- stats-e SSH -- Progress/etc/PAM. d/[email protected]: /backup/data # lsatd CVS newrole polkit-1 runuser sshd Su-lchfn eject other poweroff runuser-l ssh-keycat system-auth-acchsh fingerprint-auth-AC Pam. d reboot setup su system-config-networkconfig-util halt passwd remote SmartCard-auth-AC sudo system-config-Network-Transport crond login password-auth-AC run_init SMTP. postfix Sudo-I


6.) receive data locally from the remote end.

# rsync -a --stats --progress [email protected]:/etc /tmp# ls /tmp/etc


7.) rsync list mode.

# rsync -nv [email protected]:/tmp/[email protected]‘s password: receiving file list ... donedrwxrwxrwt       4096 2014/08/22 03:40:01 .-rw-r--r--     813976 2014/08/12 17:59:44apr-1.5.0.tar.bz2-rw-r--r--     695303 2014/08/12 17:59:44 apr-util-1.5.3.tar.bz2-rw-r--r--    4780289 2014/08/12 17:59:44 httpd-2.4.4.tar.bz2-rw-r--r--    4724343 2012/06/29 19:27:06 phpMyAdmin-3.5.1-all-languages.tar.bz2-rw-r--r--    4657514 2012/03/12 16:58:14 wordpress-3.3.1-zh_CN.zip-rw-------          0 2014/08/12 17:05:23 yum.logdrwxrwxrwt       4096 2014/08/21 21:44:13 .ICE-unixdrwxr-xr-x       4096 2014/08/12 18:08:03 apr-1.5.0drwxr-xr-x       4096 2014/08/12 18:10:18 apr-util-1.5.3drwxr-xr-x       4096 2014/08/12 18:17:58 httpd-2.4.4drwxr-xr-x       4096 2014/08/12 23:53:21 phpMyAdmin-3.5.1-all-languages sent 11 bytes received 369 bytes  152.00bytes/sectotal size is 15671425  speedup is 41240.59 (DRY RUN)


8.) rsync service mode.

8.1) install the super daemon and enable the rsync service.

# Yum-y install xinetd # Install the super daemon. # Vim/etc/xinetd. d/rsync # default: off # Description: the rsync server is a good additionto an FTP server, as it # allows CRC checksumming etc. service rsync {disable = No # enable rsync flags = IPv6 socket_type = stream wait = no user = root server =/usr/bin/rsync server_args = -- daemon log_on_failure + = userid} # chkconfig rsync on # Start


8.2) edit the rsync configuration file, which is unavailable by default.

# Vim/etc/rsyncd. conf # global settingsuid = Nobody # the user who runs the process. Gid = Nobody # group of processes running. Use chroot = No # You cannot use chroot when running rsync. Max connections = 10 # maximum number of clients allowed to request data simultaneously. Strict modes = yes # enable strict mode. PID file =/var/run/rsyncd. pid # The PID File Location of rsync. Log File =/var/log/rsyncd. log # rsync log location. # Directory to be synced [tools] # similar to the sharing name. Path =/backup/data # Data Synchronization directory. Ignore errors = yes # ignore error messages during transmission. Read Only = No # whether it is read-only. Write only = No # whether to write only. Hosts allow = 172.16.0.0/16 # The CIDR block that can be synchronized. Hosts deny = * # filtered out from above. synchronization is not allowed if the network segment beyond 172.16.0.0. List = false # whether to allow remote listing of files in a directory. Uid = root # the user ID used for sharing. Gid = root # ID of the group used for sharing.

 

8.3) Restart xinetd and check the rsync listening port.

#/Etc/init. d/xinetd start # Start the super daemon process. Starting xinetd: [OK] # SS-tnl # rsync listens to port State Recv-Q send-Q local address: Port peeraddress: Port listen 0 64: 873 on port 873


9) The customer host pushes data to the rsync service host.

Switch to host 172.16.32.10

# rsync /etc/fstab 172.16.32.11::tools

 

10.) Go Back To The Rsync host and check the data under/backup/data.

# Ls/backup/data/# rsync host view fstab

 

11.) Another method is rsync-based push.

# Rsync-A/etc/PAM. d rsync: // 172.16.32.11/tools # ls/backup/data/# rsync host view Pam. d

 

12.) the remote host pulls data from the rsync host.

# Rsync-A 172.16.32.11: Tools // TMP # ls/tmp/# rsync host operation Pam. d


3. Rsync + inotify synchronize data from time to time.

1.) infographic introduction:

Inotify has been added to the kernel on Linux Kernel 2.6.13. It is a monitoring tool that can track changes in file data. It integrates rsync to synchronize changed data from time to time.

 

2) view the kernel version and the location of the infographic file.

# uname -r2.6.32-431.el6.x86_64# ls -l /proc/sys/fs/inotify/total 0-rw-r--r-- 1 root root 0 Aug 20 17:26max_queued_events-rw-r--r-- 1 root root 0 Aug 20 17:26max_user_instances-rw-r--r-- 1 root root 0 Aug 20 17:26max_user_watches

 

3) inotify-tools is installed on the client to synchronize data between two host directories.

# Rpm-QL inotify-tools/usr/bin/inotifywait # command to monitor file changes. /Usr/bin/inotifywatch # monitors time changes and access times ....


4.) inotify Command Options.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/53/wKioL1P4UlTTucvuAADoW3qH_ec589.jpg "style =" float: none; "Title =" inotifycommand example .jpg "alt =" wkiol1p4ulttucvuaadow3qh_ec589.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/53/wKioL1P4VuXB5r-0AALz25VECMw675.jpg "Title =" newinotify.jpg "alt =" wKioL1P4VuXB5r-0AALz25VECMw675.jpg "/>


5) Add the inotify monitoring script to synchronize data from time to time.

# vim /usr/local/bin/inotify.sh#!/bin/bash/usr/bin/inotifywait -mrq --timefmt ‘%d/%m/%y%H:%M‘ --format ‘%T %w%f‘ -e modify,delete,create,attrib /data/ | while readfiledo/usr/bin/rsync -vzrtopg --delete --progress /data/  [email protected]::toolsecho "${files} was rsynced" >>/var/log/rsync.log 2>&1done


6.) Add a data entry to the client to test whether the data is synchronized between the rsync + inotify client and the rsync server.

Run bash-X to view the script execution process.

# Bash-x/usr/local/bin/inotify. sh + Read File +/usr/bin/inotifywait-mrq -- timefmt '% d/% m/% Y % H: % m' -- format' % T % w % F'-e modify, delete, create, attrib/data/# Sending incremental file list when I create a nihao file on the client. /nihao/# the newly created data sent 68 bytes provisioned ed 16 bytes 168.00 Bytes/sectotal size is 0 speedup is 0.00 + echo 'was rsynced' + Read File


7) Go to the rsync server to check whether the synchronization is performed.

# ls /backup/data/nihao


You can define a Cron task plan and execute it once every minute.

*/1 *****/bin/sh/usr/local/bin/inotify. Sh &>/dev/null



4. Pull Linux data on the Windows rsync client host for synchronization.

The Windows operating system is windwosserver 2008 R2.

1) install rsync software in advance, Windows version.

2) use powershell of the 2008 System to view the rsync port number. Check whether the instance is started.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/52/wKiom1P4U2bDEALyAAWrRzOM2hg628.jpg "Title =" 2014-08-23_095505.jpg "alt =" wkiom1p4u2bdealyaawrrzom2hg628.jpg "/>


3.) Start pulling data from the Linux host.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/52/wKiom1P4U5izlKuNAAHixAzqEhk844.jpg "style =" float: none; "Title =" win_rsync.jpg "alt =" wkiom1p4u5izlkunaahixazqehk844.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/53/wKioL1P4VLPiv7xgAAG7eh72wm4752.jpg "style =" float: none; "Title =" 2014-08-23_160238.jpg "alt =" wkiol1p4vlpiv7xgaag7eh72wm4752.jpg "/>


Data can be synchronized.


Conclusion: even if the data is deleted on the rsync server, it does not matter. During the next script execution, the missing data will be supplemented together. Rsync user verification is not used here. Generally, there are two NICs on the server, and the Intranet Nic is used for data synchronization. The external Nic must support online services. Since the Intranet Nic is used, the rsync host also defines valid network segments for data synchronization, so user authentication is not performed.

 


This article from "you smile to anyone" blog, please be sure to keep this source http://54276311.blog.51cto.com/9130197/1543817

Rsync + inotify enables data synchronization from time to time and cross-system data synchronization.

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.