天天看點

菜鳥60秒配置Rsync實時同步一站式

 網上有很多實時同步的文章,采用觸發式同步,不過給我感覺用的不爽,這樣配置那樣也要配置太麻煩,自己寫兩腳本全自動下,隻要在腳本裡面對應改下目錄,及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

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

下面紅色部分是這個腳本的注意需要修改的地方

作用于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 

考慮到複制出來編碼問題,兩段代碼在附件中

本文轉自 ibelieveme 51CTO部落格,原文連結:http://blog.51cto.com/skyson/605592,如需轉載請自行聯系原作者