rsync+inotify

  • Post author:
  • Post category:其他




1.rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他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命令的常用选项

//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.157.19* 应用:rsync,inotify-tools,脚本;
* 操作系统:Redhat7

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



6.实例


需求:


部署rsync+inotify同步/etc目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/etc目录同步至目标服务器的/tom/下


在目标服务器上进行以下操作:

	//关闭防火墙与selinux
	[root@xaii-server ~]# systemctl stop firewalld
	[root@xaii-server ~]# setenforce 0
	[root@xaii-server ~]# getenforce 
	Permissive
	[root@xaii-server ~]# 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@xaii-server ~]# yum -y install rsync
	安装过程省略...


	//设置rsyncd.conf配置文件
	[root@xaii-server ~]# vim /etc/rsyncd.conf
	[root@xaii-server ~]# cat /etc/rsyncd.conf 
	log file = /var/log/rsyncd.log  
	pidfile = /var/run/rsyncd.pid  
	lock file = /var/run/rsync.lock
	secrets file = /etc/rsync.pass
	
	[etc_from_client]
		path = /lizhao/ 
		comment = sync etc from client
		uid = root   
		gid = root    
		port = 873     
		ignore errors   
		use chroot = no  
		read only = no 
		list = no    
		max connections = 200 
		timeout = 600    
		auth users = JauLi 
		hosts allow = 192.168.157.19
		hosts deny = 192.168.1.1


	//配置用户认证文件
	[root@xaii-server ~]# vim /etc/rsync.password
	[root@xaii-server ~]# cat /etc/rsync.password
	JauLi:123456


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


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


在源服务器上做以下操作

	//关闭防火墙和selinux
	[root@xaii-client ~]# systemctl stop firewalld
	[root@xaii-client ~]# setenforce 0
	setenforce: SELinux is disabled
	[root@xaii-client ~]# 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@xaii-client ~]# yum -y install rsync
	安装过程省略...
	
	
	//创建认证密码文件,只需要密码,不需要用户
	[root@xaii-client ~]# vim /etc/rsync.password
	[root@xaii-client ~]# cat /etc/rsync.password 
	123456


	//设置文件权限,只设置文件所有者具有读取、写入权限即可
	[root@xaii-client ~]# chmod 600 /etc/rsync.password 
	[root@xaii-client ~]# ll /etc/rsync.password 
	-rw------- 1 root root 7 219 15:23 	/etc/rsync.password


	//在源服务器上创建测试目录/root/etc/test,然后在源服务器运行以下命令
	[root@xaii-client ~]# rsync -avH --port 873 --delete /root/etc/ JauLi@192.168.157.33::etc_from_client --password-file=/etc/rsync.password
	
	sending incremental file list
	./
	test/
	
	sent 49 bytes  received 15 bytes  6.10 bytes/sec
	total size is 0  speedup is 0.00


	//在目标服务器上查看,如果在/lizhao目录下,有test目录,则说明同步成功
	[root@xaii-server ~]# ll /lizhao
	总用量 0
	drwxr-xr-x. 2 root root 6 219 04:25 test


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


	//安装inotify-tools(如果本地源没有这个rpm,则需要搭建网络源下载)
	[root@xaii-client ~]# rpm -ivh inotify-tools-3.14-8.el7.x86_64.rpm 
	警告:inotify-tools-3.14-8.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
	准备中...                          ################################# [100%]
	正在升级/安装...
	   1:inotify-tools-3.14-8.el7         ################################# [100%]


//写同步脚本,这是最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下


//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去

	//创建一个脚本,并修改权限为755
	[root@xaii-client ~]# mkdir /scripts
	[root@xaii-client ~]# touch /scripts/inotify.sh
	[root@xaii-client ~]# chmod 755 /scripts/inotify.sh 
	[root@xaii-client ~]# ll /scripts/inotify.sh 
	-rwxr-xr-x 1 root root 0 219 15:54 /scripts/inotify.sh


填写inotify.sh脚本

	[root@xaii-client ~]# vim /scripts/inotify.sh 
	[root@xaii-client ~]# cat /scripts/inotify.sh 
	host=192.168.157.33     //目标服务器的ip(备份服务器)
		src=/etc        //在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
		des=etc_from_client     //自定义的模块名,需要与目标服务器上定义的同步名称一致
		password=/etc/rsync.password        //执行数据同步的密码文件
		user=JauLi          //执行数据同步的用户名
		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 "${files} was rsynced" >>/tmp/rsync.log 2>&1
		done
//检查脚本语法是否有错,如果没错则先手动启动脚本
	[root@xaii-client ~]# bash -s /scripts/inotify.sh 
	[root@xaii-client ~]# nohup bash /scripts/inotify.sh &
	[1] 2710
	//nohup: 忽略输入并把输出追加到"nohup.out"
	
	
	[root@xaii-client ~]# ps -ef|grep inotify
	root       2671   2169  0 15:59 pts/0    00:00:00 bash -s /scripts/inotify.sh
	root       2699   2671  0 16:08 pts/0    00:00:00 bash -s /scripts/inotify.sh
	root       2710   2699  0 16:10 pts/0    00:00:00 bash /scripts/inotify.sh
	root       2711   2710  0 16:10 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
	root       2712   2710  0 16:10 pts/0    00:00:00 bash /scripts/inotify.sh
	root       2714   2699  0 16:10 pts/0    00:00:00 grep --color=auto inotify

//在源服务器上生成一个文件或者目录,然后查看inotify生成的日志
	[root@xaii-client ~]# echo "hello world">>/etc/httpd24/test
	[root@xaii-client ~]# tail /tmp/rsync.log 
	20190219 16:12 /etc/httpd24/testCREATE was rsynced
	[root@xaii-client ~]# cat /etc/httpd24/test 
	hello world

	
	//在目标服务器上进行验证
	[root@xaii-server ~]# ls /lizhao
	etc  test
	[root@xaii-server ~]# cat /lizhao/etc/httpd24/test
	hello world


设置脚本为开机自动启动

	[root@xaii-client ~]# chmod +x /etc/rc.d/rc.local 
	[root@xaii-client ~]# ll /etc/rc.d/rc.local 
	-rwxr-xr-x. 1 root root 473 627 2017 /etc/rc.d/rc.local
	[root@xaii-client ~]# vim /etc/rc.d/rc.local 
	[root@xaii-client ~]# tail -1 /etc/rc.d/rc.local 
	nohup /bin/bash /scripts/inotify.sh &


在源服务器删除一个文件,在目标服务器查看是否也是同步

	[root@xaii-client httpd24]# ls /etc/httpd24
	extra  httpd.conf  magic  mime.types  original  test
	[root@xaii-client httpd24]# rm -fr test
	[root@xaii-client httpd24]# ls
	extra  httpd.conf  magic  mime.types  original


	[root@xaii-server ~]# ls /lizhao/etc/httpd24
	extra  httpd.conf  magic  mime.types  original


至此,已经将源服务器上/etc/目录完整的同步到了目标服务器/lizhao/下



版权声明:本文为weixin_43695104原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。