05-Sersync 实时同步
Sersync 实时同步
1. 实时同步概述
1.1 什么是实时同步
- 实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目录同步至远程服务器。
1.2 为什么要实时同步
- 保证数据的连续性,解决nfs单点故障,nfs内的数据可以实时同步到远程服务器上,如果共享机nfs挂了,那么远程服务器上还有nfs共享盘的备份,保证数据不会丢失。
1.3 实时同步工具选择
-
sersync+rsync
-
Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份,并针对目录或文件的变化及时作出响应。rsync+inotify可以实触发式实时同步增量备份 sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。 sersync项目地址:https://github.com/wsgzao/sersync
2. 实时同步实践
2.1 实践题目
构思环境的话就是web01里有数据库文件,数据库每日备份发送到backup的backup模块下,web01做的挂载的存储目录,增量备份在backup的data模块
-
案例: 实现web01上传文件,实则是写入NFS至存储,当NFS存在新的数据则会实时的复制到备份服务器 1.web01上传文件至NFS存储 2.web01下的/etc/passwd备份在备份服务器的/backup 3.如何将NFS数据实时同步到备份服务器的/data目录
-
角色 内网 安装工具 客户端 web01 10.0.0.7 yum -y install nfs-utils 共享存储 NFS 10.0.0.31 yum -y install nfs-utils yum -y install rsync inotify 备份服务器 backup 10.0.0.41 yum -y install rsync
2.2 实践步骤
1. web01上传文件至NFS存储
-
1.在nfs主机上下载 yum -y install nfs-utils [root@nfs ~]# yum -y install nfs-utils 2.配置nfs服务文件 [root@nfs ~]# cat /etc/exports /data 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666) 3.当客户端访问时,默认映射为匿名用户,所以我们需要给用户指定属主和属组,并创建这个虚拟用户 [root@nfs ~]# groupadd -g666 niubi [root@nfs ~]# useradd -u666 -g666 -s /sbin/nologin -M niubi [root@nfs ~]# id niubi uid=666(niubi) gid=666(niubi) 组=666(niubi) 4.创建共享文件夹/data更改属主属组为HAHA [root@nfs ~]# mkdir /data [root@nfs ~]# chown -R niubi.niubi /data [root@nfs ~]# ll -d /data/ drwxr-xr-x 2 niubi niubi 6 2月 16 14:53 /data/ 5.开启并重启服务 [root@nfs ~]# systemctl start nfs [root@nfs ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@nfs ~]# systemctl status nfs 6.验证nfs服务是否正常 [root@nfs ~]# cat /var/lib/nfs/etab /data 10.0.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash) 7.在客户端web01上下载 yum -y install nfs-utils,无需开启 [root@web01 ~]# yum -y install nfs-utils 8.在客户端web01上测试nfs共享机的服务 [root@web01 ~]# showmount -e 10.0.0.31 Export list for 10.0.0.31: /data 10.0.0.0/24 9.nfs共享机的/data目录挂载到客户端web01的/mnt目录 [root@web01 ~]# mount -t nfs 10.0.0.31:/data /mnt [root@web01 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 devtmpfs 476M 0 476M 0% /dev tmpfs 487M 0 487M 0% /dev/shm tmpfs 487M 7.7M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda3 18G 2.0G 16G 11% / /dev/sda1 197M 110M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 10.0.0.31:/data 18G 2.0G 16G 11% /mnt 10.在web01上传文件到nfs共享机的/data [root@web01 ~]# cd /mnt/ [root@web01 mnt]# rz -E rz waiting to receive. [root@nfs ~]# ll /data/ 总用量 992 -rw-r--r-- 1 niubi niubi 1013937 2月 8 14:32 DAY_40_LNMP架构演变(1).pdf # web01上传文件至NFS存储成功
2. web01下的/etc/passwd备份在备份服务器的/backup
-
1.在backup服务器下载 yum -y install rsync [root@backup ~]# yum -y install rsync 2.配置rsync的配置文件 [root@backup ~]# cat /etc/rsyncd.conf uid = niubi gid = niubi port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.passwd [backup] path = /backup [data] path = /data 3.按照配置文件配置账户 1)创建虚拟用户niubi [root@backup ~]# useradd -s /sbin/nologin -M niubi [root@backup ~]# id niubi uid=1000(niubi) gid=1000(niubi) 组=1000(niubi) 2) 创建 /etc/rsync.passwd 密码文件输入# rsync_backup:123456 修改文件权限为600 [root@backup ~]# echo rsync_backup:123456 > /etc/rsync.passwd [root@backup ~]# chmod 600 /etc/rsync.passwd [root@backup ~]# cat /etc/rsync.passwd rsync_backup:123456 3) 创建/backup目录和/data目录并修改属主属组为HAHA [root@backup ~]# mkdir -p /backup /data [root@backup ~]# chown niubi.niubi /backup/ [root@backup ~]# chown niubi.niubi /data [root@backup ~]# ll -d /backup/ /data/ drwxr-xr-x 2 niubi niubi 6 2月 16 15:06 /backup/ drwxr-xr-x 2 niubi niubi 6 2月 16 15:06 /data/ 4.启动并设置开机自启rsyncd服务 [root@backup ~]# systemctl start rsyncd [root@backup ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. 5.将web01的/etc/passwd文件备份到backup备份服务器的backup模块 # 需要在web01上设置/etc/rsync.passwd root@web01 21:50:02 ~ # rsync -avz /etc/passwd rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.passwd sending incremental file list passwd sent 587 bytes received 43 bytes 1,260.00 bytes/sec total size is 1,162 speedup is 1.84 6.在backup备份服务器的backup模块查看 [root@backup ~]# ll /backup/ 总用量 4 -rw-r--r-- 1 niubi niubi 1162 2月 16 14:57 passwd # web01下的/etc/passwd备份在备份服务器的/backup完成
3. 如何将NFS数据实时同步到备份服务器的/data目录
-
1.在nfs共享机上安装sersync # sersync需要依赖inotify和rsync,所以需要安装对应软件 [root@nfs ~]# yum -y install rsync inotify 2.创建目录并安装sersync [root@nfs ~]# mkdir /server/tools -p [root@nfs tools]# cd /server/tools/ [root@nfs tools]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz 3.解压安装包并修改文件目录 [root@nfs tools]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs tools]# ll 总用量 712 drwxr-xr-x 2 root root 41 10月 26 2011 GNU-Linux-x86 -rw-r--r-- 1 root root 727290 2月 16 15:30 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs tools]# mv GNU-Linux-x86/ sersync [root@nfs tools]# ll 总用量 712 drwxr-xr-x 2 root root 41 10月 26 2011 sersync -rw-r--r-- 1 root root 727290 2月 16 15:30 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs tools]# cd sersync/ [root@nfs sersync]# ll 总用量 1772 -rwxr-xr-x 1 root root 2214 10月 26 2011 confxml.xml -rwxr-xr-x 1 root root 1810128 10月 26 2011 sersync2 4.配置confxml.xml文件 [root@nfs sersync]# cat -n confxml.xml 1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <head version="2.5"> 3 <host hostip="localhost" port="8008"></host> 4 <debug start="false"/> 5 <fileSystem xfs="true"/> # <!-- 文件系统 --> 6 <filter start="false"> # <!-- 排除不想同步的文件--> 7 <exclude expression="(.*)\.svn"></exclude> 8 <exclude expression="(.*)\.gz"></exclude> 9 <exclude expression="^info/*"></exclude> 10 <exclude expression="^static/*"></exclude> 11 </filter> 12 <inotify> # <!-- 监控的事件类型 --> 13 <delete start="true"/> 14 <createFolder start="true"/> 15 <createFile start="true"/> 16 <closeWrite start="true"/> 17 <moveFrom start="true"/> 18 <moveTo start="true"/> 19 <attrib start="false"/> 20 <modify start="false"/> 21 </inotify> 22 23 <sersync> 24 <localpath watch="/data/"> # <!-- 监控的目录,监控本地的data目录。 --> 25 <remote ip="10.0.0.41" name="data"/> # <!-- backup的IP以及模块 --> 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> 29 <rsync> # <!-- rsync的选项 --> 30 <commonParams params="-avz"/> # <!-- 参数 --> 31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> # 自动开启 # 用户名 # 密码文件 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="false"/> 35 </rsync> 36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> # <!-- 每60分钟执行一次同步--> 37 <crontab start="false" schedule="600"><!--600mins--> 38 <crontabfilter start="false"> 39 <exclude expression="*.php"></exclude> 40 <exclude expression="info/*"></exclude> 41 </crontabfilter> 42 </crontab> 43 <plugin start="false" name="command"/> 44 </sersync> 45 46 <plugin name="command"> 47 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> 48 <filter start="false"> 49 <include expression="(.*)\.php"/> 50 <include expression="(.*)\.sh"/> 51 </filter> 52 </plugin> 53 54 <plugin name="socket"> 55 <localpath watch="/opt/tongbu"> 56 <deshost ip="192.168.138.20" port="8009"/> 57 </localpath> 58 </plugin> 59 <plugin name="refreshCDN"> 60 <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 61 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 62 <sendurl base="http://pic.xoyo.com/cms"/> 63 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 64 </localpath> 65 </plugin> 66 </head> 5.在nfs共享机上创建rsync_backup验证密码文件 [root@nfs sersync]# echo 123456 > /etc/rsync.passwd [root@nfs sersync]# chmod 600 /etc/rsync.passwd 6.启动sersync服务守护进程 # 如果需要同步多个目录, 那么需要配置多套环境 # 相对路径 [root@nfs sersync]#./sersync2 -dro ./confxml.xml # 绝对路径 [root@nfs sersync]# /server/tools/sersync/sersync2 -dro /server/tools/sersync/confxml.xml 7.注意事项: 启动一次进程即可 不要重复启动,如果配置不正确 需要先杀死进程在重新启动 [root@nfs sersync]# ps axu|grep sersync root 17828 0.0 0.0 108716 728 ? Ssl 11:06 0:00 /server/tools/sersync/sersync2 -dro /server/tools/sersync/confxml.xml root 17885 0.0 0.0 92324 716 ? Ssl 11:23 0:00 /server/tools/sersync/sersync2 -dro /server/tools/sersync/confxml.xml root 17901 0.0 0.0 112704 956 pts/1 R+ 11:23 0:00 grep --color=auto sersync [root@nfs sersync]# kill -9 17828 17885 8.测试实时同步是否成功 1)先在web01上传文件到nfs共享 [root@web01 mnt]# cd /mnt [root@web01 mnt]# rz [root@web01 mnt]# ll 总用量 1888 -rw-r--r-- 1 666 666 917463 2月 10 17:27 20191027061644102.jpg -rw-r--r-- 1 666 666 1013937 2月 8 14:32 DAY_40_LNMP架构演变(1).pdf 2)到nfs共享机下的/data目录查看 [root@nfs sersync]# cd /data/ [root@nfs data]# ll 总用量 1888 -rw-r--r-- 1 niubi niubi 917463 2月 10 17:27 20191027061644102.jpg -rw-r--r-- 1 niubi niubi 1013937 2月 8 14:32 DAY_40_LNMP架构演变(1).pdf 3)到备份服务器backup下的data块 [root@backup ~]# ll /data/ # 命令的配置文件中设置了/data目录为实时备份目录 总用量 1888 -rw-r--r-- 1 niubi niubi 917463 2月 10 17:27 20191027061644102.jpg -rw-r--r-- 1 niubi niubi 1013937 2月 8 14:32 DAY_40_LNMP架构演变(1).pdf # 将NFS数据实时同步到备份服务器的/data目录设置成功 ---------------------------------------------------------------------------------------------- # 补充 root@nfs 18:10:33 sersync # ./sersync2 -dro ./confxml.xml 命令详解 # 查看启动参数 [root@nfs01 sersync]# ./sersync2 -h set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param _______________________________________________________ 参数-d:启用守护进程模式 参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍 参数-n: 指定开启守护线程的数量,默认为10个 参数-o:指定配置文件,默认使用confxml.xml文件 参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块 参数-m:单独启用其他模块,使用 -m socket 开启socket模块 参数-m:单独启用其他模块,使用 -m http 开启http模块 不加-m参数,则默认执行同步程序 # 执行命令 root@nfs 18:10:33 sersync # ./sersync2 -dro ./confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -d run as a daemon option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: ./confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 will ignore the inotify createFile event daemon start,sersync run behind the console use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data # 在命令的启动过程,会执行一次备份 cd /data && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 # 将nfs共享机的/data目录下的内容,同步到备份服务器的/data目录下
3. 实时同步总结
-
1.为什么要使用实时同步? 1).解决nfs单点 2).大量的静态资源迁移(本地迁移云端) 2.实时同步能解决什么问题? 1).平滑的迁移 2).备份:减少人为的干预 3.实时同步工具选择? rsync+inotify 少量文件同步,麻烦。同步大文件太慢,遍历扫描,非常影响效率。 sersync 配置简单,多线程同步,同步块。适合大量的小文件或者图片。 lsryncd 4.demo:用户上传文件-->web-->写入-->nfs存储-->inotify-->action-->rsync--->backup 用户上传文件-->web-->写入-->nfs存储(本地)--->实时的同步到-->存储(云端) web-->卸载存储(本地)--->重新挂载存储(云端)