首页 > 其他分享 >使用rsync+inotify实现/www目录实时同步

使用rsync+inotify实现/www目录实时同步

时间:2023-10-07 15:26:59浏览次数:30  
标签:rsync inotify -- backup www client file root

 

#rsync 常用于做为 linux系统下的数据镜像备份工具,实现远程同步,支持本地复制,或者与其他SSH、rsync主机同步数据,支持增量备份,配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时数据同步
官方网站: http://rsync.samba.org/
软件包:rsync,rsync-daemon(CentOS 8)
服务文件:/usr/lib/systemd/system/rsyncd.service
配置文件:/etc/rsyncd.conf
端口:873/tcp

backup服务器 10.0.0.150
client服务器 10.0.0.152

[root@backup ~]#dnf -y install rsync-daemon
[root@backup ~]#vim /etc/rsyncd.conf
uid = root
gid = root
#port = 874
#use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
pid file = /var/run/rsyncd.pid
log file = /var/run/rsyncd.log
lock file = /var/run/rsyncd.lock
reverse lookup = no
#hosts allow = 10.0.0.0/24
[bkup]  #每个模块名对应一个不同的path目录,如果同名后面模块生效
path = /data/bkup/
comment = backup dir
read only = no #默认为yes,即只读
auth users = rsyncuser #默认anonymous可以访问rsync服务器
secrets file = /etc/rsync.pas

[root@backup ~]#mkdir /data/bkup -p
[root@backup ~]#echo "rsyncuser:123456" > /etc/rsync.pas
[root@backup ~]#chmod 600 /etc/rsync.pas
[root@backup ~]#systemctl start rsyncd


#客户端配置
[root@client ~]# yum install -y inotify-tools
[root@client ~]# yum install rsync -y
[root@client ~]# echo "123456" >/etc/rsync.pas
[root@client ~]# chmod 600 /etc/rsync.pas
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::
bkup backup dir
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::bkup
drwxr-xr-x 6 2021/11/18 21:13:32 .

#同步测试
#推数据
[root@client ~]# mkdir /data/www
[root@client ~]# echo test2 >/data/www/dd.txt
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::bkup
drwxr-xr-x 23 2021/11/18 21:46:28 .
-rw-r--r-- 5 2021/11/18 21:46:28 index.txt
[root@client ~]# rsync -avz --delete --password-file=/etc/rsync.pas /data/www/ [email protected]::bkup
sending incremental file list
deleting index.txt
./
dd.txt

sent 118 bytes received 51 bytes 338.00 bytes/sec
total size is 6 speedup is 0.04
#拉数据
[root@backup ~]#echo test > /data/bkup/index.txt
[root@backup ~]#cat /data/bkup/index.txt
test
[root@client ~]# rsync -avz --delete --password-file=/etc/rsync.pas [email protected]::bkup /data/www/
receiving incremental file list
./
index.txt

sent 46 bytes received 141 bytes 374.00 bytes/sec
total size is 11 speedup is 0.06
[root@client ~]# ll /data/www
total 8
-rw-r--r-- 1 root root 6 Nov 18 21:47 dd.txt
-rw-r--r-- 1 root root 5 Nov 18 21:50 index.txt

#在数据服务器上创建inotify_rsync.sh脚本
说明:此脚本执行前先确保两主机初始数据处于同步状态,此脚本实现后续的数据同步
[root@client ~]# vim inotify_rsync.sh
f#!/bin/bash
SRC='/data/www/'
DEST='[email protected]::backup'
rpm -q rsync &> /dev/null || yum -y install rsync
inotifywait  -mrq  --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATETIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete  --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done

[root@client ~]# bash inotify_rsync.sh
[root@client www]# touch 11
[root@backup bkup]#ll
total 8
-rw-r--r-- 1 root root 0 Nov 18 22:06 11
-rw-r--r-- 1 root root 6 Nov 18 21:47 dd.txt
-rw-r--r-- 1 root root 5 Nov 18 21:50 index.txt

