rsync+inotify realizes real-time synchronization of the server


One, test

Server  IP-addr status    tool installation        system version     operation directory

Kefuduan 192.168.1.161 client inotify-tools centos7 / server / backup /

Server 192.168.1.150 server rsync centos7 /server/backup/ 

Two , placement

1 , a server (server) Configuration

(Server IP) : 192.168.1.150

(1)  Install  rsync (The backup server only installs  rsync and needs to be started)

You can run the command rpm -aq rsync or  rsync -v  to view, the following information indicates that rsync has been installed 

[root@localhost yang]# rpm -qa rsync

rsync-3.1.2-6.el7_6.1.x86_64

(2) If there is no default installation, it is recommended to run the following command

yum install rsync

Create the rsyncd.conf  file in the rsync directory you installed  and write the following information

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

uid = root

gid = root

use chroot = no

max connections = 10

strict modes = yes

hosts allow = 192.168.1.161

port = 873

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[inotify]

path = /server/backup/

ignore errors

read only = no

write only = no

hosts allow = 192.168.1.161

hosts deny = *

list = false

uid = root

gid = root

auth users = root

secrets file = /etc/rsyncd/rsync.password 

(3) Establish user and password authentication files

Use the  root  user to operate, and the custom password is  root @mzla2019  ( this password is not a login password, but a  credential for rsync  to interact with the  client  server, which can be customized )

Create  rsync.password  file and write [Username: Password], remember the password here!

[root@localhost rsyncd]# echo "root:root@mzla2019"> /etc/rsyncd/rsync.password

Need to give the password file  600  permissions

[root@localhost rsyncd]# chmod 600 /etc/rsyncd/rsync.password

(4)  Start  rsync

Start the  rsync  server as a daemon

rsync --daemon

 [root@localhost rsyncd]rsync --daemon

 [root@ localhost  rsyncd] # netstat -nltp |grep rsync  can also check whether it is started 

2 ,  (Client) Configuration

( Client IP ) : 192.168.1.161

(1)  Install  rsync

(2) Establish a password authentication file for accessing the server

Created the directory /etc/rsyncd/

 [root@localhost rsyncd]echo "root@mzla2019" >/etc/rsyncd/rsync.password   

#其中 `root@mzla2019可以自己设置密码,rsync.password 名字也可以自己设置

设置权限为只读

 [root@localhost rsyncd]# chmod 600 rsync.password 

(3)安装 inotify-tools

查看当前系统是否支持 inotify

 [root@localhost etc]# ll /proc/sys/fs/inotify/

总用量 0

-rw-r--r--. 1 root root 0 12月  5 20:12 max_queued_events

-rw-r--r--. 1 root root 0 12月  5 20:12 max_user_instances

-rw-r--r--. 1 root root 0 12月  5 20:12 max_user_watches

依次执行如下命令

[root@localhost rsyncd]# wget

http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@localhost rsyncd]# tar zxvf inotify-tools-3.14.tar.gz  

[root@localhost rsyncd]#cd inotify-tools-3.14  

[root@localhos inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify  

[root@localhos inotify-tools-3.14]# make  

[root@localhos inotify-tools-3.14]# make install

(4) 创建 rsync 复制脚本

此项功能主要是将 client 端的目录 /server/backup/ 里的内容,如果修改了(无论是添加、修改、删除文件)能够通过 inotify 监控到,并通过 rsync 实时的同步给 server 的 /server/backup/ 

创建 inotify.sh 脚本文件

[root@localhost server]# vim inotify.sh

#!/bin/bash

host=192.168.1.150

src=/server/backup/

des=inotify

password=/etc/rsyncd/rsync.password

user=root

inotify=/usr/local/inotify

 

${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

rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des

echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

done

保存文件后,为 .sh 脚本增加 764 权限

[root@localhost server]# chmod 764 inotify.sh

运行脚本

[root@localhost server]# sh inotify.sh &

[1] 1925

echo "setsid /server/inotify.sh &" >> /etc/rc.local 

3 功能测试

192.168.1.161

[root@localhost backup]# touch {1..10}.txt

[root@localhost backup]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

  

图片

查看192.168.1.150服务器

[root@localhost ~]# cd /server/backup/

[root@localhost backup]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt


Guess you like

Origin blog.51cto.com/15127516/2658345