TongXu-Server

Rsync

  • remote sync 远程同步

    • 与FTP或scp不同的是,rsync会检查发送方和接收方已有的文件,仅传输有变动的部分(默认使用文件大小和修改时间决定文件是否需要更新)。
  • 安装

    • sudo pacman -S rsync
  • 基本用法

    rsync -r source destination #r表示递归,必须要此参数,否则rsync启动不成功
    rsync -r source1 source2 destination #source1 source2都会被同步到destination
    
    rsync -a source destination #a除了可以同步以外,还可以同步元信息,比如修改时间,权限等
    
    rsync -anv sour/ destination #-n --dry-run模拟执行的结果
    
    rsync -av --delete source/ destination #将删除只存在于目标目录,不存在于源目录的文件
    
    #--exclude 排除文件
    rsync -av --exclude='*.txt' source/ destination 
    rsync -av --exclude='.*' source/ destination 
    rsync -av --exclude='file1.txt' --exclude='dir1/*' source/ destination
    rsync -av --exclude={'file1.txt'.'dir1/*'} source destination
    rsync -av --exclude='exclude-file.txt' source/ destination 
    
    #--include 指定必须同步的文件模式
    rsync -av --include='*.txt' --exclude='*' source/ destination 
    
    #ssh协议
    rsync -av source/ username@remote_host:destination 
    rsync -av username@remote_host:source/ destination
    #若ssh有附加参数
    rsync -av --progress -e 'ssh -p 2244' source/ user@remote_host:destination
    
    #rsync协议
    #如果另一台服务器安装了rsync守护程序
    rsync -av source/ 192.168.10.254::module/destination 
    #module是rsync守护程序制定的一个资源名,有管理元分配,
    #rsync rsync://192.168.10.254 可以查看rsync守护程序所有的module列表
    
    #增量备份
    rsync -av --progress --delete --link-dest /compare/path /source/path /target/path
    #--link-dest 指定基准目录compare/path,源目录和基准目录比较,找出变动的文件,将他们拷贝到目标目录/target/path中,那些没变动的目录会生成硬链接。
    #这个命令第一次是全量备份,后面对是增量备份