Linux远程备份工具Rsync使用案例

:下面所有例子中 – - 之间实际上是没有空格的,使用时请删除空格。

Rsync是一个远程数据同步工具,可通过LAN 或互联网快速同步多台主机间的文件。Rsync 本来是用以取代 rcp的一个工具,它当前由 rsync.samba.org 维护。Rsync 使用所谓的”Rsync演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

Rsync 的特色:

  1. 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。
  2. 安全:rsync 允许通过 ssh 协议来加密传输数据。
  3. 更少的带宽:rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  4. 特权:安装和执行 rsync 无需特别的权限

基本语法:

rsync options source destination


源和目标都可以是本地或远程,在进行远程传输的时候,需要指定登录名、远程服务器及文件位置

 

 rsync参数的具体解释如下:

Html代码   收藏代码
  1. -v, --verbose 详细模式输出  
  2. -q, --quiet 精简输出模式  
  3. -c, --checksum 打开校验开关,强制对文件传输进行校验  
  4. -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD  
  5. -r, --recursive 对子目录以递归模式处理  
  6. -R, --relative 使用相对路径信息  
  7. -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。  
  8. --backup-dir 将备份文件(如~filename)存放在在目录下。  
  9. -suffix=SUFFIX 定义备份文件前缀  
  10. -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)  
  11. -l, --links 保留软链结  
  12. -L, --copy-links 想对待常规文件一样处理软链结  
  13. --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结  
  14. --safe-links 忽略指向SRC路径目录树以外的链结  
  15. -H, --hard-links 保留硬链结  
  16. -p, --perms 保持文件权限  
  17. -o, --owner 保持文件属主信息  
  18. -g, --group 保持文件属组信息  
  19. -D, --devices 保持设备文件信息  
  20. -t, --times 保持文件时间信息  
  21. -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间  
  22. -n, --dry-run现实哪些文件将被传输  
  23. -W, --whole-file 拷贝文件,不进行增量检测  
  24. -x, --one-file-system 不要跨越文件系统边界  
  25. -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节  
  26. -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步  
  27. --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息  
  28. -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件  
  29. --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件  
  30. --delete 删除那些DST中SRC没有的文件  
  31. --delete-excluded 同样删除接收端那些被该选项指定排除的文件  
  32. --delete-after 传输结束以后再删除  
  33. --ignore-errors 及时出现IO错误也进行删除  
  34. --max-delete=NUM 最多删除NUM个文件  
  35. --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输  
  36. --force 强制删除目录,即使不为空  
  37. --numeric-ids 不将数字的用户和组ID匹配为用户名和组名  
  38. --timeout=TIME IP超时时间,单位为秒  
  39. -I, --ignore-times 不跳过那些有同样的时间和长度的文件  
  40. --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间  
  41. --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0  
  42. -T --temp-dir=DIR 在DIR中创建临时文件  
  43. --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份  
  44. -P 等同于 --partial  
  45. --progress 显示备份过程  
  46. -z, --compress 对备份的文件在传输时进行压缩处理  
  47. --exclude=PATTERN 指定排除不需要传输的文件模式  
  48. --include=PATTERN 指定不排除而需要传输的文件模式  
  49. --exclude-from=FILE 排除FILE中指定模式的文件  
  50. --include-from=FILE 不排除FILE指定模式匹配的文件  
  51. --version 打印版本信息  
  52. --address 绑定到特定的地址  
  53. --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件  
  54. --port=PORT 指定其他的rsync服务端口  
  55. --blocking-io 对远程shell使用阻塞IO  
  56. -stats 给出某些文件的传输状态  
  57. --progress 在传输时现实传输过程  
  58. --log-format=formAT 指定日志文件格式  
  59. --password-file=FILE 从FILE中得到密码  
  60. --bwlimit=KBPS 限制I/O带宽,KBytes per second  
  61. -h, --help 显示帮助信息  
 

 

 

 

样例:

1 在本地机器上对两个目录进行同步

$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list … done
sva.xml
svB.xml
.
sent 26385 bytes received 1098 bytes 54966.00 bytes/sec
total size is 44867 speedup is 1.63
$

参数:

  1. -z 开启压缩
  2. -v 详情输出
  3. -r 表示递归

2 利用 rsync -a 让同步时保留时间标记

rsync 选项 -a 称为归档模式,执行以下操作

  1. 递归模式
  2. 保留符号链接
  3. 保留权限
  4. 保留时间标记
  5. 保留用户名及组名
