Linux服务器之间的文件同步(单向同步:rsync+inotify)

1.rsync简介

rsync是linux系统下的数据备份工具。支持本地复制,或者与其他SSH、rsync主机同步。

2.rsync的部分特性

rsync支持很多特性:

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
  • 无须特殊权限即可安装
  • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
  • 支持匿名传输,以方便进行网站镜象

3.rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议

rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf

4.rsync命令的常用选项

  * -a, --archive       //归档
  * -v, --verbose       //啰嗦模式
  * -q, --quiet         //静默模式
  * -r, --recursive     //递归
  * -p, --perms         //保持原有的权限属性
  * -z, --compress      //在传输时压缩,节省带宽,加快传输速度
  * --delete            //在源服务器上做的删除操作也会在目标服务器上同步


5.rsync+inotify

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

示例环境:

源服务器:
* IP地址:192.168.1.83;
* 应用:rsync,inotify-tools,脚本;
* 操作系统:centos7

目标服务器:
* IP地址:192.168.1.82;
* 应用:rsync;
* 操作系统:centos7

6.实例

场景:部署rsync+inotify同步/etc/nginx/default.d目录至目标服务器的/etc/nginx/default.d下;

6.1 源服务器和目标服务器都需要操作:

	//关闭防火墙与selinux
	[root@cm2 ~]# systemctl stop firewalld
	[root@cm2 ~]# setenforce 0
	[root@cm2 ~]# getenforce 
	Permissive
	[root@cm2 ~]# systemctl disable firewalld
	Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
	Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.


	//安装rsync服务端软件
	[root@cm2 ~]# yum -y install rsync
	安装过程省略...

6.2 目标服务器上进行以下操作(192.168.1.82):

	//设置rsyncd.conf配置文件
	[root@cm2 ~]# vim /etc/rsyncd.conf 
	log file = /var/log/rsyncd.log
	pidfile = /var/run/rsyncd.pid
	lock file = /var/run/rsync.lock
	secrets file = /etc/rsync.password

	[sync_from_cm3]
	        path = /etc/nginx/default.d/
	        comment = sync nginx config file from cm3
	        uid = root
	        gid = root
	        port = 873
	        ignore errors
	        use chroot = no
	        read only = no
	        list = no
	        max connections = 200
	        timeout = 600
	        auth users = root
	        hosts allow = 192.168.1.83
	        hosts deny = 192.168.1.1


	//配置用户认证文件
	[root@cm2 ~]# vim /etc/rsync.password
	[root@cm2 ~]# cat /etc/rsync.password
	root:gmg_2019


	//设置文件权限
	[root@cm2 ~]# chmod 600 /etc/rsync.password
	[root@cm2 ~]# ll /etc/rsync.pass 
	-rw-------. 1 root root 13 2月  19 04:14 /etc/rsync.password


	//启动rsync服务并设置开机自启动
	[root@cm2 ~]# systemctl start rsyncd
	[root@cm2 ~]# systemctl enable rsyncd

6.3 源服务器上进行以下操作(192.168.1.83):

	//创建认证密码文件,只需要密码,不需要用户
	[root@cm2 ~]# vim /etc/rsync.password
	gmg_2019


	//设置文件权限,只设置文件所有者具有读取、写入权限即可
	[root@cm2 ~]# chmod 600 /etc/rsync.password 


	//在源服务器的目录/etc/nginx/default.d/操作文件,然后在源服务器运行以下命令
	[root@cm2 ~]# rsync -avH --port 873 --delete /etc/nginx/default.d/ root@192.168.1.82::sync_from_cm3 --password-file=/etc/rsync.password
	

	//安装inotify-tools工具,实时触发rsync进行同步
	//查看服务器内核是否支持inotify
	[root@cm2 ~]# ll /proc/sys/fs/inotify/
	总用量 0
	-rw-r--r-- 1 root root 0 2月  19 15:46 max_queued_events
	-rw-r--r-- 1 root root 0 2月  19 15:46 max_user_instances
	-rw-r--r-- 1 root root 0 2月  19 15:46 max_user_watches
	**如果有这个三个以max开头的文件,则表示服务器内核支持inotify**


	//下载并安装inotify-tools
	[root@cm2 ~]# wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/i/inotify-tools-3.14-9.el7.x86_64.rpm
	[root@cm2 ~]# rpm -ivh inotify-tools-3.14-8.el7.x86_64.rpm 

6.4 监控源服务器指定文件夹的文件操作和服务器之间的同步

这里编写/scripts/inotify.sh脚本(名字随便取,目录随便放)

# 编写监控和同步脚本
[root@cm3 ~]# vi /scripts/inotify.sh 
host=192.168.1.82
src=/etc/nginx/default.d/
des=sync_from_cm3
password=/etc/rsync.password
user=root
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %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 "rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des" >> /tmp/rsync.log
	echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done



# 修改可执行权限
[root@cm3 ~]# chmod 777 /scripts/inotify.sh 


# 启动脚本
nohup /bin/bash /scripts/inotify.sh &


# 设置脚本为开机自动启动
[root@cm3 ~]# vim /etc/rc.d/rc.local 
nohup /bin/bash /scripts/inotify.sh &

[root@cm3 ~]# chmod +x /etc/rc.d/rc.local

到此已经完成了,你可以修改源服务器指定文件夹内部的内容,就可以完成同步了

注:

对于文件夹中文件特别多的,会发现同步速度比较慢。可以参考一下文章

http://www.ttlsa.com/web/let-infotify-rsync-fast/


 

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值