您的位置:首页 > 运维架构 > Linux

linux下rsync定时同步搭建

2016-02-19 17:39 573 查看
==============================================

rsync同步教程教程

===============================================
一、环境
需要备份文件的服务器(服务器端)  :172.16.13.6(RHEL 5.4)
接收备份文件的服务器(客户端)    :172.16.13.5(RHEL 5.4)

同步目录

同步频率 10分钟/次

二、安装配置

1.服务器端的配置

检查是否安装了rsync服务
#rpm -q rsync
rsync-2.6.3-1

如果输出上面信息,表明已经安装了此包

如果系统没安装该服务,需要去下载安装,自行寻找资源下载,然后进入存放资源路径下,安装
#tar -zxvf rsync-3.0.8.tar.gz
#cd rsync-3.0.8
#./configure --prefix=/usr/local/rsync
#make
#make install

A、采用系统默认安装的rsync 编辑/etc/rsyncd.conf文件,如果没有则新建一个。
#vi /etc/rsyncd.conf

编辑内容如下:
#[globale]
uid=root
gid=root
use chroot
strict modes= yes  #check passwd file
port= 873 #default port
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsync.lock
max connections= 4
#[modules]
[tomcatonline]   #备份模块1
path= /opt/tomcat7_online_8080/webapps  #要备份的目录
comment=rsync service is ok
read only= no
host allow= *
auth users=alan
secrets file=/etc/rsyncd.scrt
#[modules1]
[tomcatonline_zte]   #备份模块2
path= /opt/tomcat7_online_8080/webapps/zte  #要备份的目录
comment=rsync service is ok
read only= no
host allow= *
auth users=alan
secrets file=/etc/rsyncd.scrt
#[modules2]
[tomcatonline_hw]   #备份模块3
path= /opt/tomcat7_online_8080/webapps/hw  #要备份的目录
comment=rsync service is ok
read only= no
host allow= *
auth users=alan
secrets file=/etc/rsyncd.scrt

B、添加一个密码文件
#vi /etc/rsyncd.scrt
内容如下:
alan:9876543  #(服务器端设置 格式:账号:密码  客户端:直接 密码)

C、改变权限为600
#chown root.root /etc/rsyncd.scrt
#chmod 600 /etc/rsyncd.scrt

启动服务(如开有防火墙请允许873端口通过,或者关闭防火墙)
#rsync --daemon --config=/etc/rsyncd.conf &

检查rsync是否启动
#lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME

D、配置rsync servervi
#vi /etc/xinetd.d/rsync

将disable=yes改为no

service rsync
{
disable = no
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}

E、配置rsync自动启动
#chkconfig rsync on
#chkconfig rsync --list
rsync           on

F、xinetd启动(重启)
#/etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

G、服务端配置完成,继续客户端的配置吧。

==========================================

客户端配置

===========================================

A、添加密码文件
#vi /etc/rsyncd.scrt (没有就新建)
内容如下:
9876543 (注意这里只要写密码即可)

B、改文件权限为600
#chmod 600 /etc/rsyncd.scrt

C、测试手动同步
#rsync -auzv --progress --password-file=/etc/rsyncd.scrt lutong@172.16.13.6::tomcatonline /opt/sendtest

说明:
命令:rsync -avz --password-file=密码文件路径 username@需要备份的主机IP::备

份里的模块名称 接收备份文件的路径

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性

-v, --verbose 详细模式输出

-z, --compress 对备份的文件在传输时进行压缩处理

D、建立自动每10分钟执行执行备份同步
在/root建一个脚本文件,如果不存在这个文件,会自动新建
#vi rsync

编辑内容:(完全覆盖同步更新)
rsync -azv --progress --delete --password-file=/etc/rsyncd.scrt lutong@172.16.13.6::tomcatonline /opt/tomcat7_online_8080/webapps

备注:客户端不要主动该文件,主服务器文件不改动,客户端即使改了文件,是不更新的

E、然后,修改权限,
#cd /root
#chmod u+x rsync

F、创建10分钟执行任务
#vi /etc/crontab

编辑内容:
#rsync
*/10 * * * * root /root/rsync

G、重启crontab服务
#service crond restart

H、如果需要多任务执行,请重复D步骤下来,创建/root/rsync1、/root/rsync2、/root/rsync3等文件,然后添加任务

I、客户端配置完成,查证是否10分钟自动同步
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: