主机

192.168.121.121


备机

192.168.121.122


系统:centos7.4


1、主备安装rsync

yum install rsync -y

yum install -y xinetd


2、主机创建账户配置权限

useradd rsync -s /sbin/nologin -M

mkdir /backup

chown rsync.rsync /backup


3、主机修改配置

vi /etc/rsyncd.conf

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log


[backup]

path = /backup

read only = false

list = false

hosts allow = 192.168.121.122/32

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password


echo "rsync_backup:123456">/etc/rsync.password


chmod 600 /etc/rsync.password


4、主备启动服务

rsync --daemon


如停止服务

systemctl stop rsyncd.service


如加入开机自启

echo "/usr/bin/rsync --daemon" >> /etc/rc.local


查看服务及端口

ps -ef |grep rsync | grep -v grep 

root      2065     1  0 11:15 ?        00:00:00 rsync --daemon


ss -tpln | grep rsync

LISTEN     0      5            *:873                      *:*                   users:(("rsync",pid=2065,fd=4))

LISTEN     0      5           :::873                     :::*                   users:(("rsync",pid=2065,fd=5))


5、传输文件

5.1、在备机上从主机拉取文件

主机

touch /backup/test1.txt


备机

mkdir /backup


rsync -auv [email protected]::backup /backup


输入密码123456后发现本机/backup目录下出现test1.txt文件


主机

touch /backup/test2.txt

echo 1 > /backup/test1.txt


备机再次执行文件同步

rsync -auv [email protected]::backup /backup


本机/backup目录下出现test1.txt、test2.txt两个文件


cat /backup/test1.txt 

1


5.2、从主机上推送文件到备机


主机 

echo 2 > /backup/test2


rsync -avz /backup/*  /backup

输入备机的root账户登录密码即可


如主机无密码同步到备机

主机

ssh-keygen -t rsa


ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

输入备机的root账户登录密码即可


touch /backup/test3.txt


再次执行同步发现不需要密码

rsync -avz /backup/* [email protected]:/backup