让其他服务器每天通过rsync命令将备份传过来。

环境背景

主机名 ip 系统 服务 备注
backup 192.168.0.29 CentOS7.6 rsync 3.1.2 服务端
wiki 192.168.0.147 CentOS7.6 rsync 3.1.2 客户端
客户端

服务端操作

1.安装rsync

  1. [root@backup ~]# yum install rsync -y

2.编辑rsync配置文件

[root@backup ~]# vim /etc/rsyncd.conf 
uid = nobody
gid = nobody
use chroot = no
fake super = yes
max connections = 2000
timeout = 6000
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 0.0.0.0/0
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.pass
[backup]
comment = backup server
path = /data/backup/

3.设置rsync密码文件

[root@backup ~]# echo "rsync_backup:123456">/etc/rsync.pass
[root@backup ~]# chmod 600 /etc/rsync.pass
[root@backup ~]# cat /etc/rsync.pass
rsync_backup:123456

4.创建数据目录

[root@backup ~]# mkdir -p /data/backup/
[root@backup ~]# chown -R nobody.nobody /data/backup/

5.启动并开机自启动

[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd

[root@backup ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-08-27 17:57:19 CST; 4min 10s ago
 Main PID: 9054 (rsync)
   CGroup: /system.slice/rsyncd.service
           └─9054 /usr/bin/rsync --daemon --no-detach

Aug 27 17:57:19 backup systemd[1]: Started fast remote file copy program daemon.

客户端传输验证

echo "123456" > /etc/rsync.password
chmod 600 /etc/rsync.password
rsync -arzpv /data/backup/ rsync_backup@192.168.0.29::backup --password-file=/etc/rsync.password
#将本机/data/backup目录同步rsync服务器的backup下

通过ansible执行,所有机器的本机备份都推送过来了。异地备份就实现了。

[root@manager scripts]# cat bak_to_rsyncserver.sh 
echo "123456" > /etc/rsync.password
chmod 600 /etc/rsync.password
rsync -arzpv /data/backup/ rsync_backup@192.168.0.29::backup --password-file=/etc/rsync.password
[root@manager scripts]# ansible company -m script -a bak_to_rsyncserver.sh

查看每台机器每天的备份都推送过来了。
image.png