更新时间:2022-02-14 21:56:16
Rsync命令是一个远程数据同步工具,可通过网络快速同步多台主机间的文件。它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息,也可以做增量的拷贝。支持通过ssh方式来传输文件,这样其保密性会非常好。rsync备份主要分为三种方式,一是本地到本地的备份,二是本地到网络的备份,三是网络到本地的备份。
▎命令格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Local: rsync [OPTION...] SRC... [DEST]
##拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
##使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data ##使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
##从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@192.168.78.192::www /databack ##从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@192.168.78.192::www Usages with just one SRC arg and no DEST arg will list the source files
instead of copying.
##列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.78.192/www |
▎常用选项:
选项 | 作用 |
-a | 包含-rtplgoD |
-r | 同步目录时要加上,类似cp时的-r选项 |
-v | 同步时显示一些信息,让我们知道同步的过程 |
-l |
保留软连接 |
-L | 加上该选项后,同步软链接时会把源文件给同步 |
-p | 保持文件的权限属性 |
-o | 保持文件的属主 |
-g | 保持文件的属组 |
-D | 保持设备文件信息 |
-t |
保持文件的时间属性 |
--delete | 删除DEST中SRC没有的文件 |
--exclude | 过滤指定文件不同步 |
-P |
显示同步过程,比如速率,比-v更加详细 |
-u | 加上该选项后,如果DEST中的文件比SRC新,则不同步 |
-z | 传输时压缩 |
测试示例:
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
31
32
33
|
本地到本地备份: [root@juispan ~] # rsync -aP /tmp/bigfile /usr/local/
sending incremental file list
bigfile 106070960 100% 30.24MB /s 0:00:03 (xfer #1, to-check=0/1)
sent 106083984 bytes received 31 bytes 30309718.57 bytes /sec
total size is 106070960 speedup is 1.00 本地到网络备份: [root@juispan ~] # rsync -av /tmp/bigfile 192.168.137.200:/tmp/ ##接收端也要安装rsync工具
bash : rsync : 未找到命令
rsync : connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
[root@juispan ~] # rsync -av /tmp/bigfile 192.168.137.200:/tmp/
sending incremental file list
bigfile sent 106083984 bytes received 31 bytes 7316138.97 bytes /sec
total size is 106070960 speedup is 1.00 [root@juispan ~] # ll /tmp/bigfile
-rw-r--r--. 1 root root 106070960 7月 20 09:34 /tmp/bigfile
[root@server02 tmp] # ll bigfile
-rw-r--r--. 1 root root 106070960 7月 20 09:34 bigfile ##目标端查看信息完全一致
网络到本地备份: [root@juispan ~] # rsync -av 192.168.137.200:/tmp/bigfile /
receiving incremental file list
bigfile sent 30 bytes received 106083989 bytes 6844130.26 bytes /sec
total size is 106070960 speedup is 1.00 |
▎通过ssh方式同步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@juispan ~] # rsync -av /tmp/ 192.168.137.200:/tmp/
sending incremental file list
./ bigfile py.py yum_save_tx.2017-07-20.09-37.LxNivf.yumtx sent 106085634 bytes received 77 bytes 7858200.81 bytes /sec
total size is 106072339 speedup is 1.00 [root@juispan ~] # rsync -av -e "ssh -p 22" /tmp/bigfile 192.168.137.200:/tmp/
sending incremental file list
sent 33 bytes received 12 bytes 3.91 bytes /sec
total size is 106070960 speedup is 2357132.44 |
▎通过服务的方式同步:
①编辑配置文件/etc/rsyncd.conf
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
31
32
33
34
35
36
|
[root@juispan ~] # cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area port=873 log file = /var/log/rsync/rsync .log
pid file = /var/run/rsyncd .pid
address=192.168.137.100 [ test ]
path= /root/rsync
use chroot= true
max connections=4 ##最大连接数,默认为0,表示无限制
read only=no ##如果为true,则不能上传到该模块指定路径下
list= true ##用户是否可以查询该可用模块
uid=root ##传输时使用的UID
gid=root auth users = test ##传输时使用的用户名
secrets file = /etc/rsyncd . passwd ##密码文件,不设置表示不使用。密码文件权限为600。
hosts allow=192.168.137.200 1.1.1.1 2.2.2.2 ##被允许连接该模块的主机
|
注:密码文件格式:用户名:密码
②启动服务rsync --daemon
1
2
|
[root@juispan ~] #/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
##config参数指定配置文件
|
如果需要关闭,可以kill进程。
③客户端配置
Linux采用自带rsync,Windows采用cwRsync(client)端。
Linux系统中,可以手动执行,也可以执行脚本中的rsync。
▽命令格式参考:
1
|
[root@server02 tmp] # rsync -av --progress rsync@192.168.137.100::backup ./test/
|