数据同步工具rsync

rsync应用实例

一. 经过ssh的方式linux

 

前面介绍的rsync 5种方式当中,第2、第三(1个冒号)就属于经过ssh的方式,这种方式其实就是让用户去登陆到远程机器,而后执行rsync的任务。vim

 

[root@localhost rsync]# rsync -avL test1/ www@192.168.0.101:/tmp/test2/安全

www@192.168.0.101's password:服务器

sending incremental file listssh

created directory /tmp/test2tcp

./ide

1工具

1.txt测试

2ui

2.txt

3

4

 

sent 327 bytes  received 129 bytes  182.40 bytes/sec

total size is 0  speedup is 0.00

 

这种方式就是前面介绍的第二种方式了,是经过ssh拷贝的数据,须要输入192.168.0.101 那台机器www 帐户的密码。固然也可使用第三种方式拷贝:

 

[root@localhost rsync]# rsync -avL www@192.168.0.101:/tmp/test2/ ./test3/

www@192.168.0.101's password:

receiving incremental file list

created directory ./test3

./

1

1.txt

2

2.txt

3

4

 

sent 128 bytes  received 351 bytes  38.32 bytes/sec

total size is 0  speedup is 0.00

 

以上两种方式若是写到脚本里,备份起来就有麻烦了,由于要输入密码,脚本原本就是自动的,不可能作到的。可是不表明没有解决办法。

 

那就是经过密钥验证,密钥不设立密码就ok了。还记得在前面阿铭曾经介绍过经过密钥登陆远程主机吗,下面要讲的内容就是那些东西了。

 

在操做以前咱们先讲明主机信息: 192.168.0.10 (主机名Aming-1)和 192.168.0.101 (主机名Aming)须要从Aming-1上拷贝数据到Aming上。

 

首先确认一下Aming-1上是否有这个文件 /root/.ssh/id_rsa.pub:

 

[root@Aming-1 ~]# ssh-keygen

Generating public/private rsa key pair.

 

阿铭以前生成过密钥对,因此这个文件已经存在了,若是你的Linux不存在这个文件,请按照以下方法生成:

 

