使用rsync定时远程同步

一般场景下,通过rsync可以结合crontab实现按时自动备份,在远程自动同步的场景下,rsync 需要以守护进程的方式来运行,本文记录实现异地自动备份的过程。

生产服务器主机A的地址:192.168.214.190 :centos7.4

备份数据主机B的地址:192.168.214.187 :centos6.9

客户端和服务端都要安装rsync,安装完后有些系统不会生成rsyncd.conf,需要自己创建在 /etc/rsync.d/rsyncd.conf

【在centos7.4上安装rsync会默认生成/etc/rsyncd.conf文件,但是在centos6.9上安装后则不会生成,并且,如果在要自己定义文件位置,以守护进程方式启动,那么任然要在/etc/下新建一个rsyncd.conf的文件,否则无法启动。】

服务端安装rsync:

[root@localhost ~]#yum install rsync -y

创建配置文件

[root@yufu ~]# mkdir -p /etc/rsync.d
[root@yufu ~]# touch /etc/rsync.d/rsyncd.conf
[root@yufu ~]# chmod 600 /etc/rsync.d/rsync.conf

编辑配置文件内容

vim /etc/rsync.d/rsyncd.conf

log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[bak]
path=/opt/server
uid = root
gid = root
ignore = errors
read only = no
write only = no
host allow = *
max connections = 5
host deny = 192.168.22.21
list = false
auth users = feng
secrets file = /etc/rsync.d/server.pass

添加备份用户的用户密码文件,并修改文件权限

echo 'filebak:filebak' > /etc/rsync.d/server.pass
chmod 600 server.pass

服务端的rsync安装配置好后就可启动了,rsync是以守护进程的方式启动:

[root@localhost ~]#rsync --daemon --config=/etc/rsync.d/rsyncd.conf 

[root@localhost ~]#ps -ef | grep rsync
root      2356     1  0 17:29 ?        00:00:00 rsync --daemon --config=/etc/rsync.d/rsyncd.conf
root      2361  1965  0 17:29 pts/1    00:00:00 grep --color=auto rsync

到此服务端的的设置完成,接着安装客户端,在备份主机上不用做任何设置,只要安装rsync服务和设置crontab任务计划就可以, 为了在同步的过程中不用输入密码,因此需要在备份主机上创建一个secrets 文件,该文件内容是服务端的rsyncd.conf中“auth users”指定的用户的密码;这个文件名和路径随意指定,只要在执行同步指令时指定即可。

安装rsync

yum install rsync -y

向客户端添加同步用户的密码

[root@yufu ~]# echo 'fsz...' > /etc/rsync.d/feng.pass

[root@yufu ~]# chmod 600 /etc/rsync.d/feng.pass 

在客户端执行同步

[root@yufu ~]# rsync -arzvtopg --delete feng@192.168.214.190::bak /opt/app/ --password-file=/etc/rsync.d/feng.pass 
rsync: failed to connect to 192.168.214.190: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]

执行报错:原因是服务端的防火墙没有放行策略,关闭防火墙;

[root@localhost ~]#systemctl stop firewalld

再执行:

[root@yufu ~]# rsync -arzvtopg --delete feng@192.168.214.190::bak /opt/app/ --password-file=/etc/rsync.d/feng.pass 
receiving incremental file list
./
install-lnmp.sh

sent 75 bytes  received 1410 bytes  990.00 bytes/sec
total size is 3522  speedup is 2.37
[root@yufu ~]# cd /opt/app/ && tree
.
└── install-lnmp.sh

0 directories, 1 file
[root@yufu app]# ls
install-lnmp.sh

客户端设置定时备份:添加crontab任务计划,每天定时同步文件

[root@yufu ~]# crontab -l
*/1 * * * * /usr/bin/rsync -arzvtopg --delete feng@192.168.214.190::bak /opt/app/ --password-file=/etc/rsync.d/feng.pass
设置定时同步,作为测试,设置每分钟同步一次

服务端测试文件更新脚本

#!/bin/bash
dir=/opt/server
cd $dir
for i in $(seq 1 10)
do
touch file$i
sleep 61
done

测试脚本执行结束后再查看服务端和客户端的目录情况

[root@yufu app]# ls   #客户端
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  install-lnmp.sh  tess


[root@localhost server]#ls  #服务端
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  install-lnmp.sh  tess
标签:Rsync 发布于:2019-10-25 15:58:38