由於前陣子公司的 Server 負載一直過重,那幾天簡直快搞死我了,不過確實也讓自己在短時間又多學了不少東西,由於年紀大了容易忘東忘西,所以像這種東西只好趁熱紀錄下來,好讓自己以後可以翻出來看。   因為一台 Server 撐部太住,所以再開另一台 Server 來跑 Load Balance,後來證明這想法還是太膚淺,再用 ELB 之前,還是先找出真正造成 Server 負載的原因吧!很多都是程式碼的遺毒,先把這些都搞定來再考慮比較實在。 而使用 AWS 的 ELB 服務要注意的是: 1. 盡可能選用兩台等級一樣的機器 (如果只是跑 Web Service 似乎可以接受兩台等級有差距的 instance) 2. 設定好 ELB 後,記得把 DNS 加入別名 接著進入正題,如何將兩台 Server 做雙向同步,這裡我分了三個步驟: 第一步、兩台 Server 要同步的資料夾設定好 第二步、設定排程,定時執行雙向同步的動作 第三步、設定檔案新增觸發雙向同步機制 因次會使用到的工具如下: 1. 同步工具 - unison (參考連結) 2. 排程工具 - crontab 3. 目錄偵測工具 - inotifywait ( inotify-tools )

第一步:使用 unison 將雙向同步的機制設定好

1. 請先安裝 unison $ sudo apt-get install unison 2. 撰寫副檔名為 .prf 的設定檔(e.g. sync.prf),請在自己的家目錄下新增 .unison 資料夾來放設定檔 ($HOME/.unison) 下面是參考網站上提供的範例,感覺很夠用了
# Two root directories to sync. # You can use ssh:// to sync over SSH root = /home/alice/sync_folder root = ssh://dev@192.168.1.10//home/alice/sync_folder # If you want one-way mirroring from one replica to the other, specify the source replica using "force" as follows. # force = /home/alice/sync_folder # If you want Unison to run without any user input, try "batch" mode. batch = true # If you don't want to be prompted, and just accept Unison's recommendation: auto = true # Optionally, you can sync specific sub directories only (under the root). # path = dir1 # Optionally, you can ignore specific files or directories that are matched with regular expressions. # ignore = Name *.o # ignore = Path */temp/archive_* # If you want to ignore difference in file props: perms = 0
3. 確定兩台主機都可以使用 SSH 連到對方,中間牽涉到一些問題,比如:  .登入帳號是否具備有同步目錄以及子目錄操作權限  .若要排程同步就要注意帳號登入問題,最好是都將對方的 SSH public key 加到 authorize_keys,免密碼登入 4. 執行雙向同步測試,檢查結果 $ unison sync 正常的結果如下:
Contacting server... Connected [//local//home/alice/sync_folder -> //remote_host//home/alice/sync_folder] Looking for changes Waiting for changes from server Reconciling changes new file --> document1.pdf <-- new file my.jpg Propagating updates UNISON 2.40.63 started propagating changes at 21:19:13.65 on 20 Sep 2013 [BGN] Copying document1.pdf from /home/alice/sync_folder to //remote_host//home/alice/sync_folder [BGN] Copying my.jpg from //remote_host//home/alice/sync_folder to /home/alice/sync_folder [END] Copying my.jpg [END] Copying document1.pdf UNISON 2.40.63 finished propagating changes at 21:19:13.68 on 20 Sep 2013 Saving synchronizer state Synchronization complete at 21:19:13 (2 items transferred, 0 skipped, 0 failed)
5. 如果出現一些什麼 Permission denied 的訊息,應該就是資料夾權限問題,因為我一直遇到,所以才會一直寫出來,希望別再犯傻

第二步:使用 crontab,編輯排程工作

6. 指令 crontab -e,進入排程編輯 $ crontab -e 然後加上指令(下面範例為,每15分鐘同步一次) */15 * * * * /usr/bin/unison sync

第三步:偵測同步資料夾內檔案新增動作

7. 監測目錄內檔案是否有新增,使用 inotifywait 指令,需要先安裝 inotify-tools $ sudo apt-get install inotify-tools 8. 撰寫偵測指令的 script 檔 $ vim watch.sh 內容如下 #!/bin/bash while true; do inotifywait -e create -e moved_to -r 監測目錄 && /usr/bin/unison sync; done 9. 更改 script 檔權限為可執行 $ chmod a+x watch.sh 10. 背景執行 script 來常駐偵測動作 $ ./watch.sh &
PS. 注意事項,要停止偵測動作的時候,先停止 script 的執行,然後在停止偵測指令  .先查詢 script 的 process id   $ ps aux | grep watch.sh
ubuntu 29247 0.0 0.2 11028 1384 pts/0 S 10:13 0:00 /bin/bash ./watch.sh
 .刪除 script 的 process   $ kill 29247  .在查詢 inotifywait 的 process id   $ ps aux | grep inotifywait
ubuntu 29466 0.0 0.1 6596 704 pts/0 S 10:15 0:00 inotifywait -e crea…
 .刪除偵測的 process   $ kill 29466
文章標籤
全站熱搜
創作者介紹
創作者 soarlin 的頭像
soarlin

每天都有新鮮事

soarlin 發表在 痞客邦 留言(0) 人氣(1,746)