rsync+inotify实现服务器之间的文件实时同步教程

三、 开始安装配置


  1. 环境:

系统版本:centos7

源服务器:10.99.123.19

从服务器:10.99.123.20

同步目录:/mnt

我们要实现让20服务器在19服务器上我们需要同步的文件发生改变的时候自动同步19服务器上的文件,一切以19服务器为主

  1. 安装rsync(同时操作两台服务器)
yum install rsync -y
  1. 关闭selinux(永久关闭)

比较坑爹弄不懂selinux这东西所以我选择永久关闭

vi /etc/selinux/config

修改成如下:


\# This file controls the state of SELinux on the system.
\# SELINUX= can take one of these three values:
\#     enforcing - SELinux security policy is enforced.
\#     permissive - SELinux prints warnings instead of enforcing.
\#     disabled - No SELinux policy is loaded.
\# SELINUX=enforcing
\# SELINUXTYPE= can take one of three two values:
\#     targeted - Targeted processes are protected,
\#     minimum - Modification of targeted policy. Only selected processes are protected.
\#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
SELINUX=disabled

重启服务器:

reboot
  1. 开发防火墙端口873(rsync的默认端口)

永久开启

firewall-cmd --permanent --add-port=873/tcp

查看端口状态

firewall-cmd --permanent --query-port=873/tcp

重启防火墙服务

systemctl restart firewalld.service
  1. 测试是否能执行同步

启动rsync服务

systemctl start rsyncd.service

添加开机自启

systemctl enable rsyncd.service

把19服务器的mnt文件下的所有文件推送到20服务器

rsync -azP --delete /mnt/ root@10.99.123.20:/mnt

结果(第一次会出现):

The authenticity of host '10.99.123.20 (10.99.123.20)' can't be established.
ECDSA key fingerprint is a2:ed:95:17:3e:63:c9:30:45:42:2d:d0:ac:3d:00:4c.
Are you sure you want to continue connecting (yes/no)? 

输入:yes

Warning: Permanently added '10.99.123.20' (ECDSA) to the list of known hosts.
root@10.99.123.20's password:

输入密码:你20服务器root用户的密码

sending incremental file list
./
test.conf
         453 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 303 bytes  received 34 bytes  96.29 bytes/sec
total size is 453  speedup is 1.34


同步成功!

  1. 不难发现,每次同步都需要输密码,这样我们实时同步就不能进行了,所以这里需要做一个免密码的操作

在19服务器上执行如下命令来生成配对密钥:

ssh-keygen -t rsa 

一路回车

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
16:dd:0d:50:7d:ea:99:73:46:79:23:1d:31:2d:7d:86 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo. =o|
|         . . oE.B|
|        . . . o=+|
|         .   ..+o|
|        S    ..+o|
|       .      = o|
|               + |
|                 |
|                 |
+-----------------+

把生成的密钥发送到20服务器

ssh-copy-id root@10.99.123.20

提示

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.99.123.20's password:

输入密码后:


Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.99.123.20'"
and check to make sure that only the key(s) you wanted were added.

测试是否可以免密码

ssh root@10.99.123.20

正常情况已经可以免密码了,那么接下来我们要去实现实时同步

  1. 实时同步我们需要在19服务器安装inotify

安装inotify

yum install inotify-tools --enablerepo=epel

如果提示:


已加载插件:fastestmirror


Error getting repository data for epel, repository not found

添加yum源(再安装)

 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm

编写同步脚本

脚本内容如下,保存为/etc/inotify-rsync.sh, 保存的路径自己可以根据情况定

set -e
src=/mnt/

target=10.99.123.20
/usr/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 -azP --delete /mnt/ root@10.99.123.20:/mnt

done


修改脚本权限

chmod 755 /etc/inotify-rsync.sh

执行脚本(加&为后台运行,不加则为前台执行)

/etc/inotify-rsync.sh&

将脚本执行添加到开机启动项

echo "/etc/inotify-rsync.sh&" >>/etc/rc.local

运行后面我们在源目录添加一新文件进行测试,很快就可以看到它出现在目标目录

也可以重启下,看看服务有没有自动重启

reboot
posted @ 2017-03-15 15:09  xiejianlong  阅读(90)  评论(0)    收藏  举报