[root@Aming-1 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

3b:74:af:e8:08:ac:99:30:3f:ef:84:7a:a0:a6:3d:89 root@Aming-1

 

在这个过程当中会有一些交互的过程,它首先提示要输入这个密钥的密码,出于安全考虑应该定义个密码,可是咱们的目的就是为了自动化同步数据。

 

因此这里不输入任何密码,直接按回车,即密码为空。最后则生成了私钥(/root/.ssh/id_rsa)和公钥文件(/root/.ssh/id_rsa.pub)

 

把公钥文件的内容拷贝到目标机器上:

 

[root@Aming-1 ~]# cat .ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5SPyJ/kliGTAMUan/GCN325VS8jMxvOn4uQoLU/NqBpCI3MrmvSucv6EAzxx1J2uOssW08el06LG+cUwXmm5mkqDRBV6C9qNnR/bVV5vr3QsUwbKPr7fdyJvruQWWR7cSL+mjP0SYmG2Qy2JcM3hl1IZArzC6yeUnq2Gwbax8LgbZE3XfRfOYdimwyh5Tfft7yLYipWc37k+oRUWkI3mW7PalsOlfQhxrLD/lS891y6RdSbGxMJWPoV0KMFbVh+uJgyAXpeuWl+F+/iuQPzb6w3h4pWI31bvbsE9BU82jSzHYEjpq3SN2MJN2vaLs5a0mVpm9zka/h4ITFB8Uy1iSQ== root@Aming-1

 

复制主机Aming-1的/root/.ssh/id_rsa.pub文件内容,并粘贴到主机Aming的/home/www/.ssh/authorized_keys中:

 

[root@Aming ~]# vim /home/www/.ssh/authorized_keys

 

在这一步也许你会遇到/home/www/.ssh目录不存在的问题,能够手动建立,并修改目录权限为700也能够执行ssh-keygen命令生成这个目录。

 

保存/home/www/.ssh/authorized_keys文件后,再到主机Aming-1上执行:

 

[root@Aming-1 ~]# ssh www@192.168.0.101

Last login: Wed Jun 12 12:24:34 2013 from 192.168.0.10

[www@Aming ~]$

 

如今不用输入密码也能够登陆主机Aming了。下面先从Aming主机退出来,再从主机Aming-1上执行一下rsync命令试试吧。

 

[root@Aming-1 ~]# rsync -av rsync/test1/ www@192.168.0.101:/tmp/test4/

sending incremental file list

created directory /tmp/test4

./

1

1.txt

2

2.txt

3

4

 

sent 327 bytes  received 129 bytes  912.00 bytes/sec

total size is 0  speedup is 0.00

 

二. 经过后台服务的方式

 

这种方式能够理解成这样,在远程主机上创建一个rsync的服务器,在服务器上配置好rsync的各类应用,而后本机做为rsync的一个客户端去链接远程的rsync服务器。下面阿铭就介绍一下,如何去配置一台rsync服务器。

 

创建并配置rsync的配置文件 /etc/rsyncd.conf

 

[root@Aming-1 ~]# vim /etc/rsyncd.conf

#port=873

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

#address=192.168.0.10

 

[test]

path=/root/rsync

use chroot=true

max connections=4

read only=no

list=true

uid=root

gid=root

auth users=test

secrets file=/etc/rsyncd.passwd

hosts allow=192.168.0.101

 

其中配置文件分为两部分:所有配置部分和模块配置部分,全局部分就是几个参数而已,就像阿铭的rsyncd.conf中port, log file, pid file, address这些都属于全局配置,而[test]如下部分就是模块配置部分了。

 

一个配置文件中能够有多个模块,模块名自定义,格式就像阿铭的rsyncd.conf中的这样。其实模块中的一些参数例如use chroot, max connections, udi, gid, auth users, secrets file以及hosts allow均可以配置成全局的参数。

 

固然阿铭给出的参数并非全部的,你能够经过man rsyncd.conf 得到更多信息。下面就简单解释一下这些参数的意义:

 

port 指定在哪一个端口启动rsyncd服务,默认是873

 

log file 指定日志文件

 

pid file 指定pid文件,这个文件的做用涉及到服务的启动以及中止等进程管理操做

 

address 指定启动rsyncd服务的IP,假如你的机器有多个IP,就能够指定其中一个启动rsyncd服务,默认是在所有IP上启动

 

[test] 指定模块名,自定义

 

path 指定数据存放的路径

 

use chroot true|false 默认是true,意思是在传输文件之前首先chroot到path参数所指定的目录下。这样作的缘由是实现额外的安全防御,可是缺点是须要以roots权限,而且不能备份指向外部的符号链接所指向的目录文件。

 

默认状况下chroot值为true,若是你的数据当中有软链接文件的话建议设置成false。

 

max connections 指定最大的链接数,默认是0即没有限制

 

read only ture|false 若是为true则不能上传到该模块指定的路径下

 

list 指定当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏

 

uid/gid 指定传输文件时,以哪一个用户/组的身份传输

 

auth users 指定传输时要使用的用户名

 

secrets file 指定密码文件,该参数连同上面的参数若是不指定则不使用密码验证,注意该密码文件的权限必定要是600

 

hosts allow 指定被容许链接该模块的主机,能够是IP或者网段,若是是多个,之间用空格隔开

 

编辑secrets file,保存后要赋予600权限,若是权限不对,不能完成同步

 

[root@Aming-1 ~]# cat /etc/rsyncd.passwd

test:test123

[root@Aming-1 ~]# chmod 600 /etc/rsyncd.passwd

启动rsyncd服务

[root@Aming-1 ~]# rsync --daemon --config=/etc/rsyncd.conf

 

启动后,能够查看一下日志,并查看端口是否启动:

 

[root@Aming-1 ~]# cat /var/log/rsync.log

[root@Aming-1 ~]# netstat -lnp |grep 873

tcp     0    0 0.0.0.0:873        0.0.0.0:*  LISTEN      12066/rsync

tcp     0    0 :::873             :::*       LISTEN      12066/rsync

 

若是想开机启动,请把 rsync --daemon --confg=/etc/rsyncd.conf 写入到/etc/rc.d/rc.local文件。

 

到另外一台机器上测试

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test5/

Password:

receiving incremental file list

created directory /tmp/test5

./

1

1.txt

2

2.txt

3

4

 

sent 143 bytes  received 354 bytes  994.00 bytes/sec

total size is 0  speedup is 0.00

 

阿铭刚刚提到有一个选项叫作 “use chroot” 默认为true,若是是true,同步的文件中若是有软链接,则会有问题,首先在主机Aming-1的/root/rsync/test1/ 目录下建立一个软链接文件:

 

[root@Aming-1 ~]# ln -s /root/test.txt rsync/test1/test.txt

[root@Aming-1 ~]# ls -l rsync/test1/test.txt

lrwxrwxrwx 1 root root 14  6月 12 13:24 rsync/test1/test.txt -> /root/test.txt

 

而后再到主机Aming上,同步:

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test6/

Password:

receiving incremental file list

symlink has no referent: "/test1/test.txt" (in test)

created directory /tmp/test6

./

1

1.txt

2

2.txt

3

4

 

sent 143 bytes  received 419 bytes  1124.00 bytes/sec

total size is 0  speedup is 0.00

rsync error: some files/attrs were not transferred (see previous errors) (code

23) at main.c(1532) [generator=3.0.6]

 

能够看到,若是设置 “use chroot” 为true则同步软链接文件会有问题,下面阿铭把主机Aming-1的rsync配置文件修改一下,把true改成false:

 

[root@Aming-1 ~]# sed -i 's/use chroot=true/use chroot=false/'  /etc/rsyncd.conf

[root@Aming-1 ~]# grep 'use chroot' /etc/rsyncd.conf

use chroot=false

 

而后再到主机Aming上再次执行同步:

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test7/

Password:

receiving incremental file list

created directory /tmp/test7

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 162 bytes  received 410 bytes  1144.00 bytes/sec

total size is 0  speedup is 0.00

 

这样就没有任何问题啦,你也许会奇怪,为何阿铭修改完rsyncd.conf配置文件后,没有重启rsyncd服务呢?其实这是rsync的一个特定机制,配置文件时即时生效的,不用重启服务。

 

上面的例子中,阿铭都有输入密码,这样一样也不能写入脚本中自动执行,其实这种方式也是能够不用手动输入密码的,它有两种实现方式。

 

第一种,指定密码文件

 

在客户端上,也就是主机Aming上,编辑一个密码文件:

 

[root@Aming ~]# vim /etc/pass

 

加入test用户的密码:

 

[root@Aming ~]# cat /etc/pass

test123

 

修改密码文件的权限:

 

[root@Aming ~]# chmod 600 /etc/pass

 

在同步的时候,指定一下密码文件,就能够省去输入密码的步骤了:

 

[root@Aming ~]# rsync -avL test@192.168.0.10::test/test1/ /tmp/test8/ --password-file=/etc/pass

receiving incremental file list

created directory /tmp/test8

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 190 bytes  received 451 bytes  1282.00 bytes/sec

total size is 0  speedup is 0.00

 

第二种:在rsync服务器端不指定用户

 

在服务端也就是主机Aming-1上修改配置文件rsyncd.conf, 去掉关于认证帐户的配置项(auth user 和 secrets file这两行):

 

sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf

上面的这个命令是把 “auth users” 和 “secrets file” 两行的最前面加一个 “#”, 这样就把这两行注释掉,使其失去意义。

 

在前面阿铭不曾讲过sed的这种用法,其实也不难弄明白,只是用分号把两个替换的子命令块给替换了而已。而后咱们再到客户端主机Aming上测试:

 

[root@Aming ~]# rsync -avL 192.168.0.10::test/test1/ /tmp/test9/

receiving incremental file list

created directory /tmp/test9

./

1

1.txt

2

2.txt

3

4

test.txt

 

sent 162 bytes  received 410 bytes  1144.00 bytes/sec

total size is 0  speedup is 0.00

 

注意,这里不用再加test这个用户了,默认是以root的身份拷贝的,如今已经不须要输入密码了。

 

数据同步工具rsync介绍(1)

数据备份,毫无疑问很重要。阿铭就曾经有过一次很是痛苦的经历,备份策略没有作好,结果磁盘坏掉数据丢失,简直是撕心裂肺的痛呀。还好数据重要性不是特别高,即便是不高也是丢失了数据,这是做为系统管理员最不该该出现的事故。

 

因此,在你之后的系统维护工做中,必定要把数据备份当回事,认真对待。在linux系统下数据备份的工具不少,但阿铭就只用一种那就是rsync. 从字面上的意思你能够理解为remote sync (远程同步)这样可让你理解的更深入一些。

 

Rsync不只能够远程同步数据(相似于scp [1]),固然还能够本地同步数据(相似于cp),但不一样于cp或scp的一点是,rsync不像cp/scp同样会覆盖之前的数据(若是数据已经存在),它会先判断已经存在的数据和新数据有什么不一样,只有不一样时才会把不一样的部分覆盖掉。若是你的linux没有rsync命令请使用 yum install -y rsync 安装。

 

下面阿铭先举一个例子,而后再详细讲解rsync的用法:

 

[root@localhost ~]# rsync -av 123.txt /tmp/

 sending incremental file list

 123.txt

 

 sent 71 bytes  received 31 bytes  204.00 bytes/sec

 total size is 0  speedup is 0.00

 

上面例子表示把当前目录下的123.txt同步到/tmp/目录下,也能够更改目标文件的名字, rsync -av 123.txt /tmp/234.txt 若是是远程拷贝的话就是这样的形式了: IP:path (如:10.0.2.34:/root/)

 

[root@localhost ~]# rsync -av 123.txt 192.168.0.101:/data/

The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.

RSA key fingerprint is b4:54:5f:73:ec:c2:60:5f:c3:79:c0:f9:51:e9:ac:e5.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.0.101' (RSA) to the list of known hosts.

root@192.168.0.101's password:

 

首次链接会提示是否要继续链接,咱们输入yes继续,当创建链接后,须要输入密码。若是手动去执行这些操做还好,但如果写在脚本中怎么办?这就涉及到添加信任关系了。

 

1. rsync的命令格式

 

rsync [OPTION]... SRC DEST

 

rsync [OPTION]... SRC [USER@]HOST:DEST

 

rsync [OPTION]... [USER@]HOST:SRC DEST

 

rsync [OPTION]... [USER@]HOST::SRC DEST

 

rsync [OPTION]... SRC [USER@]HOST::DEST

 

在一开始举的两个例子,第一个例子即为第一种格式,第二个例子即为第二种格式,但不一样的是,阿铭并无加user@host 若是不加默认指的是root。

 

第三种格式是从远程目录同步数据到本地。第四种以及第五种格式使用了两个冒号,这种方式和前面的方式的不一样在于验证方式不一样,稍后详细介绍。

 

2. rsync经常使用选项

 

-a 归档模式,表示以递归方式传输文件,并保持全部属性,等同于-rlptgoD, -a选项后面能够跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个例如 -a--no-l 等同于-rptgoD

 

-r 对子目录以递归模式处理,主要是针对目录来讲的,若是单独传一个文件不须要加-r,可是传输的是目录必须加-r选项

 

-v 打印一些信息出来,好比速率,文件数量等

 

-l 保留软链结

 

-L 向对待常规文件同样处理软链结,若是是SRC中有软链接文件,则加上该选项后将会把软链接指向的目标文件拷贝到DST

 

-p 保持文件权限

 

-o 保持文件属主信息

 

-g 保持文件属组信息

 

-D 保持设备文件信息

 

-t 保持文件时间信息

 

--delete 删除那些DST中SRC没有的文件

 

--exclude=PATTERN 指定排除不须要传输的文件,等号后面跟文件名,能够是万用字符模式(如*.txt)

 

--progress 在同步的过程当中能够看到同步的过程状态,好比统计要同步的文件数量、同步的文件传输速度等等

 

-u 加上这个选项后将会把DST中比SRC还新的文件排除掉,不会覆盖

 

选项确实有点多,不过不用担忧,阿铭工做这么多年,经常使用的选项页仅仅那么几个: (-a -v --delete --exclude ), 请熟记他们吧。

 

数据同步工具rsync介绍(2)

1) 创建目录以及文件:

 

