十八、Rsync 远程同步数据

在linux系统下数据备份的工具。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)? yesWarning: Permanently added '192.168.0.101' (RSA) to the list of known [email protected]'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)

实验:

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 1l

rwxrwxrwx 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 1l

rwxrwxrwx 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 listcreated directory test2./1skipping non-regular file "123.txt"23sent 200 bytes  received 72 bytes  544.00 bytes/sectotal 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 listcreated directory test2./1123.txt23sent 231 bytes  received 91 bytes  644.00 bytes/sectotal 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/sectotal 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./1sent 94 bytes  

received 34 bytes  256.00 bytes/sectotal 

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 listdeleting 123.txtsent 52 bytes  received 12 bytes  128.00 bytes/sectotal 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/sectotal 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 这些选项,还有些选项阿铭都没有介绍,如果在以后的工作中遇到特殊需求了,就去查一下rsync的man文档吧。

3. rsync 应用实例

1)通过ssh的方式

最上面介绍的5种方式当中,第二、第三(1个冒号)就属于通过ssh的方式,这种方式其实就是让用户去登录到远程机器,然后执行rsync的任务。

[root@localhost rsync]# rsync -avL test1/ [email protected]:/tmp/test2/

[email protected]'s password:

sending incremental file listcreated directory /tmp/test2./11.txt22.txt34sent 327 bytes  

received 129 bytes  182.40 bytes/sectotal size is 0  

speedup is 0.00

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

[root@localhost rsync]# rsync -avL [email protected]:/tmp/test2/ ./test3/

[email protected]'s password:

receiving incremental file listcreated directory ./test3./11.txt22.txt34sent 128 bytes  

received 351 bytes  38.32 bytes/sectotal 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.pubssh-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 [email protected] 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/ [email protected]:/tmp/test4/sending incremental file listcreated directory /tmp/test4./11.txt22.txt34sent 327 bytes  received 129 bytes  912.00 bytes/sectotal size is 0  speedup is 0.00

2) 通过后台服务的方式

这种方式可以理解成这样,在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后本机作为rsync的一个客户端去连接远程的rsync服务器。下面就介绍一下,如何去配置一台rsync服务器。

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

[root@Aming-1 ~]# vim /etc/rsyncd.conf#port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pid#address=192.168.0.10[test]path=/root/rsyncuse chroot=truemax connections=4read only=nolist=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts 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.passwdtest: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 873tcp     0    0 0.0.0.0:873        0.0.0.0:  LISTEN      12066/rsynctcp     0    0 :::873             :::*       LISTEN      12066/rsync

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

到另一台机器上测试

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test5/Password:receiving incremental file listcreated directory /tmp/test5./11.txt22.txt34sent 143 bytes  received 354 bytes  994.00 bytes/sectotal 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.txtlrwxrwxrwx 1 root root 14  6月 12 13:24 rsync/test1/test.txt -> /root/test.txt

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

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test6/Password:receiving incremental file listsymlink has no referent: "/test1/test.txt" (in test)created directory /tmp/test6./11.txt22.txt34sent 143 bytes  received 419 bytes  1124.00 bytes/sectotal size is 0  speedup is 0.00rsync error: some files/attrs were not transferred (see previous errors) (code23) 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.confuse chroot=false

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

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test7/Password:receiving incremental file listcreated directory /tmp/test7./11.txt22.txt34test.txtsent 162 bytes  received 410 bytes  1144.00 bytes/sectotal size is 0  speedup is 0.00

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

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

第一种,指定密码文件

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

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

加入test用户的密码:

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

修改密码文件的权限:

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

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

[root@Aming ~]# rsync -avL [email protected]::test/test1/ /tmp/test8/ --password-file=/etc/pa***eceiving incremental file listcreated directory /tmp/test8./11.txt22.txt34test.txtsent 190 bytes  received 451 bytes  1282.00 bytes/sectotal 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 listcreated directory /tmp/test9./11.txt22.txt34test.txtsent 162 bytes  received 410 bytes  1144.00 bytes/sectotal size is 0  speedup is 0.00

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


http://ask.apelearn.com/question/5440X298X  拓展


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章