rsync+lsyncd實現實時同步

爲了網站的隨時可以遷移或者負載均衡,必須保證web根目錄的一致性,所以得實時同步

下面是我模擬遷移前的一個web同步:

用於遷移前的數據同步,主要利用rsync+lsync

客戶端:192.168.1.135 (網站備份數據)

服務器:192.168.1.136 (網站源數據)

客戶端安裝rsync

yum install -y rsync xinetd

 

rsync添加配置文件

vi /etc/rsyncd.conf

[root@cm root]# cat /etc/rsyncd.conf

uid = nobody                      //啓動服務的用戶

gid = nobody                      //啓動服務的組

use chroot = yes                   //限制籠環境

max connections = 10               //最大連接數

timeout = 300                     //超時時間

strict modes = yes                 

pid file = /var/run/rsync/rsyncd.pid     

#lock file = /var/run/rsync/rsyncd.lock

log file = /var/log/rsync/rsyncd.log

[data]                           //模塊

#path =/usr/local/nginx-1.6.3/html/home/paixian/root

path = /usr/local/nginx/html/home/paixian/root     //模塊路徑

ignore errors = yes                             //忽略錯誤

read only = no                                //是否只讀

write only = no                               //是否只寫

hosts allow = *                               //訪問限制

#hosts deny =

list = false                                   

uid = root

gid = root

auth users = min                                //認證用戶

secrets file = /etc/rsyncd.passwd                  //認證密碼文件

 

配置認證文件

vi /etc/rsyncd.passwd

[root@cm root]# cat /etc/rsyncd.passwd

min:min                                      //用戶:密碼

添加權限

chmod 600 /etc/rsyncd.passwd                    //授權

啓動服務

service xinetd restart

檢測服務是否啓動

ss -tulnp|grep 873

 

 

服務器安裝rsync

yum install -y rsync xinetd

添加認證文件

vi /etc/rsyncd.passwd

[root@min bin]# cat /etc/rsyncd.passwd

min

授權認證文件

chmod 600 /etc/rsyncd.passwd

測試客戶端的rsync

rsync --list-only--password-file=/etc/rsyncd.passwd [email protected]::data

[root@min bin]# rsync --list-only --password-file=/etc/[email protected]::data

drwxrwxrwx        4096 2015/10/15 18:22:35 .

-rwxr-xr-x         404 2015/09/17 17:17:30 admin.php

-rwxr-xr-x         246 2015/09/17 22:14:00 config.php

-rwxr-xr-x         299 2015/09/17 17:15:37 index.php

-rwxr-xr-x          25 2015/09/17 17:17:30 phpinfo.php

-rwxr-xr-x        1414 2015/09/17 17:15:36product_list.phtml

-rwxr-xr-x        1998 2015/09/17 17:17:30 test.php

-rw-r--r--         114 2015/10/15 12:29:55 test1.php

-rw-r--r--           4 2015/10/15 18:22:17 test2.php

drwxrwxrwx        4096 2015/09/17 17:17:53 .cache

drwxrwxrwx        4096 2015/09/17 17:17:31 admin

drwxrwxrwx        4096 2015/09/17 17:15:36 data

drwxrwxrwx        4096 2015/09/17 17:17:30 demo

drwxrwxrwx        4096 2015/09/17 17:15:36 demo2

drwxrwxrwx        4096 2015/09/17 17:13:48phpdemo_boxcontroler

drwxrwxrwx        4096 2015/10/15 15:26:03 root

drwxrwxrwx        4096 2015/09/17 17:13:49 statics

drwxrwxrwx        4096 2015/09/17 17:17:30 test

drwxrwxrwx        4096 2015/09/17 17:15:44 web

安裝lsyncd

cd /usr/local

unzip lsyncd-master.zip         //解壓

cd lsyncd-master   

cmake-DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd-2.1.5  //編譯指定路徑

make && make install

添加配置文件

cd /usr/local/lsyncd-2.1.5

mkdir etc  var                  //創建配置文件和日誌文件

vi etc/lsyncd.conf                //添加配置文件

[root@min root]# cat/usr/local/lsyncd-2.1.5/etc/lsyncd.conf  

settings {

   logfile     ="/usr/local/lsyncd-2.1.5/var/lsyncd.log",

   statusFile  ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",

    inotifyMode ="CloseWrite",

   maxProcesses = 7,

   -- nodaemon  =true,

}

sync {

   default.rsync,

   source     ="/usr/local/nginx-1.6.3/html/home/root", //源路徑

   target     ="[email protected]::data",        //目標路徑

   delete="running",

   exclude = {".*",".tmp"},                        //排除哪些文件

   delay = 0,                                   //推遲多少秒,一起發送

   init = false,

   rsync    ={

       binary = "/usr/bin/rsync",

       archive = true,

       compress = true,

       verbose  = true,

       password_file = "/etc/rsyncd.passwd",

       _extra        ={"--bwlimit=200"}

}

}

啓動lrsyncd

cd bin/

./lsyncd -log Exec/usr/local/lsyncd-2.1.5/etc/lsyncd.conf

自行檢測

mysql的實時同步:

http://10554846.blog.51cto.com/10544846/1693049

http://10554846.blog.51cto.com/10544846/1697185

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章