zoukankan      html  css  js  c++  java
  • linux的rsync工具的常用选项及ssh同步介绍

    rsync是数据备份工具(字面意思可以理解为远程同步),不仅可以远程同步数据,而且可以本地同步数据(类似与cp),但不同于cp或scp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。

    安装rsync命令:yum install -y rsync

    讲解rsync的用法

    举例将/etc/passwd同步到/tmp/目录下,并改名为1.txt,操作如下:

    [root@gary-tao ~]# rsync -av /etc/passwd /tmp/1.txt
    sending incremental file list
    passwd
    
    sent 1460 bytes  received 31 bytes  2982.00 bytes/sec
    total size is 1386  speedup is 0.93
    

    如果是远程复制,数据备份的形式就是这样的形式——IP:path,比如172.16.111.110:/root/,具体用法如下:

    [root@gary-tao ~]# rsync -av /tmp/1.txt 172.16.111.110:/tmp/2.txt 
    The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established.
    ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts.
    root@172.16.111.110's password: 
    sending incremental file list
    1.txt
    
    sent 1459 bytes  received 31 bytes  270.91 bytes/sec
    total size is 1386  speedup is 0.93
    

    rsync的命令格式

     rsync [OPTION]SRC   DEST
     rsync [OPTION]SRC   [user@]host:DEST
     rsync [OPTION][user@]host:SRC   DEST
     rsync [OPTION]SRC   [user@]host::DEST
     rsync [OPTION][user@]host::SRC   DEST
    

    解释:在前面的例子中,第一个例子为第一种格式,第二个例子为第二种格式,但不同的是并没有加用户名user@host,如果不加默认指的是root。第三种格式是用远程目录同步数据到本地。第四种和第五种格式使用了两个冒号,这种格式和其他格式的验证方式不同。


    2. rsync常用选项

    rsync常用选项

    -a 这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-rlptgoD,-a选项后面可以跟一个--no-OPTION,表示关闭-rlptgoD中的某一个,比如-a--no-l等同于-rlptgoD。
    -r 表示以递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。
    -v 表示打印一些信息,如文件列表,文件数量等
    -l 表示保留软链接
    -L 表示像对待常规文件一样处理软链接,如果SRC文件中有软链接时,则加上该选项后,将会把软链接指向的目标文件一起复制到DEST。
    -p 表示保持文件权限
    -o 表示保持文件属主信息
    -g 表示保持文件属组信息
    -D 表示保持设备文件信息
    -t 表示保持文件时间信息
    --delete 表示删除DEST中SRC中没有的文件
    --exclude=PATTERN 表示指定排除SRC中不需要传输的文件,等号后面跟文件名,可以用通配符如*.txt
    --progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。
    -u 表示把dest中比src还新的文件排除掉,不会覆盖
    -z 加上该选项,将会在传输过程中压缩
    
    但是常用的选项是-a,-v,-z,--delete和--exclude。
    

    建立目录和文件

    [root@localhost ~]# mkdir rsync
    [root@localhost ~]# cd rsync
    [root@localhost rsync]# touch 1 2 3 /root/123.txt
    [root@localhost rsync]# ln -s /root/123.txt ./123.txt
    [root@localhost rsync]# ls -l
    总用量 0
    -rw-r--r--. 1 root root  0 125 20:57 1
    lrwxrwxrwx. 1 root root 13 125 20:57 123.txt -> /root/123.txt
    -rw-r--r--. 1 root root  0 125 20:57 2
    -rw-r--r--. 1 root root  0 125 20:57 3
    

    -av 把root下的rsync目录同步到tmp下并且改名rsync_dest,示例如下:

    [root@localhost ~]# rsync -av /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    123.txt -> /root/123.txt
    
    sent 89 bytes  received 15 bytes  208.00 bytes/sec
    total size is 13  speedup is 0.12
    

    加上-L选项后,同步软连接文件时会把源文件同步,示例如下:

    [root@localhost ~]# rsync -avL /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    123.txt
    
    sent 103 bytes  received 31 bytes  268.00 bytes/sec
    total size is 0  speedup is 0.00
    

    -delete 同步时删除目标目录rsync_dest中源目录rsync没有的文件,示例如下:

    [root@localhost ~]# rsync -avL -delete /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    
    sent 64 bytes  received 12 bytes  152.00 bytes/sec
    total size is 0  speedup is 0.00
    

    --exclude 同步时过滤掉文件名或目录名为.txt,不同步(支持写多个exclude,但不支持同一个exclude有多个条件),示例如下:

    [root@localhost rsync]# rsync -avL --exclude "*.txt" /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    created directory /tmp/rsync_dest
    ./
    1
    2
    3
    
    sent 172 bytes  received 72 bytes  488.00 bytes/sec
    total size is 0  speedup is 0.00
    

    过滤掉带1开头,示例如下:

    [root@localhost ~]# rsync -avL --exclude "*.txt" --exclude "1" /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    created directory /tmp/rsync_dest
    ./
    2
    3
    
    sent 127 bytes  received 53 bytes  360.00 bytes/sec
    total size is 0  speedup is 0.00
    

    -P 选项是显示同步过程,比如速率,示例如下:

    [root@localhost ~]# rsync -avP /root/rsync/ /tmp/rsync_dest/
    sending incremental file list
    created directory /tmp/rsync_dest
    ./
    1
               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
    123.txt -> /root/123.txt
    2
               0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/5)
    3
               0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=0/5)
    
    sent 209 bytes  received 75 bytes  568.00 bytes/sec
    total size is 13  speedup is 0.05
    

    -u 选项如果目标文件中的文件比源文件新,则不同步,示例如下:

    [root@localhost rsync_dest]# rsync -avPu /root/rsync/ /tmp/rsync_dest/  //加-u保留之前文件内容
    sending incremental file list
    ./
    
    sent 89 bytes  received 15 bytes  208.00 bytes/sec
    total size is 13  speedup is 0.12
    [root@localhost rsync_dest]# cat 1   //查看文件没有被覆盖
    afdgagadsga
    dagdkdgja
    agdaga
    adgaga
    

    3. rsync通过ssh同步

    ssh同步到另外一台主机

    示例如下:

    [root@gary-tao ~]# rsync -av /etc/passwd 172.16.111.110:/tmp/aming.txt   //把文件拷贝过去
    root@172.16.111.110's password: 
    sending incremental file list
    passwd
    
    sent 1460 bytes  received 31 bytes  271.09 bytes/sec
    total size is 1386  speedup is 0.93
    [root@gary-tao ~]# rsync -avP  172.16.111.110:/tmp/aming.txt /tmp/123.txt  //把文件拷贝过来
    root@172.16.111.110's password: 
    receiving incremental file list
    aming.txt
            1386 100%    1.32MB/s    0:00:00 (xfer#1, to-check=0/1)
    
    sent 30 bytes  received 1468 bytes  332.89 bytes/sec
    total size is 1386  speedup is 0.93
    

    假设对方机器端口不是22的话,那应该如何,示例如下:

    [root@gary-tao ~]# rsync -avP -e "ssh -p 22 " /etc/passwd 172.16.111.110:/tmp/aming.txt
    root@172.16.111.110's password: 
    sending incremental file list
    passwd
            1386 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)
    
    sent 1460 bytes  received 31 bytes  331.33 bytes/sec
    total size is 1386  speedup is 0.93
    
    [root@gary-tao ~]# ssh -p 22 172.16.111.110  //通过输入端口远程连接对方机器
    root@172.16.111.110's password: 
    Last failed login: Tue Dec  5 19:45:42 CST 2017 from 172.16.111.100 on ssh:notty
    There were 2 failed login attempts since the last successful login.
    Last login: Tue Dec  5 19:34:02 2017 from 172.16.111.1
    [root@localhost ~]# 登出
    Connection to 172.16.111.110 closed.
    
  • 相关阅读:
    利用Azure Backup备份和恢复虚拟机(2)
    不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的表进
    Vi命令
    Vi三种模式详解
    C#从Excel中读取数据为空
    Visual Studio 2012/2010/2008 远程调试
    Beyond Compare 3 设置自动换行
    Text类型的字段进行数据替换
    SQLite 连接两个字符串
    基于虚拟日志压缩的数据同步方案
  • 原文地址:https://www.cnblogs.com/fungitive/p/9136197.html
Copyright © 2011-2022 走看看