一、Rsync 安装
每台服务器均需要安装rsync,但只要备份服务器端开启rsync即可
yum install rsync -y
#备份服务器开启
systemctl start rsynd
systemctl enable rsyncd
二、配置rsync【备份服务器端】
配置文件位置:/etc/rsyncd.conf
vim /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#同步xxxx目录
[test] #同步模块
path = /backups/test1/test-data/ #本地存放路径
ignore errors #忽略掉错误
read only = false #关闭只读模式
list = false
hosts allow =* #允许所有主机
auth users = root #认证用户
secrets file = /etc/rsyncd.secrets #密码存放位置
三、配置秘钥
vim /etc/rsyncd.secrets
root:test1 #用户:密码 建议直接使用服务器root密码
#修改文件权限为600
chmod 600 /etc/rsyncd.secrets
四、配置客户端【传送资料端】
客户端只需配置秘钥即可,也无需开启rsync,直接使用rsync推送即可
vim /etc/rsyncd.secrets
test1 #直接配置密码,与目标端密码一致。
#修改文件权限为600
chmod 600 /etc/rsyncd.secrets
五、同步资料【传送端】执行命令即可
#命令参数详解
-a 使用归档传输
-z 传输过程中使用压缩/解压模式
--password-file 指定秘钥文件,上方已创建
--delete 删除接收服务器多余文件,与传输端文件一致,用于增量备份,并不会删传送端任何文件
--exclude 过滤掉不需要传输的文件,支持正则表达式(过滤掉log结尾的日志文件)
--exclude='atlassian/temp' 过滤掉整个文件夹,此路径为相对路径,不能使用绝对路径,该命令为推送/opt下所有文件, 所以会过滤掉/opt/atlassian/temp的文件夹
rsync -az --password-file=/etc/rsyncd.secrets --delete --exclude='atlassian/confluence/temp' --delete --exclude='*.log' /opt/ root@x.x.x.x::test
六、rsync+sshpass 【备份服务器端去源文件服务器端拉取文件】
sshpass -p #指定资源端服务器密码
rsync -az #使用rsync
-e #指定ssh协议,-p 指定目标端ssh端口
root@X.X.X.X:/data #资料端服务器 用户@IP:文件路径
/data/bak #拉取存放至备份服务器存放路径
sshpass -p 'PASSWD' rsync -az -e 'ssh -p 22' --delete root@X.X.X.X:/data /data/bak
七、关于其他rsync的用法及参数 待写