rsync实现内网两台服务器文件同步

背景:从外网 SFTP 服务器同步数据到内网一台 Web 服务器,内网两台 Web 服务器间使用 rsync 同步数据。

要通过 rsync 实现定时从 192.168.1.150 同步文件到 192.168.1.153,可以按照以下步骤操作:

以下操作在153机器上进行

1、安装rsync 

yum install -y rsync

2、配置 SSH 免密登录

ssh-keygen -t rsa

3、将公钥复制到目标机器

ssh-copy-id [email protected]

4、测试免密登录

ssh [email protected]

5、手动测试同步命令

rsync -avz -e "ssh" [email protected]:/source/path/ /destination/path/

6、添加到定时任务

crontab -e
0 2 * * * rsync -avz -e "ssh" [email protected]:/source/path/ /destination/path/ >> /var/log/rsync.log 2>&1

#删除目标端多余文件:如果希望保持目标目录与源目录完全一致,可以添加 --delete 参数:

rsync -avz --delete -e "ssh" [email protected]:/source/path/ /destination/path/

#需要注意/destination/path/ 的路径,直接配置到/destination即可。

7、验证任务是否正常运行

systemctl status cron

你可能感兴趣的:(服务器,网络,运维)