网上有很多实时同步的文章,采用触发式同步,不过给我感觉用的不爽,这样配置那样也要配置太麻烦,自己写两脚本全自动下,只要在脚本里面对应改下目录,及IP就OK,具体看如下操作
Rync_Server:192.168.1.2
Rync_Client:192.168.1.3
双方同步目录:/var/www/test
Server脚本:实现了自动安装服务,自动配置,具体看操作噢
我们进入192.168.1.2
1、[root@manage shell]# mkdir /var/www/test#创建同步目录
2、[root@manage shell]# chmod 777 ser_rsync.py #设置脚本权限
3、[root@manage shell]# ./ser_rsync.py 执行脚本安装并提示安装成功
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
Promgram installtion is succeed
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
Promgram installtion is succeed
Rsync服务端,已经全部安装完成,下面红色部分是这个脚本的注意需要修改的地方
作用于Server的脚本!
#!/usr/bin/python
#Rsync service configution
import os,sys
#Check program is or not installation.
pro = '/usr/bin/rsync'
if not os.path.isfile(pro):
os.system('yum install -y rsync*')
etd = '/usr/sbin/xinetd'
if not os.path.isfile(etd):
os.system('yum install -y xinetd*')
#Direct change the start the way for the program.
rs = '/etc/xinetd.d/rsync'
os.system( "sed -i '6s/yes/no/g' %s" % rs)
#set up configuration
conf = '/etc/rsyncd.conf'
path = 'path = /var/www/test' #rsync directory! 需要同步的目录
os.remove(conf)
if not os.path.isfile(conf):
os.mknod(conf)
info = ['uid = root','gid = root','use chroot = no',\
'max connections = 0','[web]',path,'ignore errors ',\
'read only = no','list = yes','hosts allow = 192.168.1.0/24 ',\
'auth users = root','secrets file = /etc/server.pass ']
配置参数
for i in info:
men = open(conf,'a').write(i+'\n')
passw = '/etc/server.pass' #socket password
if not os.path.isfile(passw):
os.mknod(passw)
data = open(passw).read()
if len(data) == 0:
open(passw,'w').write ('root:*o1>sSD.df') 密码
#Start the program
command = 'service xinetd restart'
if os.system(command) == 0:
print 'Promgram installtion is succeed'
Clinet脚本:实现时实同步,具何看操作
进入192.168.1.3
1、[root@SQL1 shell]# mkdir /var/www/test #创建同步目录
2、[root@SQL1 shell]# chmod 777 rsync_client.sh #设置权限
3、[root@SQL1 shell]# nohup sh -c './rsync_client.sh' >/dev/null &#创建后台启动
4、[root@manage shell]# cd /var/www/test/ 进入192.168.1.2
5、[root@manage test]# touch testfile #创建文件
6、[root@SQL1 shell]# cd /var/www/test/ 切到192.168.1.3查看文件是否同步
7、[root@SQL1 test]# ll 文件创建成功
total 0
-rw-r--r-- 1 root root 0 Jul 17 09:22 testfile
total 0
-rw-r--r-- 1 root root 0 Jul 17 09:22 testfile
下面红色部分是这个脚本的注意需要修改的地方
作用于Client的脚本!
#!/bin/sh
#set -x
src="/var/www/test" 客户端目录
ip= "192.168.1.2"
env= "nohup sh -c ' /shell/rsync_client.sh 脚本路经' >/dev/null &"
ck=`cat "/etc/rc.local"|grep 'client'`
if [ -z "${ck}" ];then
echo $env >> '/etc/rc.local'
fi
if [ ! -f "/etc/client.pass" ];then
echo "*o1>sSD.df"密码跟SERVER一至 > /etc/client.pass
chmod 600 /etc/client.pass
fi
while :
do
sleep 10
rsync -zrtopg -- delete --password-file=/etc/client.pass root@$ip::web $src
done
#set -x
src="/var/www/test" 客户端目录
ip= "192.168.1.2"
env= "nohup sh -c ' /shell/rsync_client.sh 脚本路经' >/dev/null &"
ck=`cat "/etc/rc.local"|grep 'client'`
if [ -z "${ck}" ];then
echo $env >> '/etc/rc.local'
fi
if [ ! -f "/etc/client.pass" ];then
echo "*o1>sSD.df"密码跟SERVER一至 > /etc/client.pass
chmod 600 /etc/client.pass
fi
while :
do
sleep 10
rsync -zrtopg -- delete --password-file=/etc/client.pass root@$ip::web $src
done
考虑到复制出来编码问题,两段代码在附件中
转载于:https://blog.51cto.com/skyson/605592