软件:
CentOS: rsync daemon模式
Windows: cwRsync client端(Free)
方法:
Linux设为服务器端,同时windows作为client端,同步方法为,数据发生改动的一方想让另外一方进行同步。三种情况,windows 向linux同步,linux向windows同步,linux和windows互相同步。
步骤:
1.在Linux端配置daemon模式的rsync服务端并挂载相应的共享目录。
2.在windows客户端运行cwRsync软件,并设置定时任务,和服务器端进行同步。
配置步骤:
服务器端:
1. 建立rsync配置文件目录
# mkdir /etc/rsyncd
2. 创建配置文件
# touch rsyncd.conf
3. rsync.conf内容:
#全局设定
[global section]
1, 规定全局设定,比如socket文件,pass文件等全局性的配置
[rsync_section]
2,规定同步的目录的细节,比如权限,路径等。
配置文件样例:
#Global settings
port = 873
address = 192.168.0.x
#the service bind to the ip address
hosts allow = *
#the ip address that can access the service.
motd file = /etc/rsyncd/motd.txt
#socket options = /var/run/rsyncd.socket
#socket opt is for geek
pid file = /var/log/rsyncd.pid
log file = /var/log/rsyncd.log
#syslog facility = syslog
####Module config######Moudle defines the detail info of the share part.
[backup]
#Setting the file owner and group transfered.
uid = nobody
gid = nobody
#read only can write, wite only can read.
read only = false
#writable
write only = false
#download enable
#List the module
list = true
#Only auth user can access, not need local system user
auth users = rsync
secrets file = /etc/rsyncd.secrets
#password file like : usename:passwd
strict modes = true
#this option is true makes the password file should be read and write only by its owner
ignore errors = true
ignore nonreadable = false
#log in the transfer progress
transfer logging = true
log format = %a-%b-%f-%m-%o-%P-%t-%u
timeout = 600
dont compress = true
path = /backup
use chroot = true
#if use chroot = true, you should run rsync under root privleges
#immentounous connection can not more than 5
max connections = 5
#the lock file functioning the max connections.
lock file = /var/run/rsyslogd.lock
#启动程序
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
#关闭程序
ps -ef |grep rsync
kill -15 pid
#加入到service
新建文件
#vim /etc/init.d/rsyncd
#!/bin/bash
# chkconfig: 345 90 90
# description: running rsync in daemon mode
#author: zzp
#The 2 above config is essential if you want to add it to the chkconfig
rsync_prog="/usr/bin/rsync"
config_file="/etc/rsyncd/rsyncd.conf"
pid_file="/var/run/rsyncd.pid"
cat_prog="/bin/cat"
xargs_prog="/usr/bin/xargs"
RETVAL=0
start() {
if [ -f $pid_file ];then
echo "Service is running..."
else
echo "Starting rsync daemon..."
$rsync_prog --daemon --config=$config_file
RETVAL=$?
fi
# [ -x $sync_prog ] || return 5
# [ -f $config_file ] || return 5
# [ -f $pid_file ] || return 5
return $RETVAL
}
stop() {
echo "Stopping rsync daemon..."
if [ -f $pid_file ];then
$cat_prog $pid_file | $xargs_prog kill -15 && rm -f $pid_file
RETVAL=$?
else
echo "Serivce already down..."
return $RETVAL
fi
}
restart() {
echo "Restarting rsync daemon..."
stop
sleep 4
start
RETVAL=$?
return $RETVAL
}
case $1 in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
restart
RETVAL=$?
;;
*)
echo "Usage: service rsyncd { start|stop|restart}"
;;
esac
exit $RETVAL
#chmod u+x /etc/init.d/rsyncd
#开机自启动
echo "/usr/bin/rsync –daemon –config=/etc/rsyncd/rsyncd.conf" >> /etc/rc.d/rc.local
或
#chkconfig –add rsyncd
客户端配置
客户端有两种,一种为Linux,一种为windows。Linux采用自带rsync,Windows采用cwRsync(client)端。
下面分别说明
1. Linux
方法:写一个执行脚本,在crontab中每隔一段时间进行执行,
1. 手动执行
# rsync -av --progress rsync@192.168.0.x::backup ./test/
2. 含有密码文件的rsync
# rsync -av --progress --password-file=test.pass rsync@192.168.0.x::backup ./test/
#echo ‘xxxxxx’>/etc/syncd/rsyncd.pass && chmod 600 /etc/rsyncd/rsyncd.pass
3. 执行脚本的rsync
#!/bin/bash
rsync_prog="/usr/bin/rsync"
$rsync_prog -a --password-file=/root/test.pass rsync@192.168.0.x::backup /root/test/ 2>&1>/dev/null
#echo ‘xxxxxx’>/root/test.pass
Windows下rsync客户端,
下载cwRsync客户端(Free)
然后运行定时任务
配置方法:
安装cwRsync并配置环境:
1. 将cwRsync的bin目录设置到path环境变量中。
2. 配置密码文件,并写入密码,设置此密码文件的权限。
文件的拥有者具有读写权限。
建立执行脚本:
rsync -av --progress --password-file=/cygdrive/c/passfile rsync@192.168.0.x::backup /cygdrive/c/backup
设置定时任务:
控制面板->管理工具->任务计划程序
新建任务
建立触发器
建立动作
总结:
此种解决方案主要处理Windows服务器备份文件到Linux,或者Linux备份文件到Windows,最适合的情况是第一种,即Linux作为备份服务器,并且rsync作为进程一直守护。如果以xinetd的方式的话,则可实现rsync按照需求进行启动,从而可以节省运算资源。
这种方案也适合于Linux生产服务器每天晚上负载低的情况下,进行数据同步,即,对某个数据产生目录,或者生产日志进行同步,如果数据量一天产生的不大,且内网带宽应该没有问题,是进行每日同步的。如果对于业务比较重要的,可以进行每隔多少时间进行同步一次。