✨rsync生产场景应用:
1. 两台服务器间或一对多定时同步
2. 企业全网备份,通过本地打包备份,然后rsync结合inotify应用把全网数据统一备份到一个固定存储服务器,然后在这台存储服务器上通过脚本检查并报警管理员备份结果
3. rsync结合inotify的功能做实时的数据同步
定时同步:rsync+cron
实时同步:rsync 或 sersync
✨工作方式
单个主机本地之间的数据传输(类似于cp命令的功能)
借助rcp,ssh等通道来传输数据(类似于scp命令的功能)
以守护进程(socker)的方式传输数据(这个是rsync自身的重要功能)
✨本地数据传输模式(local-only mode)
语法:rsync [OPTION...] SRC... [DEST]
Rsync 为同步命令
[OPTION] 同步时的参数
SRC 为源,即待拷的分区,文件或目录等
DEST 为目的分区、文件或目录等
实例1:
把系统的hosts 文件同步到/opt目录
[root@13-151 ~]# rsync /etc/hosts /opt/ #等同于cp /etc/hosts /opt [root@13-151 ~]# ls /opt/ hosts mha_soft
删除操作:由于/null是空目录,所以/data也被同步成空目录 [root@13-151 data]# ls /data/ /null/ /data/: asdadw #一个文件 /null/: [root@13-151 data]# rsync -r --delete /null/ /data/ [root@13-151 data]# ls /data/ /null/ /data/: /null/: [root@13-151 data]#
✨拉复制实例:
当需要两台服务器需要目录保持一致就可以用到sync服务(C/S软件),需要一主(master) 一从(slave)
ip:192.168.13.150(master)
ip:192.168.13.151(slave)
一般都会结合crontab来完成复制
13.150操作:
第一步:
[root@13-150 cache]# rpm -qa | grep rsync #检查是否安装服务 rsync-3.0.6-9.el6.x86_64
第二步:
mkdir /etc/rsyncd && cd /etc/rsyncd/ && vim rsyncd.conf #创建目录 进入目录 自己写配置文件
[root@13-150 rsyncd]# ll 总用量 12 -rw-r--r-- 1 root root 326 8月 29 09:44 rsyncd.conf #配置文件 -rw-r--r-- 1 root root 99 8月 29 09:45 rsyncd.motd #欢迎文件 当slave链接是 出现的提示消息 -rw------- 1 root root 13 8月 29 09:43 rsyncd.passwd #密码文件 当slave链接是 需要的账号和密码
vim sryncd.conf
uid=root #决定用户权限 gid=root #决定用户组权限 port=873 #端口 max connections=0 #最大链接数 0 代表无限制 log file=/var/log/rsyncd.log #日志文件 pid file=/var/run/syncd.pid #pid文件 lock file=/var/run/rsyncd.lock #锁定文件 motd file=/etc/rsyncd/rsyncd.moth #欢迎信息文件
read only=yes # 只读 就给只读权限 不想让它做别的操作 拉复制只需要能读就好 hosts allow=192.168.13.0/24 #允许的网段 hosts deny=* #不允许任何网段访问,至于许13.0网段 [www] #共享名称 path=/cache #共享目录 list=yes #是否可见 ignore errors #忽略错误 auth users=hello #授权的账号 secrets file=/etc/rsyncd/rsyncd.passwd #授权账号的密码
[root@13-150 rsyncd]# cat rsyncd.motd ################################ #####sdasfskdsjfkjah############ ################################
[root@13-150 rsyncd]# cat rsyncd.passwd hello:123456
rsync --daemon --config=/etc/rsyncd/rsyncd.conf #启动 查看端口
注意报错多一半是因为配置文件写错
13.151操作:
vim /etc/rsync.pw
[root@13-151 etc]# cat rsync.pw #slave服务器上密码文件 只写密码即可 不用担心安全问题 123456
[root@13-151 etc]# rsync -aczP --delete --password-file=/etc/rsync.pw hello@192.168.13.150::www /cache receiving incremental file list sent 54 bytes received 163 bytes 16.07 bytes/sec total size is 2048000000 speedup is 9437788.02 [root@13-151 etc]#
到此啦复制完成 可以配置crontab来完成定时复制。
此处提供一个小脚本
[root@13-151 ~]# cat 1.sh #!/bin/bash while : do rsync -aczP --delete --password-file=/etc/rsync.pw hello@192.168.13.150::www /cache >> /dev/null
wait #可以保证上一条命令执行完毕 再去执行 #一般针对大文件 小文件就无所谓了 sleep 0.001 done
✨推复制
slave服务器不知道什么时候master什么时候会又数据
所有这时候就应该选择推复制
安装间谍软件 如果发现目录有变化 就触发推复制
缺点:高并发 文件多的时候出现问题 一点一点增加一点一点减少
优点:不怕大文件
slave服务器:
关闭master 873端口
[root@13-150 GNU-Linux-x86]# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1153/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1229/master tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3252/mysqld tcp 0 0 :::22 :::* LISTEN 1153/sshd tcp 0 0 ::1:25 :::* LISTEN 1229/master [root@13-150 GNU-Linux-x86]#
把master配置文件 scp 到slave服务器上
启动程序 # 让slave 监听873端口
[root@13-151 srync]# ll 总用量 12 -rw-r--r-- 1 root root 324 8月 29 12:02 rsyncd.conf -rw-r--r-- 1 root root 99 8月 29 11:28 rsyncd.motd -rw------- 1 root root 13 8月 29 11:28 rsyncd.passwd [root@13-151 srync]# rsync --daemon --config=/etc/srync/rsyncd.conf [root@13-151 srync]# [root@13-151 srync]# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1134/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1210/master tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 71087/rsync tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2907/mysqld tcp 0 0 :::22 :::* LISTEN 1134/sshd tcp 0 0 ::1:25 :::* LISTEN 1210/master tcp 0 0 :::873 :::* LISTEN 71087/rsync
master服务器:上传文件
sersync2.5_32bit_binary_stable_final.tar.gz 解压完叫 GNU-Linux-x86
tar xf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/src/ #指定目录解压
cd GNU-Linux-x86/ 修改文件 confxml.xml 修改一下几个项 (另一个文件是启动文件 sersync2 )
<sersync> <localpath watch="/cache"> #同步目录 <remote ip="192.168.13.151" name="www"/> #slave服务器ip name 共享名称 <!--<remote ip="192.168.8.39" name="tongbu"/>--> #第二台 <!--<remote ip="192.168.8.40" name="tongbu"/>--> # 第三台 </localpath> <rsync> <commonParams params="-artuz"/> #使用命令参数 <auth start="true" users="hello" passwordfile="/etc/rsyncd/rsyncd.passwd"/> #密码文件 <userDefinedPort start="false" port="874"/><!-- port=874 --> #默认端口 如果修改端口 需要把start='false' 修成true <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #日志 <crontab start="false" schedule="600"><!--600mins--> <crontabfilter start="false"> <exclude expression="*.php"></exclude> <exclude expression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync>
执行 出现一下算成功 如果报错 请查看
/tmp/rsync_fail_log.sh 日志
报错请仔细检查配置文件
[root@13-150 GNU-Linux-x86]# ./sersync2 -r 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: -r rsync all the local files to the remote servers before the sersync work daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is hello passwordfile is /etc/rsyncd/rsyncd.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 /cache && rsync -artuz -R --delete ./ hello@192.168.13.151::www --password-file=/etc/rsyncd/rsyncd.passwd >/dev/null 2>&1 run the sersync: watch path is: /cache
包地址:https://pan.baidu.com/s/1cvvqU-bhENws5OuviSGIeQ