[root@client ~]# tail -f /var/log/changelist.log
At on , file 22:06:32/data/www/ 11 was backuped up via rsync
At on , file 22:06:32/data/www/ 11 was backuped up via rsync
At on , file 22:06:32/data/www/ 11 was backuped up via rsync

#在客户端/data/www目录下创建文件11,在backup服务器上查看同步成功

标签:rsync,inotify,--,backup,www,client,file,root
From: https://www.cnblogs.com/tanll/p/17746369.html

相关文章

  • 配置samba共享,实现/www目录共享
    #服务器端配置:[root@samba~]#yuminstall-ysamba[root@samba~]#groupadd-rwww[root@samba~]#useradd-s/sbin/nologin-Gwwwtest1[root@samba~]#smbpasswd-atest1NewSMBpassword:RetypenewSMBpassword:Addedusertest1.[r......
  • rsync 远程同步
    一、rsync远程同步简介rsync(RemoteSync,远程同步)是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。在远程同步任务中,负责发起rsync同步操作的客户机......
  • Rsync 远程同步
    目录1.配置rsync源服务器1.1191.168.175.1081.2191.168.175.1092.发起端配置rsync+inotify192.168.175.108配置rsync源服务器192.168.175.109配置rsync源服务器192.168.175.110发起端配置rsync+inotify1.配置rsync源服务器1.1191.168.175.108vim/etc/......
  • rsync远程同步
    rsync远程同步nfs不太安全、scp是全量复制rsync是快速增量备份工具remotesync,远程同步官网:http://rsync,samba.org下行同步:(推)服务端发出命令,源地址是服务端,目的地址是客户端(从源服务器拉数据)上行同步:(拉)客户端发出命令,源地址是客户端,目的地址是服务端(推数据到源服务器)定时......
  • centos搭建rsync远程同步服务器
    rsync(remotesynchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用rsync同步本地硬盘中的不同目录。 rsync和scp的区别:rsync支持增量同步,不管是文件数量的新增还是文件内容的新增,scp不行注意事项:1、centos默认已经安装rsync了,输入命令rsync查......
  • 【转载https://www.cnblogs.com/niuben/p/12017242.html】Linux top命令详解
    Linuxtop命令详解转载出处:https://www.cnblogs.com/niuben/p/12017242.htmltop参数详解第一行,任务队列信息,同uptime命令的执行结果系统时间:07:27:05运行时间:up1:57min,当前登录用户:3user负载均衡(uptime)loadaverage:0.00,0.00,0.00average后面的三个数分......
  • linux的rsync同步功能
    环境centos7.9,rsync3.1.2介绍rsync是一种强大的数据备份和同步工具,能够在本地或远程系统之间复制和同步文件和目录。rsync可用于备份数据、同步目录、传输文件等,并具有高效、灵活和安全的特性。使用rsync的基本使用方法很简单,以下是其基本命令格式:rsync[options]sourced......
  • CentOS7 双机 rsync出错
    CentOS7双机rsync出错问题一:rsync推数据的时候,被推送的机器上必须要有rsync指令问题二:rsync推送清理原本缓存数据......
  • COMP3322 notes P1 - Internet & WWW Basic
    选这门课完全是为了推进我博客美化的大业!希望学完之后updatelogs里的一部分issues能自己亲手解决。首先来到InternetandWWWbasic:这些基本的network知识对接下来的front-endframework学习大有裨益。Internet,Web,DNS,HTTP等「最熟悉的陌生人」在这一节得以祛......
  • 关于getClass().getClassLoader().getResourceAsStream——转载自https://www.cnblogs
    关于getClass().getClassLoader().getResourceAsStreamInputStreamis=getClass().getClassLoader().getResourceAsStream("helloworld.properties");getClass():取得当前对象所属的Class对象getClassLoader():取得该Class对象的类装载器类装载器负责从Java字符文件将字符流读......