阿里云Centos7.x负载均衡服务器间代码定时同步

  • 环境:Centos7.X
  • 负载均衡的代码主/从rsync定时同步
    1. A服务器(主服务器):
      1. 安装rsync:yum install rsync -y
      2. vi /etc/rsyncd.conf
strict modes = yes
port = 873
uid = root
gid = root
use chroot = no
max connection = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
hosts allow = 172.18..X.X #内网B服务器

[www.XXX.com-rsync] #此段的名称,在B服务器同步时需指定
path = /home/wwwdata/www.XXX.com
ignore errors
read only = no
list = no
hosts allow = 172.18.X.X #内网B服务器
auth users = root
secrets file = /etc/rsyncd.password
  1. 设置同步时需要的账号密码
    1. vi /etc/rsyncd.password
    2. root:123456
    3. chmod 600 /etc/rsyncd.password
  2. Centos7.x防火墙默认不是iptables,是firewalld,因此要通过firewalld将rsync的873端口开放给B服务器
    1. 开放873端口:firewall-cmd --permanent --add-port=873/tcp
    2. 检查是否开启成功:firewall-cmd --permanent --query-port=873/tcp
    3. 重启防火墙:firewall-cmd --reload
  3. 启动:rsync --daemon --config=/etc/rsyncd.conf
  4. 开机启动:echo '/user/bin/rsync --daemon --config=/etc/rsyncd.conf' >> /etc/rc.local
B服务器(从服务器)
  1. 设置同步密码(此处应与A服设置的访问密码一致)
    1. vi /etc/rsyncd.password
    2. 123456
    3. chmod 600 /etc/rsyncd.password
  2. 写同步脚本:vi rsync_xxx
    1. #!/bin/bash
    2. /usr/bin/rsync -avzP --delete --progress --password-file=/etc/rsyncd.password [email protected]::www.XXX.com-rsync home/wwwdata/www.XXX.com
    3. chmod u+x rsync_xxx
  3. 定时同步:
    1. 定时同步:crontab -e
    2. */1 * * * * /home/wwwdata/rsync-from-106-www.XXX.com >> /home/wwwdata/_bk/crontab_www_`date+\%Y\%m\%d`.log 2>&1
    3. 重新加载:service crond reload
    4. 查看执行日志:tail -f /var/log/cron
    5. 查看自定义日志:cat /home/wwwdata/_bk/crontab_www_2016-12-24.log

你可能感兴趣的:(服务器)