服务器主从同步

摘要:采用rsync实现

安装说明

采用两台服务器,主服务器A,从服务器B 将数据从A同步到从属服务器B中
本次所有服务均在用户家目录  cd ~ 可直接进入

主服务器A配置

安装rsync
yum -y install rsync
增加软链
ln -s /etc/rsync.conf
编写对应配置
cp /etc/rsyncd.conf rsyncd.conf.back  (备份一个,防止安装错误造成不可恢复的问题)
vim rsyncd.conf
配置文件如下
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = yes
read only = no
list = no
max connections = 4
pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
timeout = 900
motd file = /etc/rsyncd/rsyncd.motd
log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# 此处backup为对应模块名,从属服务器需要使用
[backup]
#        path = /home/ftp
#        comment = ftp export area
    comment = this is module for backup sam
    path = /www/wwroot/test.samaterials.com
    ignore errors
    auth users = root
    secrets file = /etc/rsyncd.pass
配置密码
echo "root:123" > /etc/rsyncd.pass

root为自定义用户名,123 自定义密码
修改权限,从其服务重载配置
chomd 600 /etc/rsyncd.pass
systemctl restart resyncd
查看进程端口,开放端口
netstat -tunlp
看到873 端口被占用,需要开放

从服务器B配置

安装对应服务
yum -y install rsync
设置密码
echo "123" > /etc/rsyncd/rsyncd.pass

-

服务器A B 公钥校验配置

1、进入A服务器
ssh-keygen -t dsa -b 1024
后面会让输入对应的密码名称,可以自定义,生成后会生成一个秘钥文件以及一个公钥文件,公钥后缀.pub
2、进入B服务器
将A服务器中获取的公钥文件.pub 复制到B服务器
赋值公钥信息到key中
cat /root/.ssh/id_dsa.pub >> authorized_keys

-

shell 脚本执行

#!bin/bash
rsync -auv --password-file=/etc/rsyncd.pass root@64.202.187.175::backup /www/wwwroot/www.samaterials.com
执行测试
bash rsyncd.sh
可以查看对应的信息,有错误会直接报出可以对应调整
配置定时任务
crontab -l   查看当前任务
crontab -e   新增任务
30 21 * * * bash /etc/rsyncd.sh  
每天的21点30 执行一次备份
评论