• Linux Rsync


    一、Rsync介绍

       1、什么是Rsync

         Rsync 即Remote Rynchronization,是一款开源的、快速的、多功能的、可实现全量或增量的本地或者远程数据镜像同步复制、备份的优秀工具。

    类似于scp命令,但是优于scp,可以做增量的备份。Rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,这类似与cp命令,同样优于cp命令(增量)。

    Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件。

    Rsync 使用所谓的“Rsync 演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

    运行 Rsync server 的机器也叫 backup server,一个 Rsync server 可同时备份多个 client 的数据;也可以多个Rsync server 备份一个 client 的数据。
    Rsync 可以搭配 rsh 或 ssh 甚至使用 daemon 模式。Rsync server 会打开一个873的服务通道(port),等待对方(客户端) Rsync 连接。连接时,Rsync server 会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。

       2、Rsync特性

    支持拷贝特殊文件如链接、设备等
    支持排除特定文件或目录同步的功能,相当于打包命令tar的排除功能。
    支持保持源文件或目录的权限、时间、软硬链接、属主属组等所有属性的不改变。
    支持实现增量同步,既只同步发生变化的数据,因为数据传输效率很高。
    支持使用rcp、rsh、ssh等方式配合传输文件,也可以直接通过socket(进程方式)传输。
    支持匿名的或认证(无需系统用户)的进程模式传输,可实现方便安全的进行数据备份及镜像。

    二、Rsync配置

       1、Rsync安装

         一般的linux系统上都默认安装得了rsync 。如若没有安装请自行安装。

         

       2、Rsync主要配置文件

           rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)

           rsyncd.conf 服务器的主要配置文件:

    transfer logging = true
    log format = %h %o %f %l %b
    log file = /wy/logs/rsyncd.log
    pid file = /var/run/rsyncd.pid
    [data]
            path = /wy/data/doc
            comment = wydoc
            ignore errors = yes
            read only = no
            uid = root
            gid = root
            list = no
            hosts allow = 115.182.93.132  10.200.93.132
            hosts deny = *
            max connections = 10
            auth users = wy
            secrets file = /etc/rsyncd.secrets
            exclude =  dir1/  dir2/

     auth users是必须在服务器上存在的真实的系统用户,如果你想用多个用户以,号隔开,比如auth users = nobody,root

    #在rsync 服务器中,全局定义有几个比较关健的,根据我们前面所给的配置文件 rsyncd.conf 文件;
      pid file = /var/run/rsyncd.pid   注:告诉进程写到 /var/run/rsyncd.pid 文件中;
      port = 873  注:指定运行端口,默认是873,您可以自己指定;
      address = 192.168.1.171  注:指定服务器IP地址
      uid = nobody  
      gid = nobdoy 
      注:服务器端传输文件时,要发哪个用户和用户组来执行,默认是nobody。 如果用nobody 用户和用户组,可能遇到权限问题,有些文件从服务器上拉不下来。所以我就偷懒,为了方便,用了root 。不过您可以在定义要同步的目录时定义的模块中指定用户来解决权限的问题。
      
        use chroot = yes
      注:用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外。也就是说,你在 rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容;这个需要自己来尝试
     
      read only = yes
      注:read only 是只读选择,也就是说,不让客户端上传文件到服务器上。还有一个 write only选项,自己尝试是做什么用的吧;
     
      #limit access to private LANs
      hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
      注:在您可以指定单个IP,也可以指定整个网段,能提高安全性。格式是ip 与ip 之间、ip和网段之间、网段和网段之间要用空格隔开;
     
      max connections = 5  
      注:客户端最多连接数
     
      motd file = /etc/rsyncd/rsyncd.motd
      注:motd file 是定义服务器信息的,要自己写 rsyncd.motd 文件内容。当用户登录时会看到这个信息。
     
      log file = /var/log/rsync.log
      注:rsync 服务器的日志; 
     
      transfer logging = yes
      注:这是传输文件的日志
     
      log format = %t %a %m %f %b
      syslog facility = local3
      timeout = 300
      
    模块定义
      模块定义什么呢?主要是定义服务器哪个目录要被同步。每个模块都要以[name]形式。这个名字就是在rsync 客户端看到的名字,其实有点象Samba服务器提供的共享名。而服务器真正同步的数据是通过path 指定的。我们可以根据自己的需要,来指定多个模块。每个模块要指定认证用户,密码文件、但排除并不是必须的 
      下面是前面配置文件模块的例子:
     
      [home]  #模块它为我们提供了一个链接的名字,在本模块中链接到了/home目录;要用[name] 形式
      path = /usr/local/tomcat/webapp/home/statics    #指定文件目录所在位置,这是必须指定的
      auth users = root   #认证用户是root  ,是必须在服务器上存在的用户
      list=yes   #list 意思是把rsync 服务器上提供同步数据的目录在服务器上模块是否显示列出来。默认是yes 。如果你不想列出来,就no ;如果是no是比较安全的,至少别人不知道你的服务器上提供了哪些目录。你自己知道就行了;
      ignore errors  #忽略IO错误
      secrets file = /etc/rsyncd.secrets   #密码存在哪个文件
      comment = linuxsir home  data  #注释可以自己定义
      exclude = dir1/ dir2/     
      注:exclude是排除的意思,也就是说,要把/usr/local/tomcat/webapp/home/statics目录下的easylife和samba排除在外; dir1/和dir2/目录之间有空格分开

           rsyncd.secrets

    #目录、用户权限创建配置
    [root@Rsync_A ~]# useradd rsync -s /sbin/nologin -M
    [root@Rsync_A ~]# grep rsync /etc/passwd
    rsync:x:502:502::/home/rsync:/sbin/nologin
    #修改属主 [root@Rsync_A
    ~]# chown rsync.rsync /skyex/ [root@Rsync_A ~]# ls -ld /skyex/ drwxr-xr-x 2 rsync rsync 167936 May 30 22:10 /skyex/ #配置密码文件(格式---> 用户:密码) [root@Rsync_A ~]# echo "rsync_backup:skyex" >> /etc/rsync.password [root@Rsync_A ~]# cat /etc/rsync.password rsync_backup:skyex #修改权限更改密码文件权限600 [root@Rsync_A ~]# chmod 600 /etc/rsync.password [root@Rsync_A ~]# ls -ld /etc/rsync.password -rw------- 1 root root 19 May 27 22:14 /etc/rsync.password

       3、启动Rsync服务

    #启动rsync
    [root@Rsync_A ~]# rsync --daemon
    #查看rsync进程
    [root@Rsync_A ~]# ps -ef|grep rsync
    root 2779 1 0 22:41 ? 00:00:00 rsync --daemon
    root 2785 2678 0 22:41 pts/0 00:00:00 grep rsync
    #根据端口查看进程
    [root@Rsync_A ~]# lsof -i tcp:873
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    rsync 2779 root 4u IPv4 8610 0t0 TCP *:rsync (LISTEN)
    [root@Rsync_A ~]# netstat -lntup |grep 873
    tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2779/rsync

        Rsync 客户端配置

    #配置密码文件
    [root@Rsync_B ~]# echo "skyex">>/etc/rsync.secrets
    [root@Rsync_B ~]# cat /etc/rsync.secrets
    skyex
    #更改密码文件权限为600
    [root@Rsync_B ~]# chmod 600 /etc/rsync.secrets
    [root@Rsync_B ~]# ls -ld /etc/rsync.secrets
    -rw------- 1 root root 6 May 27 22:17 /etc/rsync.secrets

    三、Rsync的工作方式

        1、主机本地间的数据传输(类似cp命令的功能)
        2、借助rcp、ssh等通道来传输数据(类似scp命令的功能)
        3、以守护进程(socket)的方式传输数据

       本地数据传输模式(local-only mode)

    1 Local: rsync [OPTION...] SRC... [DEST]

       通过远程shell进行数据传输(remote shell mode)

    1 Access via remote shell:
    2 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
    3 Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

       通过守护进程的方式进行数据传输

    1 Access via rsync daemon:
    2 Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
    3 rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
    4 Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
    5 rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

       Rsync命令同步参数选项
      rsync [OPTION...] SRC ... DEST
      常用参数选项说明:
      -v, --verbose 详细模式输出,传输时的进度等信息
      -z, --compress 传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩
      -r, --recursive 对子目录以递归模式,即目录下的所有目录都同样传输,注意是小写r
      -t, --times 保持文件时间信息
      -o, --owner 保持文件属主信息
      -p, --perms 保持文件权限
      -g, --group 保持文件属组信息
      -P, --progress 显示同步的过程及传输时的进度等信息
      -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgDl 
      -D, --devices 保持设备文件信息
      -l, --links 保留软链接
      -e, --rsh=COMMAND 使用的信道协议, 指定替代rsh的shell程序。例如:ssh 
      --exclude=PATTERN 指定排除不需要传输的文件模式

       rsync -avz /opt/ /mnt #加/,只同步目录内的内容
       rsync -avz /opt /mnt #不加/,会将目录和目录内的内容一起同步

    四、具体应用

       通过Rsync在本地传输数据实践

    #通过rsync命令,推送56_rsync_a.log到10.0.0.57的家目录
    [root@Rsync_A ~]# ll
    total 76
    -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
    -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg
    -rw-r--r-- 1 root root 41751 May 21 02:08 install.log
    -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog
    -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
    [root@Rsync_A ~]# rsync -avzP 56_rsync_a.log root@10.0.0.57:~
    sending incremental file list
    56_rsync_a.log
    0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
    14 sent 75 bytes received 31 bytes 212.00 bytes/sec
    total size is 0 speedup is 0.00
    17 [root@Rsync_B ~]# ll
    total 68
    -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
    -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg
    -rw-r--r-- 1 root root 41751 May 21 02:08 install.log
    -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog
    #通过rsync命令,拉取10.0.0.57主机/tmp目录数据到本地/tmp目录下
    [root@Rsync_B tmp]# ll
    total 4
    -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log
    drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492
    30 [root@Rsync_A ~]# ll /tmp/
    total 8
    drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537
    -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
    [root@Rsync_A ~]# rsync -avz root@10.0.0.57:/tmp /tmp
    receiving incremental file list
    tmp/
    tmp/57.tmp.log
    tmp/.ICE-unix/
    tmp/ssh-XpIRDi2492/
    tmp/ssh-XpIRDi2492/agent.2492
    42 sent 45 bytes received 210 bytes 510.00 bytes/sec
    total size is 0 speedup is 0.00
    [root@Rsync_A ~]# ll /tmp/
    total 12
    drwx------ 2 root root 4096 May 30 20:09 ssh-aiWOPN2537
    -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
    drwxrwxrwt 4 root root 4096 May 30 2013 tmp
    [root@Rsync_A ~]# tree /tmp/
    /tmp/
    |-- ssh-aiWOPN2537
    | `-- agent.2537
    |-- system_init.sh
    `-- tmp
    |-- 57.tmp.log
    `-- ssh-XpIRDi2492
    `-- agent.2492
    59 3 directories, 4 files

    借助rcp、ssh等通道来传输数据实践

    #通过ssh通道推送数据
    [root@Rsync_A ~]# rsync -avzP -e "ssh -p22" 56_rsync_a.log root@10.0.0.57:/tmp
    sending incremental file list
    56_rsync_a.log
    0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
     7 sent 75 bytes received 31 bytes 212.00 bytes/sec
    total size is 0 speedup is 0.00
    10 [root@Rsync_B ~]# ll /tmp/
    total 4
    -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
    -rw-r--r-- 1 root root 0 May 30 21:38 57.tmp.log
    drwx------ 2 root root 4096 May 30 21:04 ssh-XpIRDi2492
    #通过ssh通道拉取数据
    [root@Rsync_A ~]# rsync -avz -e "ssh -p22" root@10.0.0.57:/tmp .
    receiving incremental file list
    tmp/
    tmp/56_rsync_a.log
    tmp/57.tmp.log
    tmp/.ICE-unix/
    tmp/ssh-XpIRDi2492/
    tmp/ssh-XpIRDi2492/agent.2492
    26 sent 64 bytes received 274 bytes 676.00 bytes/sec
    total size is 0 speedup is 0.00
    [root@Rsync_A ~]# ll
    total 80
    -rw-r--r-- 1 root root 0 May 30 20:34 56_rsync_a.log
    -rw------- 1 root root 967 May 21 02:09 anaconda-ks.cfg
    -rw-r--r-- 1 root root 41751 May 21 02:08 install.log
    -rw-r--r-- 1 root root 4688 May 21 02:06 install.log.syslog
    -rw-r--r-- 1 root root 2832 May 13 11:08 system_init.sh
    drwxrwxrwt 4 root root 4096 May 30 2013 tmp
    [root@Rsync_A ~]# tree tmp/
    tmp/
    |-- 56_rsync_a.log
    |-- 57.tmp.log
    `-- ssh-XpIRDi2492
    `-- agent.2492
    43 1 directory, 3 files

    通过ssh key+rsync实现批量免密码加密分发数据

    #批量将任意文件服务分发至/tmp目录下
    #!/bin/sh
    . /etc/init.d/functions
    if [ $# -ne 1 ];then
    echo "Usage:$0 argv"
     exit
    fi
    for ip in `cat iplist`
    do
    rsync -avzP $1 -e "ssh -p 52113" lican888@$ip:~ >&/dev/null
    ssh -p52113 -t lican888@$ip sudo rsync ~/$1 /etc >&/dev/null
    if [ $? -eq 0 ];then
    action "fenfa $1 successful." /bin/true
    else
    action "fenfa $1 failure." /bin/false
     fi
    done

     以守护进程(socket)的方式传输数据

     Rsync推送数据

    #推送/var/www/html到服务器端
    [root@Rsync_B script]# cd /var/www/
    [root@Rsync_B www]# tar zcvf html_$(date +%F).tar.gz ./html/
    ./html/
    ./html/d
    ./html/a
    ./html/c
    ./html/e
    ./html/b
    ./html/f
    [root@Rsync_B www]# ll
    total 8
    drwxr-xr-x 2 root root 4096 May 30 23:44 html
    -rw-r--r-- 1 root root 190 May 30 23:59 html_2013-05-30.tar.gz
    [root@Rsync_B www]#
    #推送数据压缩包至rsync服务器skyex模块内
    [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex
    Password:
    sending incremental file list
    html_2013-05-30.tar.gz
    190 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
    24 sent 277 bytes received 27 bytes 46.77 bytes/sec
    total size is 190 speedup is 0.62
    #免密码输入,推送数据
    [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync_backup@10.0.0.56::skyex --password-file=/etc/rsync.password
    sending incremental file list
    31 sent 43 bytes received 8 bytes 102.00 bytes/sec
    total size is 190 speedup is 3.73
    [root@Rsync_B www]#
    #通过rsync协议进行推送
    [root@Rsync_B www]# rsync -avzP html_2013-05-30.tar.gz rsync://rsync_backup@10.0.0.56/skyex --password-file=/etc/rsync.password 
    sending incremental file list
    39 sent 43 bytes received 8 bytes 102.00 bytes/sec
    total size is 190 speedup is 3.73
    [root@Rsync_B www]#

     客户端拉取

     /usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsyncd.secrets root@192.168.1.100::data /var/www/statics/ 

     五、总结

     Rsync 工作模式

      1.拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。

      2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。

      3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。

      4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。

      5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。

      6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。

     通过与crontab结合使用,让这个脚本定时运行。

     Rsync 服务端部署流程

      1、配置rsync配置文件
      2、创建同步的本地目录/skyex,并根据需要授权rsync服务的用户可读写/ skyex。目录和/etc/rsync.passwore为配置文件中path = / skyex参数的配置
      3、账号及密码文件配置
      4、启动rsync服务
     
    Rsync客户端配置流程
      1、echo “123”>/etc/rsync.password
      2、chmod 600 /etc/rsync.password
      提示:客户端的和服务端的/etc/rsync.password没有任何关系。只要/etc/rsync.password和客户端rsync命令中的参数--password-file=/etc/rsync.password中的路径对应即可。
     
    Rsync服务端的排错思路
      1、查看rsync服务配置文件路径是否正确,默认路径为/etc/rsyncd.conf
      2、查看配置文件里host allow,host deny,允许的ip网段是否允许客户端访问的ip网段。
      3、查看配置文件中path参数里的路径是否存在,权限是否正确(正常应为配置文件中的UID参数对应的属主和组)
      4、查看rsync服务是否启动。查看命令为:ps -ef|grep rsync,端口是否存在netstat -lnt|grep 873
      5、查看iptables防火墙和selinux是否开启允许rsync服务通过,也可以考虑关闭。
      6、查看服务端rsync配置的密码文件是否为600权限,密码文件格式是否正确,正确格式为:用户名:密码,文件路径和配置文件里的secrect file路径一致。
     
    Rsync客户端的排错思路
      1、查看客户端rsync配置的密码文件是否为600的权限,密码文件格式是否正确  注意:仅需要有密码。并且和服务端的密码一致。
      2、用telnet链接rsync服务器IP地址873端口,查看服务是否启动(可测试服务端防火墙是否阻挡)。telnet 10.0.0.141 873
      3、客户端执行命令时rsync -avzrtopgP rsync_backup@10.0.0.51::skyex/test /test --password-file=/etc/rsync.password

     参考:http://www.cnblogs.com/lycn/articles/3259331.html

     由于本人经验有限,文章中难免会有错误,请浏览文章的您指正或有不同的观点共同探讨!

  • 相关阅读:
    个人冲刺二(7)
    个人冲刺二(6)
    个人冲刺二(5)
    个人冲刺二(4)
    对称二叉树 · symmetric binary tree
    108 Convert Sorted Array to Binary Search Tree数组变成高度平衡的二叉树
    530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
    pp 集成工程师 mism师兄问一问
    17. Merge Two Binary Trees 融合二叉树
    270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点
  • 原文地址:https://www.cnblogs.com/exceptioneye/p/4937745.html
Copyright © 2020-2023  润新知