[root@localhost ~]# mkdir rsync

[root@localhost ~]# cd rsync

[root@localhost rsync]# mkdir test1

[root@localhost rsync]# cd test1

[root@localhost test1]# touch 1 2 3

[root@localhost test1]# ln -s /root/123.txt ./123.txt

[root@localhost test1]# ls -l

总用量 0

-rw-r--r-- 1 root root  0  6月 10 12:58 1

lrwxrwxrwx 1 root root 13  6月 10 12:59 123.txt -> /root/123.txt

-rw-r--r-- 1 root root  0  6月 10 12:58 2

-rw-r--r-- 1 root root  0  6月 10 12:58 3

[root@localhost test1]# cd ..

 

阿铭创建这些文件的目的就是为作试验作一些准备工做。

 

2)使用 -a 选项

 

[root@localhost rsync]# rsync -a test1 test2

[root@localhost rsync]# ls test2

test1

[root@localhost rsync]# ls test2/test1/

1  123.txt  2  3

 

这里有一个问题,就是原本想把test1目录直接拷贝成test2目录,可结果rsync却新建了test2目录而后把test1放到test2当中。为了不这样的状况发生,能够这样作:

 

[root@localhost rsync]# rm -rf test2

[root@localhost rsync]# rsync -a test1/ test2/

