本文共 4627 字,大约阅读时间需要 15 分钟。
安装前的准备(各同步服务器都配置)
# 因为这会降低服务器异常报警的级别,所以记得添加报警机制# SELINUX=disabled(完全不验证)vim /etc/sysconfig/selinuxSELINUX=permissiveESC:wqgetenforcesetenforce 0getenforce
安装rsync(各同步服务器都装)
yum install rsync -y
配置rsyncd.conf(各服务器都配置)
# uid、gid决定被同步过来的文件所属用户所属组# []中是一个自己指定的同步计划的名字(字母数字下划线)# path是被同步文件的存放位置# hosts allow这里注意,每一台服务器都只写其它服务器IP# hosts allow、hosts deny格式 # *(所有IP) # 192.168.0.2/24(IP段) # 192.168.0.2 192.168.0.3(指定多个IP)vim /etc/rsyncd.confuid = wwwgid = wwwuse chroot = yesmax connections = 0log file = /tmp/rsyncd.logmotd file = /tmp/rsyncd.motdpid file = /var/run/rsyncd.pidlock file = /var/run/rsyncd.locksecrets file = /etc/rsyncd.pswd[tongbu1] path = /www/file1 comment = xoyo video files ignore errors = yes read only = no hosts allow = 192.168.0.x 192.168.0.x hosts deny = *[tongbu2] path = /www/file2 comment = xoyo video files ignore errors = yes read only = no hosts allow = 192.168.0.x 192.168.0.x hosts deny = *ESC:wq
配置密码文件
vim /etc/rsyncd.pswdroot:**************ESC:wqchmod 600 /etc/rsyncd.pswdvim /etc/sersyncd.pswd**************ESC:wq
创建同步目录(各服务器都创建)
useradd wwwmkdir /wwwmkdir /www/file1mkdir /www/file2chown -R www:www /www/file1chown -R www:www /www/file2
启动rsync(各服务器都启动)
rsync --daemon
查看占用的端口号
netstat –apn|grep rsync
开启端口
yum install firewalld -ysystemctl enable firewalldsystemctl start firewalldfirewall-cmd --zone=public --permanent --add-port=873/tcpfirewall-cmd --reload
创建单元文件(如果是用yum装的的rsync,则跳过该步骤)
# 进入单元文件目录,创建rsync单元文件cd /etc/systemd/systemvim rsyncd.service[Unit]Description=Start rsync on boot.After=default.target network.target[Service]User=rootGroup=rootType=forkingPIDFile=/var/run/rsyncd.pidExecStart=/usr/bin/rsync --daemonExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=falseRestart=always[Install]WantedBy=multi-user.targetESC:wq# 修改文件权限为只有root用户可以编辑该文件chown -R root:root /etc/systemd/system/rsyncd.servicechmod -R 644 /etc/systemd/system/rsyncd.servicesystemctl daemon-reload
启动并设置开机启动
systemctl enable rsyncdsystemctl start rsyncd
安装sersync(各服务器都装)
# 源码https://code.google.com/archive/p/sersync/# 百度云https://pan.baidu.com/s/1qrwTM40aJ8ckmT84PpiNjg# 64位最新https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gz# 32位最新https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5_32bit_binary_stable_final.tar.gz# 下载安装wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gztar -zxvf sertongbu2.5.4_64bit_binary_stable_final.tar.gzmv GNU-Linux-x86/ /usr/local/sersync
配置sersync(各服务器都执行)
cd /usr/local/sersynccp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu1.xmlcp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu2.xmlvim /usr/local/sersync/tongbu1.xmlESC:wqvim /usr/local/sersync/tongbu2.xml ESC:wq
启动sersync(各服务器都执行)
/usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu1.xml/usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu2.xml
测试
# 在任意一台服务器执行su wwwcd /www/file1echo test > test.txtls# 然后在其他每台服务器执行cd /www/file1ls# 如果结果中包含test.txt# 并且文件内容为'test'# 即表示安装成功
配置开机启动rsync、sersync
# 开机启动rsyncsystemctl enable rsyncdsystemctl start rsyncd# 开机启动sersyncvim /etc/rc.local/usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu1.xml/usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu2.xmlESC:wqchmod +x /etc/rc.d/rc.local
关于日志中部分提示的说明
# 文件同步时会有如下提示# connect from UNKNOWN (192.168.0.3)# rsync to tongbu1/ from unknown (192.168.0.3)# connect from UNKNOWN (192.168.0.4)# rsync to tongbu1/ from unknown (192.168.0.4)# 虽然提示连接到未知ip,实际上只是因为没有加# 虚拟域名映射,完全不会影响rsync功能的使用。# 如果一定要处理的话,可以修改/etc/hosts文件# 这样unknown就会变成你设置的虚拟域名了vim /etc/hosts192.168.0.2 file2192.168.0.3 file3192.168.0.4 file4ESC:wq
转载地址:http://xstyx.baihongyu.com/