linux服务器之间的文件同步 rsync+inotify

服务器 A

# 安装rsync
准备安装包 rsync-3.0.9.tar.gz   解压到自己喜欢的目录

# 如果没有 gcc 环境就先安装

[root@localhost ~]# yum -y install gcc

 

# 进入解压的目录,执行以下命令开始安装
-- 安装目录根据自己喜好修改,我的安装目录是 /opt/roadside

[root@localhost rsync-3.0.9]# ./configure --prefix /opt/roadside/rsync

 

#出现以下信息
configure.sh: creating ./config.status
config.status: creating Makefile
config.status: creating lib/dummy
config.status: creating zlib/dummy
config.status: creating popt/dummy
config.status: creating shconfig
config.status: creating config.h

    rsync 3.0.9 configuration successful

# 继续安装

[root@localhost rsync-3.0.9]# make
[root@localhost rsync-3.0.9]# make install

 

#安装完成,可以检查安装是否成功

[root@localhost rsync-3.0.9]# rsync -h

 

#建立密码认证文件(其中 "rsync" 可以自己设置密码,rsync.pwd 名字也可以自己设置)

[root@localhost rsync-3.0.9]# cd /opt/roadside/rsync
[root@localhost rsync]# echo "rsync" >/opt/roadside/rsync/rsync.pwd

 

#授权

[root@localhost rsync]# chmod 600 rsync.pwd

 


# 安装inotify
准备安装包  inotify-tools-3.13.tar.gz   解压到自己喜欢的目录

#进入解压的目录,执行以下命令开始安装

[root@localhost inotify-tools-3.13]# ./configure --prefix=/opt/roadside/inotify

 

#继续安装

[root@localhost inotify-tools-3.13]# make
[root@localhost inotify-tools-3.13]# make install


#创建脚本(脚本里面的目录根据实际情况修改)

#!/bin/bash 
host=192.168.130.129 #另外一台服务器地址
src=/opt/roadside/test/    #被监控目录
des=web
user=webuser
/opt/roadside/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
/usr/bin/rsync -va --delete --progress --password-file=/opt/roadside/rsync/rsync.pwd $src $user@$host::$des 
echo "${files} was rsynced" >>/opt/roadside/test/rsync.log 2>&1 
done


#脚本命名为 rsync.sh,放到监控的目录里,比如我的就放到 /opt/roadside/test 下面,并给予764权限

[root@localhost test]# chmod 764 rsync.sh

 

# 可以通过以下命令添加到开机自启动

[root@localhost test]# echo "/opt/roadside/test/rsync.sh" >> /etc/rc.local

 

-----------------------------------------------------------------------------------------------------------

服务器 B (备份服务器)

#安装rsync(备份服务器只安装rsync)....... 安装步骤同上
-- 安装目录根据自己喜好修改,我的安装目录是 /opt/cloud

# 解压 rsync-3.0.9.tar.gz ,进入解压目录

# 执行以下命令开始安装

[root@localhost rsync-3.0.9]# ./configure --prefix /opt/cloud/rsync

 

#出现以下信息

configure.sh: creating ./config.status
config.status: creating Makefile
config.status: creating lib/dummy
config.status: creating zlib/dummy
config.status: creating popt/dummy
config.status: creating shconfig
config.status: creating config.h

    rsync 3.0.9 configuration successful

#继续安装

[root@localhost rsync-3.0.9]# make
[root@localhost rsync-3.0.9]# make install

 

#建立用户与密码认证文件(在服务器 A 端建立的密码文件,只有密码,没有用户名;而在备份服务端B 里建立的密码文件,用户名与密码都有)

[root@localhost rsync-3.0.9]# cd /opt/cloud/rsync
[root@localhost rsync]# echo "webuser:rsync" > /opt/cloud/rsync/rsync.pwd

 

# 授权

[root@localhost rsync]# chmod 600 rsync.pwd

 


# 创建配置文件

uid = root 
gid = root 
use chroot = no 
max connections = 10 
strict modes = yes
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log 
[web] 
path = /opt/cloud/test/
comment = web file
ignore errors 
read only = no 
write only = no 
hosts allow = 192.168.130.128
hosts deny = * 
list = false
uid = root 
gid = root 
auth users = webuser 
secrets file = /opt/cloud/rsync/rsync.pwd
[forum]
path = /opt/roadside/test
comment = web
ignore errors
read only = no
list = no
transfer logging = no


#把配置文件命名为 rsync.conf,放到 /opt/cloud/rsync/ 目录里

#服务器 B 启动 rsync 

[root@localhost rsync]# /opt/cloud/rsync/bin/rsync --daemon --config=/opt/cloud/rsync/rsync.conf

#检查启动状态

[root@localhost rsync]# ps aux | grep rsync

#也可以添加到开机自启动

[root@localhost rsync]# echo "/opt/cloud/rsync/bin/rsync --daemon --config=/opt/cloud/rsync/rsync.conf" >> /etc/rc.local

#启动服务器 A 的 inotifywait

[root@localhost roadside]# /opt/roadside/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%H' --format '%T %w%f%e' -e modify,delete,create,attrib /opt/roadside/test/


#启动服务器 A 的 rsync (脚本有问题,启动报错,忽略这一步吧)

[root@localhost test]# sh rsync.sh

 

******************************************************************************************************************************************

脚本不好使,暂时没找到原因,使用以下命令可以同步目录(在 B 服务器的 /opt/cloud/test 目录下增加文件时,执行一下下面这条指令,发现服务器 A 的 /opt/roadside/test 目录下也多出了相同的文件,流程跑通了,很 OK !!!)
#服务器 A 执行

[root@localhost test]# rsync -auv --delete --password-file=/opt/roadside/rsync/rsync.pwd webuser@192.168.130.129::web  /opt/roadside/test/

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下内容请忽略

按照项目实际情况, 服务器 B 是巡检车, 服务器 A 是云平台, 所以以上笔记中出现目录的地方把 roadside 和 cloud 调换一下即可

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值