zoukankan      html  css  js  c++  java
  • linux下的数据备份工具rsync讲解

    linux下的数据备份工具 rsync(remote sync 远程同步)

     

    名词解释:

    sync(Synchronize,即“同步”)为UNIX操作系统的标准系统调用,功能为将内核文件系统缓冲区的所有数据(也即预定将通过低级I/O系统调用写入存储介质的数据)写入存储介质(如硬盘)。

    sync是一个linux同步命令,含义为迫使缓冲块数据立即写盘并更新超级块。在linux系统中,为了加快数据的读取速度,默认情况下,某些数据将不会直接写入硬盘,而是先暂存内存中,如果一个数据被重复写,这样速度一定快,但存在一个问题,万一重新启动,或者是关机,或者是不正常断电的情况下,由于数据还没来得及存入硬盘,会造成数据更新不正常,这时需要命令sync进行数据的写入,即#sync,在内存中尚未更新的的数据会写入硬盘中。所以在关机或者开机之前最好多执行这个几次,以确保数据写入硬盘。

    Rsync不仅可以远程同步数据(类似于scp),当然还可以本地同步数据(类似于cp),但不同于cp或scp的一点是,rsync不像cp/scp一样会覆盖以前的数据(如果数据已经存在),它会先判断已经存在的数据和新数据有什么不同,只有不同时才会把不同的部分覆盖掉。

    scp 用来远程拷贝数据,通过ssh协议通信。它的语法很简单,类似于cp, 唯一不同的是,源地址或者目标地址需要使用远程主机的ip或者hostname. 例如要把本地的数据拷贝到远程一台主机(192.168.0.111)的/data/目录下,可以这样实现: scp /dir/filename root@192.168.0.111:/data/ 其中filename 可以是目录也可以是文件。或者也可以把远程的文件拷贝到本地: scp root@192.168.0.111:/data/filename /data/

    示例,scp拷贝root目录下面的文件到远程主机192.168.20.10的data目录下;

    1
    2
    3
    4
    5
    6
    [root@yong ~]# scp /root/iptables.sh root@192.168.20.10:/data
    The authenticity of host '192.168.20.10 (192.168.20.10)' can't be established.
    RSA key fingerprint is 84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.20.10' (RSA) to the list of known hosts.root@192.168.20.10's password: 
    iptables.sh                                             100%  254     0.3KB/s   00:00

    首次连接会提示是否要继续连接,我们输入yes继续,当建立连接后,需要输入远程主机root密码。

    示例,scp拷贝目录需要加-r参数,拷贝的同时可以更改目录名;

    1
    2
    3
    4
    5
    6
    7
    8
    [root@yong rsync]# scp -r /root/rsync/test1/ 192.168.20.10:/data/a/
    root@192.168.20.10's password: 
    1                                                       100%    0     0.0KB/s   00:00    
    1.txt                                                   100%    0     0.0KB/s   00:00    
    2                                                       100%    0     0.0KB/s   00:00    
    2.txt                                                   100%    0     0.0KB/s   00:00    
    aa                                                      100%    0     0.0KB/s   00:00    
    3                                                       100%    0     0.0KB/s   00:00

     

    安装rysnc的命令:yum install -y rsync

    示例一,拷贝当前目录下test.txt 到/tmp目录下,拷贝的同时也可以更改文件名;

    1
    2
    3
    4
    5
    6
    7
    [root@yong ~]# rsync -av test.txt /tmp/a.txt
    sending incremental file list
    test.txt
    sent 279 bytes  received 31 bytes  620.00 bytes/sec
    total size is 203  speedup is 0.65
    [root@yong ~]# ls /tmp/a.txt 
    /tmp/a.txt

    示例二,拷贝当前目录test.txt 到远程主机192.168.20.10 /data目录下,需要输入远程主机的密码;

    1
    2
    3
    4
    5
    6
    [root@yong ~]# rsync -av test.txt 192.168.20.10:/data/
    root@192.168.20.10's password: 
    sending incremental file list
    test.txt
    sent 279 bytes  received 31 bytes  88.57 bytes/sec
    total size is 203  speedup is 0.65

    示例三,从远程主机192.168.20.10 /data目录下拷贝httpd文件到本地当前目录下;

    1
    2
    3
    4
    5
    6
    [root@yong ~]# rsync -av 192.168.20.10:/data/httpd ./
    root@192.168.20.10's password: 
    receiving incremental file list
    httpd
    sent 30 bytes  received 7538228 bytes  886853.88 bytes/sec
    total size is 7537230  speedup is 1.00

     

    1. rsync 命令格式

    rsync [OPTION]... SRC  DEST

    rsync [OPTION]... SRC  [USER@]HOST:DEST

    rsync [OPTION]... [USER@]HOST:SRC  DEST

    rsync [OPTION]... [USER@]HOST::SRC  DEST

    rsync [OPTION]... SRC  [USER@]HOST::DEST

    上面第一个例子即为第一种格式,第二个例子即为第二种格式,但不同的是,user@host 如果不加默认指的是root用户;第三个例子即为第三种格式是从远程目录同步数据到本地。第四种以及第五种格式使用了两个冒号,这种方式和前面的方式的不同在于验证方式不同。

    2. rsync常用选项

    -a    归档模式,表示以递归方式传输文件,并保持所有属性,等同于-rlptgoD, -a选项后面可以跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个,例如-a--no-l 等同于-rptgoD

    -r    对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项

    -v    打印一些信息出来,比如速率,文件数量等

    -l     保留软链结

    -L     向对待常规文件一样处理软链接,如果是SRC中有软链接文件,则加上该选项后将会把软链接指向的目标文件拷贝到DST

    -p     保持文件权限

    -o     保持文件属主信息

    -g     保持文件属组信息

    -D     保持设备文件信息

    -t     保持文件时间信息

    --delete    删除那些DST中SRC没有的文件

    --exclude=PATTERN    指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)

    --progress    在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等

    -u    加上这个选项后将会把DEST目标文件中比SRC源文件还新的文件排除掉,不会覆盖

    最常用的选项有 -a -v --delete --exclude

     

    示例:创建实验环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@yong ~]# mkdir rsync
    [root@yong ~]# cd rsync/
    [root@yong rsync]# mkdir test1
    [root@yong rsync]# cd test1
    [root@yong test1]# touch 1 2 3
    [root@yong test1]# ln -s /root/123.txt ./123.txt
    [root@yong test1]# ls -l
    -rw-r--r-- 1 root root  0 Apr 22 16:02 1
    lrwxrwxrwx 1 root root 13 Apr 22 16:03 123.txt -> /root/123.txt
    -rw-r--r-- 1 root root  0 Apr 22 16:02 2
    -rw-r--r-- 1 root root  0 Apr 22 16:02 3
    [root@yong test1]# cd ..

    实验目的,拷贝test1目录到test2目录,实际上rsync新建立了一个test2目录并把test1目录放在test2目录下面,这不是我们想要的结果;

    1
    2
    3
    4
    5
    6
    7
    [root@yong rsync]# rsync -a test1 test2
    [root@yong rsync]# ls test2
    test1
    [root@yong rsync]# ls
    test1  test2
    [root@yong rsync]# ls test2/test1/
    1  123.txt  2  3

    为了避免上面的操作,在同步目录的时候在目录后面加 /

    1
    2
    3
    4
    5
    [root@yong rsync]# rsync -a test1/ test2/
    [root@yong rsync]# ls
    test1  test2
    [root@yong rsync]# ls test2/
    1  123.txt  2  3

    -a选项还可以与--no-OPTION一起使用,意思为不理会选项的文件;如下例,跳过123.txt软链接文件,不做拷贝;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    [root@yong rsync]# rsync -av --no-l test1/ test2/
    sending incremental file list
    created directory test2
    ./
    1
    skipping non-regular file "123.txt"
    2
    3
    sent 200 bytes  received 72 bytes  544.00 bytes/sec
    total size is 13  speedup is 0.05
    [root@yong rsync]# ls
    test1  test2
    [root@yong rsync]# ls -l test2/
    total 0
    -rw-r--r-- 1 root root 0 Apr 22 16:02 1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 2
    -rw-r--r-- 1 root root 0 Apr 22 16:02 3

    加-L选项,拷贝的时候会拷贝软链接文件对应的源文件到目标目录里面;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@yong rsync]# rsync -avL test1/ test2/
    sending incremental file list
    created directory test2
    ./
    1
    123.txt
    2
    3
    sent 231 bytes  received 91 bytes  644.00 bytes/sec
    total size is 0  speedup is 0.00
    1
    2
    3
    4
    5
    6
    [root@yong rsync]# ls -l test2/
    total 0
    -rw-r--r-- 1 root root 0 Apr 22 16:02 1
    -rw-r--r-- 1 root root 0 Apr 22 16:03 123.txt
    -rw-r--r-- 1 root root 0 Apr 22 16:02 2
    -rw-r--r-- 1 root root 0 Apr 22 16:02 3

    test1和test2目录下的1文件,创建的时间是一样的;touch test2/1之后创建时间晚了一些。同步的时候不加-u选项,发现同步后的时间还是test1/1的创建时间;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@yong rsync]# ll test1/1 test2/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1
    [root@yong rsync]# touch test2/1
    [root@yong rsync]# ll test1/1 test2/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1
    -rw-r--r-- 1 root root 0 Apr 22 16:31 test2/1
    [root@yong rsync]# rsync -av test1/1 test2/1
    sending incremental file list
    1
    sent 65 bytes  received 31 bytes  192.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ll test1/1 test2/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1

    加-u选项,如果目标文件比源文件新,那么会忽略掉该文件,不做同步;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@yong rsync]# touch test2/1
    [root@yong rsync]# ll test2/1
    -rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1
    [root@yong rsync]# rsync -avu test1/1 test2/1
    sending incremental file list
    sent 26 bytes  received 12 bytes  76.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ll test2/1 test1/1
    -rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1
    -rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1

    删除test1/123.txt,同步之后test2目录下的123.txt不会删除;

    加--delete选项,同步之后test2目录下的123.txt文件也会删除;具体作用是删除目标目录比源目录多出来的文件;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@yong rsync]# rm -f test1/123.txt 
    [root@yong rsync]# ls test1/
    1  2  3
    [root@yong rsync]# rsync -av test1/ test2/
    sending incremental file list
    ./
    1
    sent 94 bytes  received 34 bytes  256.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ls test2/
    1  123.txt  2  3
    1
    2
    3
    4
    5
    6
    7
    [root@yong rsync]# rsync -av --delete test1/ test2/
    sending incremental file list
    deleting 123.txt
    sent 52 bytes  received 12 bytes  128.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ls test2/
    1  2  3

    --exclude选项的作用是,同步的过程中排除文件;选项后面的文件不会同步拷贝到目标目录下;

    1
    2
    3
    4
    5
    6
    [root@yong rsync]# touch test1/aa
    [root@yong rsync]# rsync -a --exclude="aa" test1/ test2/
    [root@yong rsync]# ls test1/
    1  2  3  aa
    [root@yong rsync]# ls test2/
    1  2  3

    --progress选项的作用是显示同步过程的详细信息;--exclude选项后面也可以使用通配符 * 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@yong rsync]# touch test1/1.txt test1/2.txt
    [root@yong rsync]# rsync -a --progress --exclude="*.txt" test1/ test2/
    sending incremental file list
    ./
    aa
               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/5)
    sent 109 bytes  received 34 bytes  286.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ls test2/
    1  2  3  aa
    [root@yong rsync]# ls test1/
    1  1.txt  2  2.txt  3  aa

    3. rsync选项总结

    rsync -av   dir1/ dir2/    #其中dir2/目录可以不存在,记得同步目录时一定要在末尾加上/

    -a    会把软连接原原本本的拷贝过去;

    -v    可视化显示同步信息,如传输的文件大小,传输速度,发送和接收的大小;

    rsync -avL test1/ test2/    加-L会拷贝软连接文件对应的源文件到目标目录里面;

    touch test2/1.txt; rsync -avu test1/ test2/    -u 选项的作用是,如果目标文件比源文件新,那么会忽略掉该文件

    rsync -av --delete test1/ test2/   #这样会把test2/目录比test1/目录多出来的文件删除掉

    rsync -a --exclude=“2.txt” test1/ test2/  #在同步的过程中,会忽略掉2.txt这个文件

    rsync -a --progress --exclude=“*.txt” test1/ test2/  #--progress 显示同步过程的详细信息,--exclude后面也可以使用通配符*

    4. rsync应用实例 - ssh方式访问

    第一种方式:本地拷贝到远程linux主机,需要输入远程主机的密码;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@yong rsync]# rsync -avL test1/ root@192.168.20.10:/tmp/test2/
    root@192.168.20.10's password: 
    sending incremental file list
    created directory /tmp/test2
    ./
    1
    1.txt
    2
    2.txt
    3
    aa
    sent 336 bytes  received 129 bytes  132.86 bytes/sec
    total size is 0  speedup is 0.00

    第二种方式:从远程linux主机同步到本地机器,也需要输入远程主机的密码;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@yong rsync]# rsync -avL root@192.168.20.10:/tmp/test2/ ./test3/
    root@192.168.20.10's password: 
    receiving incremental file list
    created directory ./test3
    ./
    1
    1.txt
    2
    2.txt
    3
    aa
    sent 128 bytes  received 329 bytes  130.57 bytes/sec
    total size is 0  speedup is 0.00

    通过创建秘钥对,让两台机器产生信任关系同步数据时不用输入密码

    具体步骤如下:

    在A机器当前用户家目录创建.ssh目录,执行ssh-keygen命令,提示输入密码的时候直接回车,默认为空密码,最后生成公钥id_rsa.pub和私钥id_rsa文件;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [root@yong ~]# mkdir .ssh
    [root@yong ~]# ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in 
    /root/.ssh/id_rsa
    .
    Your public key has been saved in 
    /root/.ssh/id_rsa.pub
    .
    The key fingerprint is:
    ab:0e:83:11:69:69:34:d2:34:87:b8:07:40:36:e4:a3 
    root@yong.com
    The key's randomart image is:
    +--[ RSA 2048]----+
    |*O=..            |
    |=o+*             |
    | =*              |
    |ooo.             |
    |E..     S        |
    |   o     .       |
    |  . o   .        |
    |     o .         |
    |     .o          |
    +-----------------+
    [
    root@yong ~]# ls .ssh/
    id_rsa  id_rsa.pub
    1
    2
    [root@yong ~]# cat .ssh/id_rsa.pub 
    ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArLHpwzyzfPtjihx90/vdl9HqtLfmpuNDGYL+UWOcFAAul6PVz81fb/0RSAAPSu1Q3UdBWXlTqPUH4JucwaxYW+obW/zmteuZRaGf06iY01cj/Nr74ML0792cvfjCU87FyEH+ZcvNhgRU+iTo+ES0kkLEuHV1x3JJLOhgYzIU0RtuU3CRiFxPHo92jNjpOs66YW3TbOX0AEB47WoRmKThiQVwoU7Lwqpl/N7vQHtdj9vPtsVZsguKlCB5a2YoxRpnbdn8a5jdzVKuy+hBrdfP/1NkmVU9mBTV/f0b+is5uHmQqNVAQW5fIi7QdVWG8HuyQliOXZUxoxGPCAjCuNy+dw== root@yong.com

    拷贝A机器的公钥内容,复制到B机器的/root/.ssh/authorized_keys文件中(如果B机器没有.ssh目录和authorized_keys文件需要创建)

    1
    2
    3
    4
    [root@localhost ~]# mkdir .ssh
    [root@localhost ~]# chmod 700 .ssh/
    [root@localhost ~]# touch .ssh/authorized_keys
    [root@localhost ~]# vi .ssh/authorized_keys

    返回到A机器,执行命令ssh root@192.168.20.10  (B机器的ip地址)不用输入密码即可访问B机器了。

    1
    2
    3
    4
    5
    6
    7
    8
    [root@yong ~]# ssh root@192.168.20.10
    The authenticity of host '192.168.20.10 (192.168.20.10)' can't be established.
    RSA key fingerprint is 84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.20.10' (RSA) to the list of known hosts.
    Last login: Wed Apr 22 19:35:40 2015 from 192.168.20.1
    [root@localhost ~]# hostname 
    localhost.localdomain

    A机器执行exit退出,执行同步命令,这次不用输入密码即可同步。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@localhost ~]# exit
    logout
    Connection to 192.168.20.10 closed.
    [root@yong ~]# rsync -av rsync/test1/ root@192.168.20.10:/tmp/test3/
    sending incremental file list
    created directory /tmp/test3
    ./
    1
    1.txt
    2
    2.txt
    3
    aa
    sent 336 bytes  received 129 bytes  310.00 bytes/sec
    total size is 0  speedup is 0.00

    如果ssh端口不是22,那么需要指定一个端口号,命令为:rsync -av "--rsh=ssh -p port” /dir1/ root@192.168.20.10:/tmp/dir2/ 或者 rsync -av  -e "ssh -p port"  /dir1/ root@192.168.20.10:/tmp/dir2/

    更改B机器的ssh端口号为222,在A机器使用telnet 检测B机器22端口,提示连接拒绝,当然也不能同步数据,需要指定端口号 "--rsh=ssh -p 222" 可以同步数据,更改端口号之后更加安全,防止他人通过端口攻击。 

    更改ssh端口号,编辑/etc/ssh/sshd_config文件,找到PORT 22这一行更改即可,保存退出后,重启sshd服务生效。

    1
    2
    3
    4
    [root@localhost ~]# vi /etc/ssh/sshd_config 
    [root@localhost ~]# service sshd restart
    停止 sshd:                                                [确定]
    正在启动 sshd:
    1
    2
    3
    [root@yong ~]# telnet 192.168.20.10 22
    Trying 192.168.20.10...
    telnet: connect to address 192.168.20.10: Connection refused
    1
    2
    3
    4
    [root@yong ~]# rsync -av rsync/test1/ root@192.168.20.10:/tmp/test2/
    ssh: connect to host 192.168.20.10 port 22: Connection refused
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@yong ~]# rsync -av "--rsh=ssh -p 222" rsync/test1/ root@192.168.20.10:/tmp/test2/
    sending incremental file list
    created directory /tmp/test2
    ./
    1
    1.txt
    2
    2.txt
    3
    aa
    sent 336 bytes  received 129 bytes  310.00 bytes/sec
    total size is 0  speedup is 0.00

     

    5. rsync应用实例 - 服务器客户端C/S模式

    这种方式可以理解成这样,在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后本机作为rsync的一个客户端去连接远程的rsync服务器。安全性能会大大提高,也可以自定义配置很多项目,比较灵活。日常环境推荐使用。

     

    需要新建配置文件 /etc/rsyncd.conf ,内容如下,#号后的内容不用写进去;

    #port=873        #监听端口默认为873,也可以是别的端口

    log file=/var/log/rsync.log  #指定日志

    pid file=/var/run/rsyncd.pid  #指定pid

    #address=192.168.20.10  #可以定义绑定的ip

    以上部分为全局配置部分,以下为模块内的设置

    [test]    #为模块名,自定义

    path=/tmp/rsync  # 指定该模块对应在哪个目录下

    use chroot=true #是否限定在该目录下,默认为true,当有软连接时,需要改为fasle

    max connections=4  # 指定最大可以连接的客户端数

    read only=no  #是否为只读,yes为只读,不能在对应目录下写入文件。

    list=true  #是否可以列出模块名

    uid=root #以哪个用户的身份来传输

    gid=root  #以哪个组的身份来传输

    auth users=test #指定验证用户名,可以不设置

    secrets file=/etc/rsyncd.passwd #指定密码文件,如果设定验证用户,这一项必须设置

    hosts allow=192.168.0.101 #设置可以允许访问的主机,可以是网段

    密码文件/etc/rsyncd.passwd的内容格式为:username:password

    启动服务的命令是:rsync --daemon

    默认去使用/etc/rsyncd.conf这个配置文件,也可以指定配置文件 rsync --daemon --config=/etc/rsyncd2.conf

    实验,编辑rsyncd.conf文件,保存退出;

    配置文件hosts allow 允许访问主机设置ip网段,也可以设置固定ip,多个ip用空格分隔。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@localhost rsync]# cat /etc/rsyncd.conf 
    #port=873
    log file=/var/log/rsync.log
    pid file=/var/run/rsyncd.pid
    #address=192.168.20.10
    [test]
    path=/tmp/rsync
    use chroot=true
    max connections=4
    read only=no
    list=true
    uid=root
    gid=root
    #auth users=test
    #secrets file=/etc/rsyncd.passwd
    hosts allow=192.168.20.0/24

    启动rsync服务,进程也启动,默认873端口也监听;

    1
    2
    3
    4
    5
    6
    7
    [root@localhost tmp]# rsync --daemon
    [root@localhost tmp]# ps aux |grep rsync
    root      1583  0.0  0.1   6232   644 ?        Ss   14:49   0:00 rsync --daemon
    root      1585  0.0  0.1   5976   740 pts/0    S+   14:50   0:00 grep rsync
    [root@localhost tmp]# netstat -nlp |grep rsync
    tcp        0      0 0.0.0.0:873                 0.0.0.0:*      LISTEN      1583/rsync          
    tcp        0      0 :::873                      :::*           LISTEN      15

    配置OK后,访问远程服务器#rsync -av test@192.168.20.10::test/test1/ /tmp/test1/

    中间2个冒号,后面跟模块名test,后面跟test1目录,实际就是/tmp/rsync/test1/目录,模块对应的目录里面。

    1)从本地同步数据到服务器端,同步的时候也可以进行改名;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@yong rsync]# rsync -avL --progress test1/ test@192.168.20.10::test/test/
    sending incremental file list
    created directory /test
    ./
    1
               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/10)
    1.txt
            1076 100%    0.00kB/s    0:00:00 (xfer#2, to-check=7/10)
    2
               0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=6/10)
    2.txt
               0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=5/10)
    3
               0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=4/10)
    aa
               0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=3/10)
    tree
           36464 100%    1.93MB/s    0:00:00 (xfer#7, to-check=2/10)
    a1/
    b1/
    sent 37992 bytes  received 152 bytes  3632.76 bytes/sec
    total size is 37540  speedup is 0.98
    1
    2
    [root@localhost rsync]# ls test/
    1  1.txt  2  2.txt  3  a1  aa  b1  tree

    2)从服务器同步数据到本地;

    1
    2
    3
    4
    5
    6
    7
    8
    [root@yong rsync]# rsync -avL --progress 192.168.20.10::test/abc ./
    receiving incremental file list
    abc
               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)
    sent 45 bytes  received 94 bytes  13.24 bytes/sec
    total size is 0  speedup is 0.00
    [root@yong rsync]# ls abc 
    abc

    3)更改服务器配置文件read only=yes  再从本地同步数据到服务器,会报错提示module模块只读,不能写入数据;从服务器同步到本地不受影响。

    1
    2
    3
    [root@localhost rsync]# sed -i 's/read only=no/read only=yes/' /etc/rsyncd.conf 
    [root@localhost rsync]# grep 'read' /etc/rsyncd.conf 
    read only=yes
    1
    2
    3
    4
    5
    6
    [root@yong rsync]# rsync -avL --progress test1/ 192.168.20.10::test/abc/
    sending incremental file list
    ERROR: module is read only
    rsync error: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6]
    rsyncread error: Connection reset by peer (104)
    rsync error: error in rsync protocol data stream (code 12) at io.c(759) [sender=3.0.6]
  • 相关阅读:
    jxl导入/导出excel
    iText导出pdf、word、图片
    Java CSV操作(导出和导入)
    Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
    jQuery插件:跨浏览器复制jQuery-zclip
    微信小程序之表单提交
    微信小程序只之全局存储
    微信小程序之工具js封装与使用
    重构的艺术 深入认识
    重构的素养
  • 原文地址:https://www.cnblogs.com/yuzhaokai0523/p/4453079.html
Copyright © 2011-2022 走看看