您的位置:首页 > 其它

rsync工具的介绍与常用选项,rsync通过ssh同步

2018-01-30 18:24 811 查看

rsync工具介绍

一个系统管理员,数据备份是必不可少,在Linux系统下数据备份的工具很多,其中重点介绍就是rsync工具,rsync不仅可以远程同步数据,还可以本地同步数据,且不会覆盖以前的数据在已经存在的数据情况下,而是先判断已经存在的数据和新的数据差异,只有不同的时候才会把不同的部分覆盖。

以下举个例子;

[root@ask-02 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 1428 bytes received 31 bytes 2918.00 bytes/sec
total size is 1354 speedup is 0.93

可以看到上列中把/etc/passwd复制到了tmp目录下并改名为1.txt

关于rsync的命令格式

rsync [OPTION]... SRC DEST //将数据同步(复制)到本地指定的路径下。

rsync [OPTION]... SRC [USER@]HOST:DEST //将文件复制远程同步到指定的用户的指定路径下,上面的例子没有指定user默认就是root。

rsync [OPTION]... [USER@]HOST:SRC DEST //从远程目录同步数据到本地。

rsync [OPTION]... [USER@]HOST::SRC DEST //从远程目录同步数据到本地,加了两个冒号验证方式不同。

rsync [OPTION]... SRC [USER@]HOST::DEST //将文件复制远程同步到指定的用户的指定路径下,加了两个冒号验证方式不同。

rsync常用选项

rsync命令各项的含义如下

-a:这是归档模式,表示已递归方式传输文件,并保持所有属性,它等同于 -rlptgoD。-a选项后面可以跟一个 --no-OPTION,表示关闭 -rlptgoD中的某一个,比如 -a--no-l等同 -rlptgoD。

-r:表示已递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。

-v:表示打印一些信息,比如文件列表、文件数量等。

-l:表示保留软链接。

-L:表示像对待常规文件一样处理软连接。如果是SRC中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到DST。

-p:表示保持文件权限。

-o:表示保持文件属主信息。

-g:表示保持文件属组信息。

-D:表示保持设备文件信息。

-t:表示保持文件时间信息。

--delete:表示删除DST中SRC没有的文件。

--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟文件名,可以是通用字符模式(比如*.txt)。

-P(--progress):表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。

-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。

-z:加上该选项,将会在传输过程中压缩。

常用选项示例演示

首先创建需要演示的文件;

[root@ask-02 ~]# mkdir rsync-Z
[root@ask-02 ~]# cd rsync-Z/
[root@ask-02 rsync-Z]# mkdir test1
[root@ask-02 rsync-Z]# cd test1/
[root@ask-02 test1]# touch 1 2 3 /root/123.txt
[root@ask-02 test1]# ln -s /root/123.txt ./123.txt
[root@ask-02 test1]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3

使用-av选项;

[root@ask-02 rsync-Z]# rsync -av test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt -> /root/123.txt
2
3

sent 209 bytes received 75 bytes 568.00 bytes/sec
total size is 13 speedup is 0.05
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
总用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3

以上示例中我给目录后都加了斜杠,这是因为如果你不加斜杠,命令会自动创建一个test2目录,然后将test1目录放进test2中去,加了斜杠便解决这个问题,所以以后要养成加斜杠的习惯。

使用-L选项;

[root@ask-02 rsync-Z]# rm -rf /tmp/test2
[root@ask-02 rsync-Z]# rsync -avL test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt
2
3

sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
总用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
-rw-r--r--. 1 root root 0 1月 30 16:33 123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3

上列加上-L选项便把SRC中软连接的目标文件复制到DST

使用--delete选项;

[root@ask-02 rsync-Z]# touch /tmp/test2/new.txt
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 123.txt 2 3 new.txt
[root@ask-02 rsync-Z]# rsync -avL --delete test1/ /tmp/test2/
sending incremental file list
./
deleting new.txt

sent 67 bytes received 15 bytes 164.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls !$
ls /tmp/test2/
1 123.txt 2 3

如果在DST增加文件,而SRC当中没有这些文件,同步时加上--delete选项后就会删除新增的文件

使用--exclude选项;

[root@ask-02 rsync-Z]# touch test1/4
[root@ask-02 rsync-Z]# ls test1/
1 123.txt 2 3 4
[root@ask-02 rsync-Z]# rsync -avL --exclude="4" test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3

sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls test2
1 123.txt 2 3**

该选项还支持匹配字符(*)使用;

[root@ask-02 rsync-Z]# ls test1
1 123.txt 2 3 345.txt 4 567.txt
[root@ask-02 rsync-Z]# rsync -avL --exclude="*.txt" test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
2
3
4

sent 221 bytes received 91 bytes 624.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 2 3 4

使用-P(--progress)选项;

[root@ask-02 rsync-Z]# rm -rf /tmp/test2/
[root@ask-02 rsync-Z]# rsync -avP test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
123.txt -> /root/123.txt
2
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=4/8)
3
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
345.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=2/8)
4
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=1/8)
567.txt
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8)

sent 372 bytes received 132 bytes 1008.00 bytes/sec
total size is 13 speedup is 0.03

加上-P观察同步过程状态。

使用-u选项;

[root@ask-02 rsync-Z]# vim /tmp/test2/567.txt
[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world
[root@ask-02 rsync-Z]# rsync -avPu test1/ /tmp/test2/
sending incremental file list
./

sent 119 bytes received 15 bytes 268.00 bytes/sec
total size is 14 speedup is 0.10
[root@ask-02 rsync-Z]# cat test1/567.txt

[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world

加上-u选项后,可以看到目标文件的心内容不会被源目录文件的覆盖。

rsync通过ssh同步

前面介绍到的rsync5中命令格式中,第二种和三种(使用一个冒号)就属于通过ssh的方式备份数据,这种方式其实就是让用户登录到远程服务器上执行rsync的任务。

[root@ask-02 rsync-Z]# rsync -av /etc/passwd 192.168.74.128:/tmp/test1.txt
root@192.168.74.128's password:
sending incremental file list
passwd

sent 1428 bytes received 31 bytes 224.46 bytes/sec
total size is 1354 speedup is 0.93

如果你的另外一台服务器还没有安装rsync工具需要先进行安装

[root@ask-01 ~]# yum install -y rsync

这时你一个cat以下目标服务器上/tmp/下的test1.txt文件,这里名字是自定义的,你可以命名为你熟知的名字

[root@ask-01 ~]# cat /tmp/test1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

当然还可以从远程将文件发送到本机上

[root@ask-02 rsync-Z]# rsync -avP 192.168.74.128:/tmp/test1.txt /tmp/cs1.txt
root@192.168.74.128's password:
receiving incremental file list
test1.txt
1354 100% 1.29MB/s 0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes received 1436 bytes 101.10 bytes/sec
total size is 1354 speedup is 0.92

同样的你可以cat以下本机/tmp/目录下的cs1.txt文件检查一下。
如果你的另外一台服务器端口并不是22,那么你可以加个选项来操作

[root@ask-02 rsync-Z]# rsync -avP -e "ssh -p 22" /etc/passwd/ 192.168.74.128:/tmp/cs1.txt

加上-e选项后面跟远程命令设定端口。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rsync 介绍 通过