zoukankan      html  css  js  c++  java
  • rsync之实战

    简介

    rsync是远程(或本地)复制和同步文件最常用的命令。 借助rsync命令,你可以跨目录,跨磁盘和跨网络远程与本地数据进行复制和同步。举例来说:在两台Linux主机之间进行数据备份和镜像。本文介绍在Linux主机上进行远程和本地传输文件的常见用法,不需要root账户也可以允许rsync。

    rsync特性

    1. 高效地复制同步数据到对端,或者对端到本地
    2. 支持复制链接、设备、属主、属组、权限
    3. 比scp(Secure Copy)更快。rsync使用远程更新协议( remote-update protocol ),这允许仅仅传输两组文件之间的差异。对于首次传输,它将文件或目录的全部内容从源复制到目标,但是从下次起,它仅将变化部分复制到目标。
    4. Rsync消耗较少的带宽,因为它使用压缩和解压缩方法,同时发送和接收数据两端。HTTP压缩技术

    安装

    yum -y install rsync

     

    基本语法

    rsync options source destination

    -v : 详细模式输出

    -r : 递归拷贝数据,但是传输数据时不保留时间戳和权限

    -a : 归档模式, 归档模式总是递归拷贝,而且保留符号链接、权限、属主、属组时间戳

    -z : 压缩传输

    -h : human-readable

    --progress: 显示传输过程

    --exclude=PATTERN 指定排除传输的文件模式

    --include=PATTERN 指定需要传输的文件模式

    --delete 同步时,删除那些DST中有,而SRC没有的文件

    --max-size:限定传输文件大小的上限

    --dry-run:显示那些文件将被传输,并不会实际传输

    --bwlimit:限制传输带宽

    -W:拷贝文件,不进行增量检测

    使用场景

    本地拷贝同步文件、目录

    同步一个文件从本地一个目录到另一个目录,如果目标目录不纯在,会自动创建

    [root@tecmint]# rsync -zvh backup.tar /tmp/backups/
    created directory /tmp/backups
    backup.tar
    sent 14.71M bytes  received 31 bytes  3.27M bytes/sec
    total size is 16.18M  speedup is 1.10

    同步目录

    [root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/
    sending incremental file list
    rpmpkgs/
    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
    rpmpkgs/nagios-3.5.0.tar.gz
    rpmpkgs/nagios-plugins-1.4.16.tar.gz
    sent 4.99M bytes  received 92 bytes  3.33M bytes/sec
    total size is 4.99M  speedup is 1.00

    远程拷贝同步文件、目录

    本地到远程

    [root@tecmint]$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/
    
    root@192.168.0.101's password:
    
    sending incremental file list
    
    ./
    
    httpd-2.2.3-82.el5.centos.i386.rpm
    
    mod_ssl-2.2.3-82.el5.centos.i386.rpm
    
    nagios-3.5.0.tar.gz
    
    nagios-plugins-1.4.16.tar.gz
    
    sent 4993369 bytes  received 91 bytes  399476.80 bytes/sec
    
    total size is 4991313  speedup is 1.00

    远程到本地

    [root@tecmint]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms
    
    root@192.168.0.100's password:
    
    receiving incremental file list
    
    created directory /tmp/myrpms
    
    rpmpkgs/
    
    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
    
    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
    
    rpmpkgs/nagios-3.5.0.tar.gz
    
    rpmpkgs/nagios-plugins-1.4.16.tar.gz
    
    sent 91 bytes  received 4.99M bytes  322.16K bytes/sec
    
    total size is 4.99M  speedup is 1.00

    通过ssh使用rsync

    SSH(Secure Shell)以加密方式传输数据时,接获数据并破解很难。rsync同ssh一起使用可以增强传输按权限,注意可能需要用户密码。

    从本地到远程

    [root@tecmint]# rsync -avzhe ssh backup.tar root@192.168.0.100:/backups/
    
    root@192.168.0.100's password:
    
    sending incremental file list
    
    backup.tar
    
    sent 14.71M bytes  received 31 bytes  1.28M bytes/sec
    
    total size is 16.18M  speedup is 1.10

    从远程到本地

    [root@tecmint]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/
    
    root@192.168.0.100's password:
    
    receiving incremental file list
    
    install.log
    
    sent 30 bytes  received 8.12K bytes  1.48K bytes/sec
    
    total size is 30.74K  speedup is 3.77

    传输数据时显示传输过程

    使用--progress参数

    [root@tecmint]# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs
    
    root@192.168.0.100's password:
    
    sending incremental file list
    
    created directory /root/rpmpkgs
    
    rpmpkgs/
    
    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
    
               1.02M 100%        2.72MB/s        0:00:00 (xfer#1, to-check=3/5)
    
    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
    
              99.04K 100%  241.19kB/s        0:00:00 (xfer#2, to-check=2/5)
    
    rpmpkgs/nagios-3.5.0.tar.gz
    
               1.79M 100%        1.56MB/s        0:00:01 (xfer#3, to-check=1/5)
    
    rpmpkgs/nagios-plugins-1.4.16.tar.gz
    
               2.09M 100%        1.47MB/s        0:00:01 (xfer#4, to-check=0/5)
    
    sent 4.99M bytes  received 92 bytes  475.56K bytes/sec
    
    total size is 4.99M  speedup is 1.00

    使用--exclude和--include

    传输R开头的文件、目录,派出其他情况的文件、目录

    [root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/var/lib/rpm/ /root/rpm
    
    root@192.168.0.101's password:
    
    receiving incremental file list
    
    created directory /root/rpm
    
    ./
    
    Requirename
    
    Requireversion
    
    sent 67 bytes  received 167289 bytes  7438.04 bytes/sec
    
    total size is 434176  speedup is 2.59

    说明:

        1. 如果多个需要排除或者包含的时候, 需要设置多个,例如

    rsync -avzh --exclude=".svn" --exclude="temp"
    说明:同步除.svn和temp之外的文件
    
    当然还有一种方式, 可以使用 --exclude-from=exclude.list 的方式指定文件, 该文件中填写具体的过滤文件
    
    例如exclude.list文件内容:
    .svn
    *.log

      exclude-from指定的文件最好是绝对路径, 而exclude.list文件中定义的则为相对路径

    使用--delete

    --delete用于同步时,删除那些DST中有,而SRC没有的文件

    再target主机创建test.txt

    [root@tecmint]# touch test.txt
    [root@tecmint]# rsync -avz --delete root@192.168.0.100:/var/lib/rpm/ .
    Password:
    receiving file list ... done
    deleting test.txt
    ./
    sent 26 bytes  received 390 bytes  48.94 bytes/sec
    total size is 45305958  speedup is 108908.55

    使用--max-size

    --max-size用于限制传输时文件的大小,只有≤max-size的文件才会被传输

    [root@tecmint]# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.100:/root/tmprpm
    
    root@192.168.0.100's password:
    
    sending incremental file list
    
    created directory /root/tmprpm
    
    ./
    
    Conflictname
    
    Group
    
    Installtid
    
    Name
    
    Provideversion
    
    Pubkeys
    
    Requireversion
    
    Sha1header
    
    Sigmd5
    
    Triggername
    
    __db.001
    
    sent 189.79K bytes  received 224 bytes  13.10K bytes/sec
    
    total size is 38.08M  speedup is 200.43

    传输完毕后自动删除源文件、目录

    假设你有一个主Web Server和一个数据备份Sever,你创建了每日备份任务并与备份服务器同步备份,你不希望在Web Server中保留该本地备份副本。

    那么,你是否会等待传输完成然后手动删除这些本地备份文件? 当然不。 可以使用--remove-source-files选项完成此自动删除

    [root@tecmint]# rsync --remove-source-files -zvh backup.tar /tmp/backups/

    backup.tar

    sent 14.71M bytes received 31 bytes 4.20M bytes/sec

    total size is 16.18M speedup is 1.10

    [root@tecmint]# ll backup.tar

    ls: backup.tar: No such file or directory

    使用--dry-run

    如果你对rsync不熟悉,贸然使用rsync可能会搞乱对端文件、目录。借助--dry-run可以让你知道会传输些什么东西,但实际上并没有传输任何东西。如果输出结果与你的预期吻合,可以去掉--dry-run,进行实际的传输工作。

    root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/
    
    backup.tar
    
    sent 35 bytes  received 15 bytes  100.00 bytes/sec
    
    total size is 16.18M  speedup is 323584.00 (DRY RUN)

    设置同步时带宽

    使用--bwlimit可以设置同步时网络带宽上限

    [root@tecmint]# rsync --bwlimit=100 -avzhe ssh  /var/lib/rpm/  root@192.168.0.100:/root/tmprpm/
    root@192.168.0.100's password:
    sending incremental file list
    sent 324 bytes  received 12 bytes  61.09 bytes/sec
    total size is 38.08M  speedup is 113347.05

    rsync由于采用远程更新协议( remote-update protocol ),默认是同步变化的字节或块。使用-W可以取消这种机制,整个文件同步

    [root@tecmint]# rsync -zvhW backup.tar /tmp/backups/backup.tar
    backup.tar
    sent 14.71M bytes  received 31 bytes  3.27M bytes/sec
    total size is 16.18M  speedup is 1.10

    指定端口

    默认端口是22, 如果是其他端口, 需要进行指定

    rsync -avz -e 'ssh -p 2222' user@remote-server:/path/to/remote/dir
  • 相关阅读:
    spring3: Bean的命名与Bean的实例化
    极客软件测试52讲总结分享
    如何有效地搭建测试环境?
    用xshell 连接docker Linux服务器
    python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告
    CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
    jenkins 关联 钉钉机器人
    Jenkins pipeline 语法详解
    jenkins 添加 证书凭证Credentials
    项目中使用的S2SH整合中使用的struts.xml(参考模板)
  • 原文地址:https://www.cnblogs.com/xingxia/p/rsync_use.html
Copyright © 2011-2022 走看看