[root@localhost rsync]# ls -l test2/

总用量 0

-rw-r--r-- 1 root root  0  6月 10 12:58 1

lrwxrwxrwx 1 root root 13  6月 10 12:59 123.txt -> /root/123.txt

-rw-r--r-- 1 root root  0  6月 10 12:58 2

-rw-r--r-- 1 root root  0  6月 10 12:58 3

 

加一个斜杠就行了,因此阿铭建议你在使用rsync备份目录时要养成加斜杠的习惯。在上面讲了-a选项等同于-rlptgoD,并且 -a 还能够和 --no-OPTIN 一并使用。下面看看-l选项的做用:

 

[root@localhost rsync]# rsync -av --no-l test1/ test2/

sending incremental file list

created directory test2

./

1

skipping non-regular file "123.txt"

2

3

 

sent 200 bytes  received 72 bytes  544.00 bytes/sec

total size is 13  speedup is 0.05

 

使用-v选项看来就是方便,上例告诉咱们跳过了非普通文件123.txt,其实123.txt是一个软链接文件,若是不使用-l选项则不理会软链接文件的。

 

虽然加上-l选项会把软链接文件给拷贝过去,可是软链接的目标文件却没有拷贝过去,有时候我们指向拷贝软链接文件所指向的目标文件,那这时候该怎么办呢?

 

