用于多台或单台机器内备份数据 可以增量,可以全量 本地模式、远程模式、守护进程模式

备份机器backup一般和负载均衡, web服务器, 数据库, 在同一网段, 所以需要异地的再复制一份backup(异地容灾)

本地模式

rsync ~~-a~~ /etc/ /tmp/

远程模式

偶尔用用

  1. scp -rp /etc root@backup:/tmp -P # r:递归
  2. rsync -avz /etc root@backup:/tmp # a:递归并保留原信息,v:显示详情,z:自动打包解包,--delete完全同步

守护进程模式(常用)

cs架构, 好处是不用每次都输入密码

服务端配置

  1. 复制配置文件
    1. cat >/etc/rsyncd.conf<<EOF
    2. ##rsyncd.conf start##
    3. ##rsyncd 20221111
    4. fake super = yes
    5. uid =rsync
    6. gid =rsync
    7. use chroot = no
    8. max connections = 200
    9. timeout = 300
    10. pid file = /var/run/rsyncd.pid
    11. lock file = /var/run/rsync.lock
    12. log file = /var/log/rsyncd.log
    13. ignore errors
    14. read only = false
    15. list = false
    16. #hosts allow = 172.16.1.0/24
    17. ##hosts deny = 0.0.0.0/32
    18. auth users = rsync_backup
    19. secrets file = /etc/rsync.password
    20. [data]
    21. comment = "backup dir by Rdymy"
    22. path = /data
    23. EOF
    1. ##rsyncd.conf start##
    2. ##rsyncd 20221111
    3. fake super = yes # 不开启c7报错
    4. uid =rsync # 服务端运行的虚拟用户!!!
    5. gid =rsync # 同上!!!
    6. use chroot = no
    7. max connections = 200 # 最大连接数
    8. timeout = 300 # 连接超时时间
    9. pid file = /var/run/rsyncd.pid # 存放服务的pid号
    10. lock file = /var/run/rsync.lock # 服务的锁文件,防止重复运行
    11. log file = /var/log/rsyncd.log # 服务端日志目录,不配置默认到/var/log/message!!!
    12. ignore errors # 忽略错误
    13. read only = false #可以进行读写
    14. list = false # 关闭rsync在客户端显示模块列表的功能
    15. #hosts allow = 172.16.1.0/24 # 只准许哪些ip或网段访问!!!
    16. ##hosts deny = 0.0.0.0/32 # 拒绝哪些网段访问!!!
    17. auth users = rsync_backup # 服务端验证用户名!!!
    18. secrets file = /etc/rsync.password # 服务端验证密码文件!!!
    19. [data] # 模块名!!!
    20. comment = "backup dir by Rdymy" # 注释
    21. path = /data # 模块对应的目录!!!
    ```bash useradd -s /sbin/nologin -M rsync echo ‘rsync_backup:1’ >/etc/rsync.password chmod 600 /etc/rsync.password

创建同步目录

mkdir xxx chown rsync.rsync xxx

```bash
systemctl restart rsyncd
systemctl enable rsyncd
ps -ef | grep rsync  # 检查服务是否启动
ss -lntup | grep rsync  # 检查端口是否开启

客户端配置并测试

echo '1' >/etc/rsync.pass
chmod 600 /etc/rsync.pass
rsync -avz /etc/hosts rsync_backup@backup::data --password-file=/etc/rsync.pass
#less /var/log/rsyncd.log  # 有问题可以看看日志

原理总结(了解)

rsyncd - 图1

排障

rsync: failed to connect to exam_backup (47.110.82.86): Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(127) [sender=3.1.3]

# 解决方法
放行阿里云安全组873端口(ss -lntup | grep rsync)
包括防火墙,selinux什么的没关也记得关了


@ERROR: auth failed on module data
rsync error: error starting client-server protocol (code 5) at main.c(1648或1649) [sender=3.1.2]
# 解决方法
密码文件的属主属组可能给了匿名用户,要给当前用户比如root
(耗了我好几个小时,注意看/var/log/rsync.log,解决不了的问题可以第二天再处理,时间宝贵)
其他可能:密码文件不存在,密码写错,未指定客户端用户

没写的错误看这里