$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list … done
./
sva.xml
svB.xml
.
sent 26499 bytes received 1104 bytes 55206.00 bytes/sec
total size is 44867 speedup is 1.63
$

3 仅同步一个文件

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys

sent 42 bytes received 12380 bytes 3549.14 bytes/sec
total size is 12288 speedup is 0.99

4 从本地同步文件到远程服务器

$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/

Password:
building file list … done
./
rpm/
rpm/Basenames
rpm/Conflictname

sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec
total size is 45305958 speedup is 2.87

就像你所看到的,需要在远程目录前加上 ssh 登录方式,格式为 username@machinename:path

5 同步远程文件到本地

和上面差不多,做个相反的操作

$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm
 /root/temp
Password:
receiving file list … done
rpm/
rpm/Basenames
.
sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec
total size is 45305958 speedup is 2.87

6 同步时指定远程 shell

用 -e 参数可以指定远程 ssh ,比如用 rsync -e ssh 来指定为 ssh

$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm
 /root/temp
Password:
receiving file list … done
rpm/
rpm/Basenames

sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec
total size is 45305958 speedup is 2.87

7 不要覆盖被修改过的目的文件

使用 rsync -u 选项可以排除被修改过的目的文件

$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames

$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm
 /root/temp
Password:
receiving file list … done
rpm/

sent 122 bytes received 505 bytes 114.00 bytes/sec
total size is 45305958 speedup is 72258.31

$ ls -lrt
total 39088
-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames

8 仅仅同步目录权(不同步文件)

使用 -d 参数

$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/
 .
Password:
receiving file list … done
logrotate.status
CAM/
YaST2/
acpi/

sent 240 bytes received 1830 bytes 318.46 bytes/sec
total size is 956 speedup is 0.46

9 查看每个文件的传输进程

使用 – -progress 参数

$ rsync -avz – -progress thegeekstuff@192.168.200.10:/var/lib/rpm/
 /root/temp/
Password:
receiving file list …
19 files to consider
./
Basenames
5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19)
Conflictname
12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec
total size is 45305958 speedup is 2.87

10 删除在目的文件夹中创建的文件

用 – -delete 参数

# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt

$ rsync -avz – -delete thegeekstuff@192.168.200.10:/var/lib/rpm/
 .
Password:
receiving file list … done
deleting new-file.txt
./

sent 26 bytes received 390 bytes 48.94 bytes/sec
total size is 45305958 speedup is 108908.55

11 不要在目的文件夹中创建新文件

有时能只想同步目的地中存在的文件,而排除源文件中新建的文件,可以使用 – -exiting 参数

$ rsync -avz –existing root@192.168.1.2:/var/lib/rpm/
 .
root@192.168.1.2′s
 password:
receiving file list … done
./

sent 26 bytes received 419 bytes 46.84 bytes/sec
total size is 88551424 speedup is 198991.96

12 查看源和目的文件之间的改变情况

用 -i 参数

$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/
 /root/temp/
Password:
receiving file list … done
>f.st…. Basenames
.f….og. Dirnames

sent 48 bytes received 2182544 bytes 291012.27 bytes/sec
total size is 45305958 speedup is 20.76

输出结果中在每个文件最前面会多显示 9 个字母,分别表示为

> 已经传输
f 表示这是一个文件
d 表示这是一个目录
s 表示尺寸被更改
t 时间标记有变化
o 用户被更改
g 用户组被更改

13 在传输时启用包含和排除模式

$ rsync -avz – -include ‘P*’ – -exclude ‘*’ thegeekstuff@192.168.200.10:/var/lib/rpm/
 /root/temp/
Password:
receiving file list … done
./
Packages
Providename
Provideversion
Pubkeys

sent 129 bytes received 10286798 bytes 2285983.78 bytes/sec
total size is 32768000 speedup is 3.19

14 不要传输大文件

使用 – - max-size 参数

$ rsync -avz – -max-size=’100K’ thegeekstuff@192.168.200.10:/var/lib/rpm/
 /root/temp/
Password:
receiving file list … done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername

sent 252 bytes received 123081 bytes 18974.31 bytes/sec
total size is 45305958 speedup is 367.35

15 传输所有文件

不管有没有改变,再次把所有文件都传输一遍,用 -W 参数

# rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/
 /root/temp
Password:
receiving file list … done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name

sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec
total size is 45305958 speedup is 2.87

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值