3)使用-L选项

 

[root@localhost rsync]# rsync -avL test1/ test2/

sending incremental file list

created directory test2

./

1

123.txt

2

3

 

sent 231 bytes  received 91 bytes  644.00 bytes/sec

total size is 0  speedup is 0.00

[root@localhost rsync]# ls -l test2/

总用量 0

-rw-r--r-- 1 root root 0  6月 10 12:58 1

-rw-r--r-- 1 root root 0  6月 10 12:39 123.txt

-rw-r--r-- 1 root root 0  6月 10 12:58 2

-rw-r--r-- 1 root root 0  6月 10 12:58 3

 

加上 -L 选项就能够把SRC中软链接的目标文件给拷贝到DST.

 

4) 使用-u选项

 

首先查看一下test1/1 和test2/1的建立时间(确定是同样的),而后使用touch修改一下test2/1的建立时间(此时test2/1要比test1/1的建立时间晚了一些)。

 

若是不加-u选项的话,会把test2/1的建立时间变成和test1/1的建立时间同样。这样讲也许你会迷糊,不妨看一看:

 

[root@localhost rsync]# ll test1/1 test2/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test1/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test2/1

 

二者之间的建立时间是同样的,下面修改test2/1 的建立时间,而后不加-u同步:

 

[root@localhost rsync]# touch test2/1

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:20 test2/1

[root@localhost rsync]# rsync -a test1/1 test2/

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test2/1

 

test2/1 的建立时间又变成和test1/1的建立时间同样了。下面加上 -u 再看看结果是怎么样的:

 

[root@localhost rsync]# touch test2/1

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:31 test2/1

[root@localhost rsync]# rsync -avu test1/ test2/

sending incremental file list

./

123.txt -> /root/123.txt

 

sent 100 bytes  received 18 bytes  236.00 bytes/sec

total size is 13  speedup is 0.11

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 0  6月 10 13:31 test2/1

[root@localhost rsync]# ll test1/1

-rw-r--r-- 1 root root 0  6月 10 12:58 test1/1

 

加上-u 选项后,不会再把 test1/1 同步为 test2/1 了,如今你明白 -u 选项的妙用了吧。

 

