RSYNC 客户端部署
RSYNC 客户端部署起来非常简单,我们仅仅需要安装 rsync 软件,然后创建一个本地密码文件。该密码文件为对应rsync 服务端配置文件里面每个模块下面的虚拟用户的密码,以此来实现免密码同步!
操作如下:
1、安装rsync
[root@rsync-server ~]# yum install rsync -y
2、配置本地密码文件
[root@rsync-server ~]# vim /etc/rsync.password # 该文件里面仅存放密码即可
[root@rsync-server ~]# cat /etc/rsync.password # 配置完毕之后检查
[root@rsync-server ~]# ll /etc/rsync.password # 配置完毕之后检查
OK!!!
RSYNC的服务端和客户端都配置完毕,现在我们可以进行测试了。
我们需要去rsync客户端,来完成下面的测试操作。需要注意,测试同样分为两种情况,每种情况又有两种语法写法,分别为:
拉取:①rsync [OPTION...] [USER@]HOST::SRC... [DEST]
②rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
推送:①rsync [OPTION...] SRC... [USER@]HOST:DEST
②rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
特别值得注意的是,与远程shell模式命令不同的是,第①种语法格式, [USER@]HOST::SRC和 [USER@]HOST::DEST结尾处,均为双冒号连接SRC或DEST。另外,这个SRC或DEST已经不再是路径了,而是守护进程中配置的模块的名称了。
下面列举我们在rsync客户端上,对rsync服务端进行拉取或推送的操作范例:
第一种格式:
拉取:
rsync -avzP rsync_backup@172.16.100.1::sunsky /test/ --password-file=/etc/rsync.password
推送:
rsync -avzP /test rsync_backup@172.16.100.1::sunsky --password-file=/etc/rsync.password
第二种格式:
拉取:
rsync -avzP rsync://rsync_backup@172.16.100.1:/sunsky/ /test/ --password-file=/etc/rsync.password
推送:
rsync -avzP /test/ rsync://resync_backup@192.168.1.111:/sunsky/ --password-file=/etc/rsync.password
以上就是,rsync整个服务端和客户端的配置。
在生产环境中,我们可以专门部署一台rsync服务器,专门负责接收业务服务器的日志归档和配置文件归档。当然,我们也可以部署在svn服务器之前,来作为网站代码推送的服务器,或者说一台web服务器专门用来做分发的时候。
但是我们知道,我们的日志归档都是在凌晨,因此我们不可能天天晚上去做向rsync服务端的推送操作。并且,如果日常web内容做分发的次数频繁的话,我们不可能每时每刻都盯着它去做手动推送。因此我们可以借助crontab计划任务来实现定时推送。
[root@rsync-server ~]# crontab -e
# Automatic synchronization log files at 1 a.m
* 1 * * * /usr/bin/rsync-avzP rsync_nobody@172.16.100.1::weblog /data/log/ --password-file=/etc/rsyncd.passwd > /dev/null 2>&1
# Automatic distribution site file points every second of every day
* * * * * /usr/bin/rsync-avzP --delete rsync_nobody@172.16.100.1::webdata /data/web/ --password-file=/etc/rsyncd.passwd > /dev/null 2>&1
注意:上面用到的weblog和webdata都是模块的名字,我们在rsyncd.conf里面指定模块所对应的目录路径。
但是,我们仍然会发现一个问题,虽然使用计划认为能满足我们的日志归档定时推送到rsync服务端,但是每次都有1分钟的延迟。并且,在某些作为网站发布平台的情况下,rsync每分钟都去检查整体一致性然后做推送,这样子非常耗费资源。因此,我们需要对此种情况进行改良。在后面一篇博文,我会带大家一起了解rsync+inotify,借助rsync+inotify来实现实时同步文件。
在本文最后面,我贴上rsync的--delete和--exclude的操作效果!
--delete操作如下:
[root@rsync-server ~]# ls /test/ #查看rsync服务端,sunsky模块下的/test目录的文件列表
e
[root@rsync-client ~]# ls /test/ #查看rsync客户端本地/test目录的文件列表
a b c d
以上为操作前的环境,请保持这个环境一致,然后做如下操作:(下面的推送和拉取都是基于此环境做的)
在rsync客户端做拉取操作:
[root@rsync-client ~]# rsync -avzP --delete rsync_backup@172.16.100.1::sunsky /test/ --password-file=/etc/rsync.password
receiving incremental file list
deleting d
deleting c
deleting b
deleting a
./
e
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 86 bytes received 143 bytes 458.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-client ~]# ls /test/
e
[root@rsync-server ~]# ls /test/
e
我们发现,rsync客户端本身的a,b,c,d文件都消失了,取而代之的是e文件,并且我们在rsync的拉取过程中,也能看到删除操作!
在rsync客户端做推送操作:
[root@rsync-client ~]# rsync -avzP --delete /test/ rsync_backup@172.16.100.1::sunsky --password-file=/etc/rsync.password
sending incremental file list
./
deleting e
a
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
b
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/5)
c
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=1/5)
d
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=0/5)
sent 201 bytes received 87 bytes 576.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-client ~]# ls /test/
a b c d
[root@rsync-server ~]# ls /test/
a b c d
我们发现,rsync服务端本身的e文件消失了,取而代之的是a,b,c,d文件,并且我们在rsync的推送过程中,也能看到删除操作!
--exclude操作如下:
[root@rsync-server ~]# tree /test/
/test/
0 directories, 0 files
[root@rsync-client ~]# tree /test/
/test/
├── a
├── b
├── c.txt
├── x
│ └── xxx.txt
└── x.txt
2 directories, 4 files
以上为操作前的环境,请保持这个环境一致,然后做如下操作:
在rsync客户端做推送操作:
[root@rsync-client ~]# rsync -avz --exclude=*.txt /test/ root@172.16.100.1:/test/
root@172.16.100.1's password:
sending incremental file list
./
a
b
x/
sent 154 bytes received 57 bytes 60.29 bytes/sec
total size is 0 speedup is 0.00
在rsync服务端进行查看同步效果:
[root@rsync-server ~]# tree /test/
/test/
├── a
├── b
└── x
1 directory, 2 files
我们可以发现,只要是能被*.txt匹配到的都没有被推送过去!
如果说,你有很多需要排除的目录或者文件,不希望他们被同步过去,此时你可以使用--exclude-from参数,该参数后面跟一个文件,你可以将需要匹配排除的项写到该文件里面!
例子:
在rsync客户端做推送操作:
[root@rsync-client ~]# cat /tmp/exclude
x
*.txt
[root@rsync-client ~]# rsync -avz --exclude-from=/tmp/exclude /test/ root@172.16.100.1:/test/
root@172.16.100.1's password:
sending incremental file list
./
a
b
xx/
sent 151 bytes received 57 bytes 59.43 bytes/sec
total size is 0 speedup is 0.00
在rsync服务端进行查看同步效果:
[root@rsync-server ~]# tree /test/
/test/
├── a
├── b
└── xx
1 directory, 2 files
以上就是我们的--exclude和--exclude-from的详细用法!
Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103807.htm
