一.简介
Remote Sync 远程同步,unix系统下数据镜像备份工具,一款快速增量备份工具,远程同步,支持本地复制,或者与其他ssh rsync主机同步。
二.特性
可以保存正个目录树和文件系统。
可以很容易做到保持原来文件的权限,时间和软硬连接等。
无需特殊权限即可安装。
快速:第一次同步时,rsync会复制全部内容,但在第一次只传输修改过的文件,rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
安全:可以使用scp ssh等方式传输文件,也可以使用socket连接。
支持匿名传输,方便进行网站镜像。
三.下载安装
#wget https://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz
# mkdir /usr/local/rsync -pv
# ./configure –prefix=/usr/local/rsync ;make ;make install
也可以使用yum安装:
#yum install rsync
注意:必须在服务器A和服务器B上都安装rsync,其中A服务器模式运行rsync,而B上则以客户端方式运行rcync,这样web服务器A上运行守护进程,在B上定时运行客户端程序来备份web服务器A上需要备份的内容。
四.配置rsync的服务端
Rsyncs主要有三个配置文件rsyncd.conf(主配置文件),rsyncd.secrets(密码文件),rsyncd.motd(rsync服务器信息)
服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,需要手动创建。
1)具体步骤如下:
# mkdir /etc/rsyncd 创建配置目录
# touch /etc/rsyncd/rsyncd.conf 创建主配置文件
#ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
# touch /etc/rsyncd/rsyncd.secrets 创建用户密码文件
# chmod 600 /etc/rsyncd/rsyncd.secrets 为了提高密码文件的安全性,建议把文件的权限改成600
# touch /etc/rsyncd/rsyncd.motd 创建定义服务器信息的文件
2)编辑主配置文件:# vim /etc/rsyncd/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 5
secrets file = /etc/rsyncd.secrets
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
commen= hello world
[web]
path = /www/
auth users = test
read only = no
hosts allow = 192.168.100.0/24
hosts deny = *
list = true
ignore errors
以下案例是完整配置[仅供参考:]
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873 ##监听端口
address = 192.168.100.10 ##监听地址
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes ##是否限制在指定目录,为了安装,一般需要启用
read only = no
#limit access to private LANs
hosts allow=192.168.100.0/255.255.255.0 ##允许网段
hosts deny=*
max connections = 5
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
#log file = /var/log/rsync.log
#This will log every file transferred – up to 85,000+ per user, per sync
#transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
##定义一个同步目录
[webhome]
path = /var/www/liangqi
list=yes
ignore errors
auth users = liangqi
secrets file = /etc/rsyncd/rsyncd.secrets ##指定上述账号密码文件
comment = web home
exclude = data/ ##排除目录
3)编辑密码认证文件: vim /etc/rsyncd.secrets
test:123
# chmod 600 /etc/rsyncd.secrets
注意:这里的帐号虽是上面写的,跟系统帐号无关。
启动rsync
# rsync –daemon –config=/etc/rsyncd/rsyncd.conf
设置为开机启动:
#echo “/usr/bin/rsync –daemon –config=/etc/rsyncd/rsyncd.conf” >>/etc/rc.d/rc.local
Tip:这里的启动方式比较也别,如果需要重启,需要kill掉rsync的进程,再重新运行。
五.配置rsync的客户端
在客户端安装方式和服务端一直,只需额外配置一个密码文件。具体如下:
# vim /etc/rsyncd.pass
123 此处为test的密码。
# chmod 600 /etc/rsyncd/rsyncd.pass
在客户端同步:
# rsync -avx –password-file=/etc/rsyncd.pass [email protected]::web /www说明:
–delete:本地与服务器完全一样,如果本地存在不一样的,则删除,慎用
–password-file:指定密码文件,如果不指定,则需手动输入
-a :参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z :传输时压缩;
-P :传输进度;
-v :传输时的进度等信息
[email protected] ::webhome 用双冒号引用主配置文件中的定义资源,也可以使用单引号跟绝对路径
以后同步时,只会同步新增内容。最后把上述命令加入到计划任务即可。