5)使用 --delete 选项

 

首先删除test1/123.txt:

 

[root@localhost rsync]# rm -f test1/123.txt

[root@localhost rsync]# ls test1/

1  2  3

 

而后把test1/ 目录 同步到 test2/ 目录下:

 

[root@localhost rsync]# rsync -av test1/ test2/

sending incremental file list

./

1

 

sent 94 bytes  received 34 bytes  256.00 bytes/sec

total size is 0  speedup is 0.00

[root@localhost rsync]# ls test2/

1  123.txt  2  3

 

test2/目录并无删除掉123.txt, 下面加上 --delete 选项:

 

[root@localhost rsync]# rsync -av --delete test1/ test2/

sending incremental file list

deleting 123.txt

 

sent 52 bytes  received 12 bytes  128.00 bytes/sec

total size is 0  speedup is 0.00

[root@localhost rsync]# ls test2/

1  2  3

 

test2/ 目录里的123.txt也被删除了,这就是 --delete 选项的用处。还有一种状况就是若是在DST增长文件了,而SRC当中没有这些文件,同步时加上 --delete 选项后一样会删除新增的文件:

 

[root@localhost rsync]# touch test2/4

[root@localhost rsync]# ls test1/

1  2  3

[root@localhost rsync]# ls test2/

1  2  3  4

[root@localhost rsync]# rsync -a --delete test1/ test2/

[root@localhost rsync]# ls test1/

1  2  3

[root@localhost rsync]# ls test2/

1  2  3

 

6)使用 --exclude 选项

 

[root@localhost rsync]# touch test1/4

[root@localhost rsync]# rsync -a --exclude="4" test1/ test2/

[root@localhost rsync]# ls test1/

1  2  3  4

[root@localhost rsync]# ls test2/

1  2  3

 

另外还可使用匹配字符 *

 

[root@localhost rsync]# touch test1/1.txt test1/2.txt

[root@localhost rsync]# ls test1/

1  1.txt  2  2.txt  3  4

[root@localhost rsync]# rsync -a --progress --exclude="*.txt" test1/ test2/

sending incremental file list

./

4

           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/5)

 

sent 104 bytes  received 34 bytes  276.00 bytes/sec

total size is 0  speedup is 0.00

[root@localhost rsync]# ls test2/

1  2  3  4

 

上例中,阿铭也连带着使用了 --progress 选项,这个主要是用来观察rsync同步过程的状态的。

 

最后简单总结一下,平时你使用rsync同步数据的时候,使用-a选项基本上就能够达到咱们想要的效果了,只是有时候会有个别的需求,会用到 -a --no-OPTION, -u, -L, --delete, --exclude 以及 progress 这些选项。

 

常见问题  : 

1. rsync 同步命令中,下面两种方式有什么不一样呢?

答 : (1) rsync -av /dira/ ip:/dirb/

(2) rsync -av /dira/ ip::dirb

(1)前者是经过ssh方式同步的

(2)后者是经过rsync服务的方式同步的

2.rsync 同步时,若是要同步的源中有软链接,如何把软链接的目标文件或者目录同步?

答 : 同步源文件须要加-L选项

3.rsync 同步数据时,如何过滤出全部.txt的文件不一样步?

加上--exclude选项:--exclude=“*.txt”

 

4.rsync同步数据时,若是目标文件比源文件还新,则忽略该文件,如何作?

保留更新使用-u或者--update选项

 

5.使用rsync同步数据时,假如咱们采用的是ssh方式,而且目标机器的sshd端口并非默认的22端口,那咱们如何作?

rsync "--rsh=ssh -p 10022"或者rsync -e "ssh -p 10022"

6.rsync同步时,如何删除目标数据多出来的数据,即源上不存在,但目标却存在的文件或者目录?

加上--delete选项

 

7.rsync使用服务模式时,若是咱们指定了一个密码文件,那么这个密码文件的权限应该设置成多少才能够?

600或400

8.

 

 

参考连接:

https://mp.weixin.qq.com/s/-aMDm-HGMfsaqF9Mr62yrQ

https://mp.weixin.qq.com/s/zVAPWTWrPuuquPwVmDl8mQ

https://mp.weixin.qq.com/s/2g5at0hjep9xZ8pUbmQ-Ug

相关文章
相关标签/搜索