CentOS7通过rsync+crontab实现两台服务器文件同步

时间:2021-06-19 21:48:44

centos7系统中已经默认安装rsync

1:主服务器配置

(1)修改rsyncd.conf 配置文件

[root@localhost app]# vi /etc/rsyncd.conf
motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
port = 873
address = 192.168.0.24
uid = wzh
gid = wzh
use chroot = no
read only = no
max connections = 10
timeout = 900
ignore nonreadable = yes

[common]
comment = rsync data
path = /data/imgs/
ignore errors
auth users = wzh
secrets file = /etc/rsyncd.secrets
hosts allow = 192.168.0.0/255.255.255.0
hosts deny = *
list = false

(2)创建用户密码

echo "wzh:wzh" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

(3)创建提示信息文件

echo "rsync data" > /etc/rsyncd.motd

(4)配置防火墙

firewall-cmd --zone=public --add-port=873/tcp --permanent
firewall-cmd --reload

(5)启动服务

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

2:备服务器配置

(1)创建密码文件

echo "wzh" > /home/wzh/passwd
chmod 600 /home/wzh/passwd

(2)拉取

[wzh@localhost ~]$ rsync -avz --password-file=/home/wzh/passwd wzh@192.168.0.24::common /data/imgs/
rsync data

receiving incremental file list
./

sent 58 bytes received 121 bytes 358.00 bytes/sec
total size is 0 speedup is 0.00

(3)将命令加入到定时任务中,每1分钟执行同步一次

$ vi /home/wzh/rsync_data_imgs.sh

#!/bin/bash
rsync -avz --password-file=/home/wzh/passwd wzh@192.168.0.24::common /data/imgs/ > /dev/null 2>&1 &

$ crontab -e
*/1 * * * * /home/wzh/rsync_data_imgs.sh

$ crontab -l