rsync介绍
rsync可以实现远程数据快速复制,备份,这个服务类似于scp命令,但它的功能远远超出scp的范畴
rsync可以实现增量备份,全量备份功能.这取决于她的 quick check (快速检查)算法
服务端部署rsync (守护进程方式)
-
检查是否安装
rpm -qa rsync
[root@rsync ~]# rpm -qa rsync rsync-3.1.2-6.el7_6.1.x86_64
如果没有安装可以使用 yum install rsync -y
-
编写配置文件
vim /etc/rsyncd.conf 解释: /# ---------------------------------# uid = rsync # rsync用户 gid = rsync # rsync组 use chroot = no # 安全配置 max connections = 200 # 最大连接数 timeout = 300 # 连接超时时间 (单位: s) pid file = /var/run/rsyncd.pid # pid文件 lock file = /var/run/rsync.lock # 锁文件 log file = /var/log/rsyncd.log # 日志文件 #hosts deny = 0.0.0.0/32 # 拒绝的IP段 auth users = chenleilei # 允许连接rsync服务的账号(非系统账号) secrets file = /etc/rsync.password # 密码验证文件 # [backup] # 模块名 可以随意修改 comment = "backup dir by oldboy" # 连接的 欢迎语 没什么用 path = /backup # 备份的目录 ignore errors # 忽略个别传输失败的文件,不影响传输任务 hosts allow = 192.168.0.0/24 # 允许的IP段 list = false # true:禁止写入 flase=允许写入 read only = false # true:禁止写入 flase=允许写入 fake super = yes # !!!重要!!! 在cento7 中需要加上此行,centos6可以忽略 /# ---------------------------------# 配置: uid = rsync gid = rsync 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 #hosts deny = 0.0.0.0/32 auth users = chenleilei secrets file = /etc/rsync.password [backup] comment = "backup dir by oldboy" path = /backup ignore errors hosts allow = 192.168.0.0/24 list = false read only = false fake super = yes
-
创建备份目录
mkdir -p /backup chown -R rsync.rsync /backup
-
添加传输的认证用户密码文件 [ /etc/rsync.password ]
echo "chenleilei:123" >/etc/rsync.password chmod 600 /etc/rsync.password
-
权限修改
[root@rsync ~]chmod 600 /etc/rsync.password [root@rsync ~]# ls -la /etc/rsync.password -rw------- 1 root root 15 Aug 14 04:13 /etc/rsync.password [root@rsync ~]# cat /etc/rsync.password chenleilei:123
-
创建运行用户
useradd rsync -s /sbin/nologin -M [root@rsync ~]# id rsync uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
-
启动rsync
[root@rsync ~]# rsync --daemon 检查端口: [root@rsync ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 16526/rsync tcp6 0 0 :::873 :::* LISTEN 16526/rsync
客户端部署
1. 确认软件是否安装
[root@mm01 ~]# rpm -qa rsync
rsync-3.1.2-6.el7_6.1.x86_64
2. 已安装配置
[root@mm01 ~]# echo "123" >client.pass
[root@mm01 ~]# chmod 600 client.pass
2.1 未安装配置
[root@mm01 ~]# yum install -y rsync
[root@mm01 ~]# echo "123" >client.pass
[root@mm01 ~]# chmod 600 client.pass
测试传送
非密钥传输:
rsync -avz /etc/rsyncd.conf chenleilei@192.168.0.164::backup
密钥传输:
rsync -avz /etc/hosts chenleilei@192.168.0.164::backup --password-file=/root/client.pass
注意: 密钥所有者权限可以是 root.root 但文件权限必须是 600,这也是客户端部署时为什么要
chmod 600 client.pass 的原因!
rsync重要参数
--delete 无差异同步
将本地数据无差异同步到远程客户端,如果远程客户端同步的文件夹有文件也会一并被清除
此命令需要谨慎使用.一旦出错,无法恢复
语法:
rsync -avz --delete /etc/rsyncd.conf chenleilei@192.168.0.164::backup
--exclude=file 排除指定文件
在推送时,排除指定文件的推送
[root@mm01 ceshi]# rsync -avz chenleilei@192.168.0.164::backup/888/ /var/log/ --exclude=2.txt --password-file=/root/client.pass
receiving incremental file list
./
1.txt
3.txt
2.txt被排除
rsync错误集锦
错误1:
@ERROR: Unknown module 'backup' rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
网络原因无法传输,可能是当前主机IP不在允许的范围
解决: 检查服务器端 /etc/rsyncd.conf 中的 "hosts allow = " 配置
错误2:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
配置参数问题: fake super = yes
在centos6 中 这行配置不加 也可以成功,但是在centos 7中如果没有此行配置,则会报以上错误
解决: 在配置文件中添加 fake super = yes
错误3:
failed to create pid file /var/run/rsyncd.pid: File exists
服务已启动,再次启动会报这个错
可以通过 netstat -lntup 来查看服务端口 873 是否存在
错误4:
rsync: failed to connect to 192.168.0.164 (192.168.0.164): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
这可能是服务没有启动导致的原因,需要检查远程rsync服务是否存在.
错误5:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
这个报错,除了上面说的 fake super =yes 没有配置 还有可能是 权限不对
需要检查备份文件夹的权限是不是 rsync 所有者和所有主
[root@rsync /]# ll /backup
total 4
drwxr-xr-x 10 root root 4096 Aug 14 17:27 log #如果是root 可能就会出现问题,请改为rsync权限
解决办法:
chown -R rsync.rsync /backup
错误6
rsync 防火墙问题:
错误7
错误8
错误9:
错误10:
rsync的使用
rsync 客户端拉取 服务器的 backup目录中文件到本地目录
rsync -avz chenleilei@192.168.0.164::backup /var/log/ --password-file=/root/client.pass
rsync 客户端拉取 服务器的 backup目录到本地 /var/log 目录
rsync -avz chenleilei@192.168.0.164::backup /var/log/ --password-file=/root/client.pass
rsync 客户端拉取 服务器的 backup目录中的文件 不含 backup目录到本地 /var/log 目录
rsync -avz chenleilei@192.168.0.164::backup /var/log/ --password-file=/root/client.pass


发表评论