Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
Rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配置文件。Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。
Rsync支持大多数的类Unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。此外,它在windows平台下也有相应的版本,如cwRsync和Sync2NAS等工具。
一、安装与启动rsync服务
1)、下载:
10.100.10.10(服务器端)
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
wget http://rsync.samba.org/ftp/rsync/src/rsync-patches-3.0.9.tar.gz
tar xvf rsync-3.0.9.tar.gz
tar xvf rsync-patches-3.0.9.tar.gz
yum list rsync
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Installed Packages
rsync.x86_64 3.0.6-9.el6 @base
yum erase –y rsync 卸载系统自带的rsync
cd rsync-3.0.9
./configure --disable-debug --with-rsh=ssh --prefix=/usr/local/rsync
Make && make install
ln -sv /usr/local/rsync/bin/rsync /usr/bin/rsync
ln -sv /usr/local/rsync/bin/rsync /usr/sbin/rsync
2)为rsync提供配置文件/etc/rsyncd.conf,内容类似如下内容:
Vim /etc/rsyncd.conf
# Section 1: Global settings
uid = root
gid = root
use chroot = no
max connections = 3
strict modes = yesi
port = 873
pid file = /var/run/rsync/rsyncd.pid
log file = /var/log/rsync/rsyncd.log
lock file = /var/log/rsync/rsyncd.lock
# Section 2:Directory to be synced
[httpdlog]
path = /var/log/httpd/
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
hosts deny = *
list = false
auth users = loguser
secrets file = /etc/rsync.passwd
[tomcatlogs]
path = /appsdata/apps/tomcat/logs
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
hosts deny = *
list = false
auth users = loguser
secrets file = /etc/rsync.passwd
[nginxlog]
path = /appsdata/apps/nginx/logs
ignore errors = yes
hosts allow = x.x.x.0/255.255.255.0 y.y.0.0/255.255.0.0 允许来同步数据的主机,多个用空格隔开
hosts deny = *
list = false
auth users = loguser
secrets file = /etc/rsync.passwd
vim /etc/rsync.passwd
loguser:loguser123
其中冒号前的是用户名,冒号后的是对应用户的密码。此文件不能为其他任意用户可访问,因此可用如下命令修改:
# chmod 600 /etc/rsync.passwd
3)、启动rsync服务
mkdir -pv /var/run/rsync/ /var/log/rsync /var/lock/rsync
rsync --daemon –config=/etc/rsyncd.conf netstat -tnlp|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 26635/rsync
tcp 0 0 :::873 :::* LISTEN 26635/rsync
