CentOS uses Inotify+rsync for file synchronization

Source: Internet
Author: User
Tags inotify rsync

1. Environment deployment

Inotify-master 10.10.6.208

Inotify-slave 10.10.6.149

2, two servers are installed Rsync

Yum Install-y rsync

3. Inotify-slave Deployment

Create a new rsync user and module catalog and change its user groups

Useradd rsync-s/sbin/nologin #添加rsync用户
Mkdir/usr/local/backup #创建rsync Daemon Module directory for working mode
Chown Rsync.rsync/usr/local/backup #更改模块目录的用户组

Writing an rsync profile/etc/rsyncd.conf
Vi/etc/rsyncd.conf

Strict modes =yes #是否检查口令文件的权限
Port = 873 #默认端口873
Log file =/var/log/rsyncd.log #日志记录文件 The original, I did not use
PID file =/var/log/rsyncd.pid #运行进程的ID写到哪里

[Test]
Max connections = #客户端最大连接数, default 0 (No limit)
UID = rsync #指定该模块传输文件时守护进程应该具有的uid
GID = rsync #指定该模块传输文件时守护进程应该具有的gid
Path =/usr/local/backup/# The directory that needs to be backed up
Ignore errors # can ignore some unrelated IO errors
Read Only = no #no客户端可上传文件, yes read-only
Write only = no #no客户端可下载文件, yes cannot be downloaded
Hosts allow = * #充许任何主机连接
Hosts deny = 10.5.3.77 #禁止指定的主机连接
Auth users = rsync_backup # Virtual User
Secrets file =/etc/rsync.password # Virtual user password files location

Configure a virtual user's password file
echo "rsync_backup:123456" >/etc/rsync.password# Note: Rsync_backup is a virtual user, 123456 is the password for this virtual user
chmod 600/etc/rsync.password #为密码文件提权 for added safety

Start the Rsync service
Rsync--daemon--config=/etc/rsyncd.conf

4. Inotify-master Deployment

Installing Inotify-tools
Tar zxvf Inotify-tools-3.13.tar
CD inotify-tools-3.13
./configure--prefix=/usr/local/inotify-tools
Make
Make install

Test push
echo "123456" >/etc/rsync.password
chmod 600/etc/rsync.password
echo "Hello Dsideal" >test.txt
Rsync-avz test.txt [Email protected]::test--password-file=/etc/rsync.password
Sending incremental file list
Sent bytes received 8 bytes 76.00 bytes/sec
Total size was speedup is 0.37

The test was successful and the/usr/local/backup directory file for the 10.10.6.149 was synchronized.

Write a monitoring script and load it into the background execution

VI inotify.sh

#!/bin/bash
#para
host01=10.10.6.149
src=/usr/local/backup/#本地监控的目录
Dst=test # The module name of the Rsync service for Inotify-slave
User=rsync_backup #inotify Virtual user of the Rsync service for-slave
rsync_passfile=/etc/ Rsync.password #本地调用rsync服务的密码文件
Inotify_home=/usr/local/inotify-tools #inotify的安装目录
#judge
If [!-e "$src "] \
| | [!-e "${rsync_passfile}"] \
| | [!-e "${inotify_home}/bin/inotifywait"] \
| | [!-e "/usr/bin/rsync"];
then
echo "Check File and Folder"
Exit 9
Fi
${inotify_home}/bin/inotifywait-mrq--timefmt '%d/%m/%y%H :%M '--format '%T%w%f '-E close_write,delete,create,attrib $src | While the read file
do
CD $src &&/usr/bin/rsync-aruz-r--delete./[email protected] $host:: $dst--pas Sword-file=${rsync_passfile} >/dev/null 2>&1
echo "${files} was rsynced" >>/tmp/rsync.log 2> &1
Echo "---------------------------------------------------------------------------"
Done
Exit 0


# sh inotify.sh & #将脚本加入后台执行

CentOS uses Inotify+rsync for file synchronization

Related Article

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.