zoukankan      html  css  js  c++  java
  • Rsync

    Rsync

    1. Rsync简介:

    具有可使本地和远程两台主机之间的数据快速复制同步镜像,远程备份的功能,这个功能类似ssh带的scp命令,但又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。当然,Rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,这又类似cp命令,但同样也优于cp命令,cp每次都是全量拷贝,而rsync可以增量拷贝。

    主机名 IP 备注
    master 192.168.200.20 服务端
    node01 192.168.200.21 客户端
    #两台服务器都必须装两个软件包
    [root@master ~]# yum -y install rsync openssh-clients
    [root@master ~]# rpm -qa rsync openssh-clients
    rsync-3.1.2-10.el7.x86_64
    openssh-clients-7.4p1-21.el7.x86_64
    

    rsync 命令常用参数选项说明:

    • -v,--verbose 详细模式输出,传输时的进度等信息
    • -z,--compress 传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩。
    • -a,--archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgD1(字母1)

    2. Rsync--本地用户

    2.1 只加不删

    1.已知

    [root@master ~]# ll backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    
    [root@master ~]# ll ceshi/
    总用量 0
    

    2.把共享目录/backup推到本地的/ceshi目录相下

    [root@master ~]# rsync -avz /root/backup/ /root/ceshi/
    sending incremental file list
    ./
    123.txt
    456.txt
    789.txt
    
    sent 249 bytes  received 76 bytes  650.00 bytes/sec
    total size is 12  speedup is 0.04
    

    3.检测

    [root@master ~]# ll backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
     
    [root@master ~]# ll ceshi/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    

    2.2 又加又删

    rsync的流氓属性:我复制给你你所没有的文件,同时删除你有我没有的文件(保存两台服务器数据的完全同步)

    1.已知

    [root@master ~]# ll backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    
    [root@master ~]# ll ceshi/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 13:00 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 yunwei.txt
    -rw-r--r- 1 root root 4 2月  22 12:50 ywb.txt
    
    

    2.把共享目录/backup推到本地的/ceshi目录相下

    #添加--delete参数
    [root@master ~]# rsync -avz --delete /root/backup/ /root/ceshi/
    sending incremental file list
    deleting ywb.txt
    deleting yunwei.txt
    ./
    456.txt
    789.txt
    
    sent 205 bytes  received 82 bytes  574.00 bytes/sec
    total size is 12  speedup is 0.04
    

    3.检测

    [root@master ~]# ll backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    
    [root@master ~]# ll ceshi/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    

    3. Rsync--不同主机之间的传输

    1.查看服务端信息

    [root@master ~]# ll backup/
    总用量 0
    

    2.查看客户端信息

    [root@node ~]# ll ceshi/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    

    3.将服务端信息备份到本地

    [root@node ~]# rsync -avz /root/ceshi/ root@192.168.200.20:/root/backup/
    root@192.168.200.20's password: 
    sending incremental file list
    ./
    123.txt
    456.txt
    789.txt
    
    sent 249 bytes  received 76 bytes  26.00 bytes/sec
    total size is 12  speedup is 0.04
    
    #备注:可根据自己的需要加不加--delete
    

    4.检测备份信息

    [root@master ~]# ll backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    

    4. Rsync--虚拟用户

    4.1 服务端

    1.编写配置文件

    image_1ev42lf1f10fq1cq88p028j8ek9.png-34.8kB

    [root@master ~]# vim /etc/rsyncd.conf 
    [root@master ~]# tail -16 /etc/rsyncd.conf 
    uid = rsync
    gid = rsync
    use chroot = no
    max connections = 200
    timeout = 300
    pid file = /var/run/rsyncd.pid
    lock file = /var/log/rcyncd.lock
    [backup]
    path = /root/backup/
    ignore errors
    read only = false
    list = fals
    fake super = yes
    hosts allow = 192.168.200.0/24
    hosts deny = 0.0.0.0/24
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    

    2.创建程序用户

    [root@master ~]# useradd -M -s /sbin/nologin rsync
    [root@master ~]# id rsync
    uid=1000(rsync) gid=1000(rsync) 组=1000(rsync)
    

    3.创建它的共享目录

    [root@master ~]# mkdir /backup
    

    4.修改共享目录backup的属主与属组

    [root@master ~]# chown rsync:rsync /backup/
    [root@master ~]# ll -d /backup/
    drwxr-xr-x 2 rsync rsync 6 2月  22 13:20 /backup/
    

    5.创建rsync虚拟账号名与密码

    [root@master ~]# vim /etc/rsync.password 
    [root@master ~]# cat /etc/rsync.password 
    rsync_backup:123456
    

    6.修改rsync.password的权限

    [root@master ~]# chmod 600 /etc/rsync.password 
    [root@master ~]# ll /etc/rsync.password 
    -rw------- 1 root root 20 2月  22 12:32 /etc/rsync.password
    

    7.启动进程

    [root@master ~]# rsync --daemon
    
    pkill rsync
    #可以使用此命令干掉进程
    

    8.查看端口

    [root@master ~]# netstat -anp | grep rsync
    tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1561/rsync          
    tcp6       0      0 :::873                  :::*                    LISTEN      1561/rsync          
    unix  2      [ ]         DGRAM                    22524    1561/rsync      
    

    9.加入开机自启动

    [root@master ~]# echo "rsync --daemon" >> /etc/rc.local 
    [root@master ~]# tail -1 /etc/rc.local 
    rsync --daemon
    

    4.2 客户端

    1.创建密码

    [root@node ~]# vim /etc/rsync.password 
    [root@node ~]# cat /etc/rsync.password 
    123456
    

    2.加权限

    [root@node ~]# chmod 600 /etc/rsync.password 
    [root@node ~]# ll -d /etc/rsync.password 
    -rw------- 1 root root 7 2月  22 12:01 /etc/rsync.password
    

    4.3 测试

    1.查看服务端信息

    [root@master ~]# ll /backup/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 123.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 ywb.txt
    

    2.查看客户端信息

    [root@node ~]# ll ceshi/
    总用量 12
    -rw-r--r-- 1 root root 4 2月  22 12:50 456.txt
    -rw-r--r-- 1 root root 4 2月  22 12:51 789.txt
    -rw-r--r-- 1 root root 4 2月  22 12:50 yunwei.txt
    

    3.由客户端操作

    [root@node ~]# rsync -avz --delete /root/ceshi/ rsync_backup@192.168.200.20::backup --password-file=/etc/rsync.password 
    sending incremental file list
    deleting ywb.txt
    deleting 123.txt
    ./
    456.txt
    yunwei.txt
    
    sent 216 bytes  received 87 bytes  28.86 bytes/sec
    total size is 12  speedup is 0.04
    

    4.服务端检查

    [root@master ~]# ll /backup/
    总用量 12
    -rw------- 1 rsync rsync 4 2月  22 13:44 456.txt
    -rw-r--r-- 1 root  root  4 2月  22 12:51 789.txt
    -rw------- 1 rsync rsync 4 2月  22 13:44 yunwei.txt
    

    5. rsync 常见错误与解决方法整理

    问题1:

    @ERROR: chroot failed 
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    原因:服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。

    问题2:

    @ERROR: auth failed on module tee 
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    原因:服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。

    问题3:

    @ERROR: Unknown module ‘tee_nonexists' 
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    原因:服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

    问题4:在client上遇到问题:
    4.1

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    rsync: could not open password file "/etc/rsync.pas": No such file or directory (2) 
    Password: 
    @ERROR: auth failed on module backup 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--password-file=/etc/rsync.pas

    4.2

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    @ERROR: auth failed on module backup 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,但是服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登陆名不可缺少

    问题5:

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    @ERROR: chdir failed 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题,是因为服务器端的/home/backup 其中backup这个目录并没有设置,所以提示:chdir failed

    问题6:

    rsync: write failed on "/home/backup2010/wensong": No space left on device (28) 
    rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7] 
    rsync: connection unexpectedly closed (2721 bytes received so far) [generator] 
    rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7] 
    

    磁盘空间不够,所以无法操作。
    可以通过df /home/backup2010来查看可用空间和已用空间

    问题7:网络收集问题 
    7.1 权限问题 
    类似如下的提示:

    rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)
    

    注意查看同步的目录权限是否为755

    7.2 time out

    rsync: failed to connect to 203.100.192.66: Connection timed out (110) 
    rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5] 
    

    检查服务器的端口netstat –tunlp,远程telnet测试。
    可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。

    还有一种在同步过程中可能会提示没有权限 (将同步目录加上SvcwRsync全部权限即可,更简单的方法就是将SvcwRsync设为管理员即可)

    7.3 服务未启动

    rsync: failed to connect to 10.10.10.170: Connection refused (111) 
    rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
    

    启动服务:rsync --daemon --config=/etc/rsyncd.conf

    7.4磁盘空间满

    rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
    Skipping any contents from this failed directory 
    

    7.5Ctrl+C或者大量文件

    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5] 
    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
    

    说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器

    问题8.Rsync error: 
    错误一:

    @ERROR: auth failed on module xxxxx 
    rsync: connection unexpectedly closed (90 bytes read so far) 
    rsync error: error in rsync protocol data stream (code 12) at io.c(150)
    

    说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。

    错误二:

    password file must not be other-accessible 
    continuing without password file 
    Password: 
    

    说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd

    错误三:

    @ERROR: chroot failed 
    rsync: connection unexpectedly closed (75 bytes read so far) 
    rsync error: error in rsync protocol data stream (code 12) at io.c(150) 
    

    说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步

    错误四:

    rsync: failed to connect to 218.107.243.2: No route to host (113) 
    rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9] 
    

    说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致

    错误五:

    @ERROR: access denied to www from unknown (192.168.1.123)
    rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
    rsync error: error in rsync protocol data stream (code 12) at io.c(359)
    

    说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致

    错误六:

    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
    rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
    

    说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器

    错误七:

    rsync: read error: Connection reset by peer (104)
    rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
    

    说明:原数据目录里没有数据存在

  • 相关阅读:
    TypeScript--变量
    TypeScript--Hello
    前端跨域的方式
    内存泄漏与垃圾回收机制
    前端拷贝
    React生命周期16版本
    Redux三大原则
    IE6常见CSS解析Bug及hack
    Redux的应用
    vue-router-基础
  • 原文地址:https://www.cnblogs.com/ywb123/p/11128773.html
Copyright © 2011-2022 走看看