rsync配置两台服务器之间的文件备份(同步)

时间:2023-03-08 20:34:23
rsync配置两台服务器之间的文件备份(同步)

rsync配置两台服务器之间的文件备份(同步)

前情提要

环境:

  • 192.168.1.2 主服务器 centos 7.7
  • 192.168.1.3 备份服务器 centos 7.7

rsync 安装(两台linux都需要安装)

在linux环境下,以centos为例,安装非常简单:

[root@qinshengfei ~]# yum install rsync

rsync的配置文件(服务端配置)

rsync 服务端安装完成之后是没有生成rsync.conf文件的,需要手动创建rsyncd.conf

[root@qinshengfei ~]# vim /etc/rsyncd.conf
#先定义整体变量
secrets file = /etc/rsyncd.secrets #配置同步用户名和密码
motd file = /etc/rsyncd.motd
read only = yes
list = yes
#uid = nobody
#gid = nobody
uid = root
gid = root
hosts allow = *    #哪些电脑可以访问rsync服务
hosts deny = 0.0.0.0/32    #哪些电脑不可以访问rsync服务
max connections = 2
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
  
#再定义要rsync的目录
[backup]
path = /data/app/files
list=yes
ignore errors
auth users = root
comment = welcome
exclude = file1/  file2/ 
  • 配置同步的用户名和密码
[root@qinshengfei ~]# vim /etc/rsyncd.secrets
  • 在rsyncd.secrets文件里配置一行即可
root:123456

rsync 备份测试(客户端执行)

 [root@qinshengfei ~]# rsync -arzvtopg --delete feng@192.168.1.2::bakup /opt/app/bakdir --password-file=/etc/rsyncd.secrets

rsync 定时备份(客户端配置)

在linux环境下,可以使用crontab和rsync结合起来做备份机制,找到/etc/crontab文件,在文件里追加这样一行

[root@qinshengfei ~]# vim /etc/crontab
40 19 * * * root rsync -aqzrtopg  --progress root@192.168.1.2::backup /opt/app/bakdir --password-file=/etc/rsyncd.secrets --log-file=/var/log/rsync.log

释义:前面两个参数配置是:每天19:40定时同步

总结