2015-11-27

MySQL Slave 重啟後步驟

此文章的場景是描述 MySQL Slave 和 Master 斷了同步資料,如何恢復。


  1. [M] myql> FLUSH TABLES WITH READ LOCK;
  2. [M] $ mysqldump --all-databases --master-data | bzip2 -9 > mysql.bz2
  3. [M] scp mysql.bz2 foo@SLAVE:
  4. [S] mysql> STOP SLAVE;
  5. [S] bzip2 -dc | mysql -u root -p
  6. [M] mysql> RESET MASTER;
  7. [S] mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=147;
  8. [S] mysql> START SLAVE;
  9. [M] UNLOCK TABLES;

2015-10-04

WordPress Replication

手上有兩台異地不同 Public IP 主機,在 CloudFlare 設定 DNS Round Robin,又運行著 WordPress,下面解說如何設定兩台主機同步。讓兩台主機都能登入,且新文章能互相同步。

MySQL 資料庫

參考 MySQL Replication 設定。要注意的是因為做的是 DNS Round Robin,所以隨機兩台主機都要設定 master-slave 兩種角色同時運行,我參考 Digital Ocean 的文章完成。

同步腳本

因為 WordPress 要處理 cache、files 資料夾,腳本請參閱 FTP Replication

FTP Replication

我有兩台異地不同 Public IP 主機,在 Cloud Flare 設定 Round Robin,運行著 nginx。此文講解使用兩個腳本同步檔案。

我的 H0 機器讓人 FTP 上傳檔案,監控程式偵測有檔案異動及開始 rsync 到 H1。示意圖如下:



H0


L3 ~ L6: 因為 web root 權限需要,所以使用 sudo 提高權限。
L8: H0 & H1 透過 ssh 加密傳輸,得事先加信任公鑰。 L14 ~ L15: inotifywait 排除掉不想監控的檔案 ([Tt]hunms.db),接著只監控特定事件 (delete, moved_to, close_write, create)。為了記錄過程我把時間戳一並寫出,log 的樣子會長這樣:
15/10/04 21:37:05 CREATE /var/www/example.com/index.html
L9: 在 H1 機器上有個 pool 目錄放置這些需要同步的檔案,原因不想讓 rsync 直接寫到 web root,權限過大危險。
L21: 移除前綴字,避免同步路徑錯誤。
L24: 移除檔案名稱,讓 rsync 同步資料夾,而非單一檔案。
L26: rsync + ssh 同步資料夾到 H1。

H1


L3 ~ L6: 因為要寫入到 web root,要求得是 root 。
L13 ~ L26: 做的事情和 H0 一樣,只是來源和目的互換。

已知問題

刪除

inotifywait 遇到刪除檔案或目錄,只會出現 OPEN,ISDIR,接著就沒了,所以也無法偵測到 DELETE 事件。rsync 直到下個時間才會把上個未刪除的工作做完。


2015-10-02

GoDaddy 註冊系統有安全疑慮

GoDaddy 註冊系統只接受最長 11 個字的 local-part,而 Email 地址的規範 RFC 5322 明確指出最長可以到 64 個字。

緣由

我在 GoDaddy 註冊了一個帳號,我的 Email local-part 有 30 個字,因為某個原因我需要重新登錄網站,結果就無法登錄。查了 Whois 發現 GoDaddy 把我的 Email 縮短了,而且是沒有規則。

客服

0. 官網沒有文字客服

GoDaddy 不知緣由為何,停止了文字客服,只剩電話客服。因為擔心英文口說無法順利解決我的問題,且如此長的國際電話肯定所費不貲。最後讓我找到了 @GoDaddyHelp

1. 英譯證件

GoDaddy 客服來信要求我提供英文翻譯的官方證件,原先試著自己 PS 將身分證翻譯成英文版本,但是客服不接受這種翻譯。後來我提供了護照,但是由於當初註冊使用的名字不是漢語拼音,而是沒有法律效力的自取英文名字。我在信中和對方表明當初我沒有想到註冊會失敗,且我的自取英文名字和漢語拼音的用處。客服人員基於保護客戶帳號安全,提供了三個方式:

0. 試著聯繫 Whois 上面錯誤的 Email。
1. 透過 ICANN 投訴
2.  法院傳票要回。

2. 將錯就錯

GoDaddy 客服拒絕我的護照證明後,我在信中嚴厲的反應這是網站註冊系統的瑕疵,造成我的困擾。客服根本不想回應我的理由,因為他有理由可以假設我是壞人,透過如此手段竊取別人的域名,想想也是有道理,就不在此爭端,徒勞罷。

我讀了 ICANN 投訴的方式,根據 2015-03-01 發佈的 Rules for Uniform Domain Name Dispute Resolution Policy,投訴方得負擔所有調查的費用,看了 GODADDY SUBPOENA POLICY/ATTORNEY TIPS 光研究每小時就得付 75 USD,這已經超出我的域名購買費用,不划算。

最後,我想到了將錯就錯的伎倆,去申請 Whois 錯誤的 Email。因為是 Gmail 所以可以隨意申請,果真行得通,重置密碼的信件順利收到,把登入權限給拿回來了!


安全疑慮

一拿到登入權限後,試著在後台變更 Email 為 30 個字的帳號,發現依然不行,然後聯繫 @GoDaddyHelp,得到的答案是:

I was not able to duplicate this in another account. You may want to try using an alternate email address.

這種回答讓我對 GoDaddy 灰心,工程文化沒落實完全,如此簡單的註冊瑕疵能用這麼簡單的方式拿回權限,如果今天我就是壞人呢?

2015-09-01

事件捕捉進化

最近寫網頁爬蟲用到並行的方法,因為 golang 接收 channel 是外層包一個 for loop,觀察到這樣會讓一個邏輯 CPU 使用率滿載,這個寫法是 busy waiting,認為這麼做耗電很浪費資源。或許 golang 採用 CSP channel 就是決定得走 busy waiting。

找到一篇 Haskell 討論 select(2) 或 poll(2) 捕捉方法,有人提出這些討論的視野都是較為底層,在 Haskell 可以用更高階優雅的方式解決。閱畢感到興奮,就是要優雅!

2014-10-08

iOS 安裝 in-house app 需要的網站

因為所處的 wifi 網路環境封鎖所有 outbound,要開放 iOS Enterprise Distribution 安裝所需的網路。修改了 iPhone wifi DNS server,得到以下記錄。


  1. 17-courier.push.apple.com
  2. 1-courier.push.apple.com
  3. 26-courier.push.apple.com
  4. 2-courier.push.apple.com
  5. 31-courier.push.apple.com
  6. 36-courier.push.apple.com
  7. 40-courier.push.apple.com
  8. 9-courier.push.apple.com
  9. a1441.g4.akamai.net
  10. a97.gi3.akamai.net
  11. apple.com
  12. apple-finance.query.yahoo.com
  13. cl4.apple.com
  14. e2842.e9.akamaiedge.net
  15. init-p01st.push.apple.com
  16. local
  17. mesu.apple.com
  18. p24-keyvalueservice.icloud.com
  19. p24-keyvalueservice.icloud.com.akadns.net
  20. setup.icloud.com
  21. www.apple.com
  22. www.isg-apple.com.akadns.net
  23. xp.apple.com

2014-05-14

CoreImage & GPUImage performance

The benchmark suite numbers,

Specs

iPhone 5s
iOS 7.1.1
GPUImage 0.1.4

Still images

CPU 181.51ms
Core Image 37.08ms
GPUImage 49.62ms

Live video

CPU 15.77ms
Core Image 3.20ms
GPUImage 2.36ms

2014-04-03

xcodebuild

xcodebuild is a command line that in charge of some actions in Xcode, like build, analyze, archive, test, installsrc, install and clear.


Let says, I need release with a enterprise provision file, so here is the script,


2014-02-07

iOS 免加 UUID 發佈方式

以往都由 Apple 審核發佈 app,測試階段都是在自己的設備上,都能知道 UUID。目前做的 app 需要免加 UUID 任由安裝,Apple 稱為 in-house distribution,能有此功能只有 iOS Developer Enterprise Program 才做得到。

2013-12-18

IRC 紀錄即時推送

irssi + inotify-tools + git 即時將紀錄推送到 github

Irssi

首先將 irssi 紀錄檔設定為每天切割:
/log open -autoopen -targets #tcffm ~/irclogs/Freenode/#tcffm-%Y-%m-%d

Git init

cd ~/irclogs/Freenode
git init
touch README
git add README
git commit -am "First commit."
git remote add origin git@github.com:OOO/tcffm.git
git push -u origin master

腳本

1. daily.sh
此腳本使用 crontab 每天 0:00 啟動,內容如下:
#!/bin/bash
killall -9 inotifywait
cd '/home/OOO/irclogs/Freenode'
today=$(date +%Y-%m-%d)
git add \#tcffm-$today
inotifywait -qme modify --format "sh onchange.sh %w" \#tcffm-$today | sh
2. onchange.sh
#!/bin/bash
git commit -am "log"
git push
將兩個腳本權限設定為 755

啟動

執行 ~/irclogs/Freenode/daily.sh


後記

inotifywait 可以監看每個檔案修改的動作,產生對應的動作。

2013-11-28

Concurrency

以前總覺得 concurrency 很難很酷,如 pthread 那樣精深,難上手。最近三年的工作經驗認識 asynchronous I/O,發現這是一種 concurrency,才覺得沒有這麼夢幻,覺得實用。我的工作經驗上,沒遇過 concurrency computing。

Computer Memory Hierarchy 解釋,CPU 以外的 I/O 都是很慢的,讓  CPU 等待是罪過,此決定掌握在 programmer 手上。

使用 aio 方式我用過幾種:
1) Object-C delegate
初體驗,也是啟發的框架。

2) go goroutine + channel
新語言的設計。

3) JavaScript ajax + callback
常見的方式。

自從開始有這些體驗,盡量落實每個動作都是 async,朝 C10M 邁進。

2013-10-22

德國電信商保護境內資料

美國竊聽流往法國通信被揭露,看到德國的電信商正在研擬一個計畫,加強保護境內資料。只要是發送端和接收端都在德國,或是德活盟友。透過路由設定,資料只會在國內走,不會往外。目前看來影響不大,因為太多服務提供者不在德國。

這個草案表現出一個態度,德國人不喜歡被監聽,可能不選擇美國服務,且自身加強保護。如此一來,德國民眾可能會有更多自己國家的服務,形成特殊現象。

2013-08-08

使用 rsync & git 建立每日備份

工作上遇到災難,救災完畢後評估使用 rsync 和 git 建立每日還原點。

災難是:
前人建立某個帳戶,家目錄在 /,用 userdel -r 清理舊帳戶,時間過久才發現災難已經造成。
兩個月前閒來無事,有做系統總備份,httpd 和 mysql 是兩個月前的紀錄,客戶難免唉唉叫。救災完畢後,使用 git 建立每日備份。

主機特性是代管客戶網頁資料,包含圖片和資料庫。

概略步驟是:
1. rsync 初始,git init
2. 每日 rsync 同步,git commit
3. 每日產生 git bundle

Webpages

使用 rsync 將來源抓到工作目錄,然後在工作目錄建 git 資料夾。


接著每天做備份



最後每天產生 git bundle


MySQL

和 webpages 相似的步驟,先初始




接著每天備份,產生 git bundle




最後產生 git bundles




還原




後記

災難後尋找備份方案時,見到有人每日、每週、每月做不同程度的備份。記得將每日的動作放進 crontab 定時執行,放在離峰時間比較不影響服務品質。

資料搞失或資料復原都需要付出慘痛的代價,如果能做成自動化,可以降低很多反應時間和損失。

2013-05-29

Notification 掉包

Google Cloud MessagingApple Push Notification Service 對於發佈推撥期間裝置離線的處理,都採以下政策:

只送最後一則。

引用 GCM:
GCM will usually deliver messages immediately after they are sent. However, this might not always be possible. For example, the device could be turned off, offline, or otherwise unavailable. In other cases, the sender itself might request that messages not be delivered until the device becomes active by using the delay_while_idleflag. Finally, GCM might intentionally delay messages to prevent an application from consuming excessive resources and negatively impacting battery life.
GCM Advanced Topics, Setting an Expiration Date for a Message
引用 APNS:
If APNs attempts to deliver a notification but the device is offline, the notification is stored for a limited period of time, and delivered to the device when it becomes available. 
Only one recent notification for a particular application is stored. If multiple notifications are sent while the device is offline, each new notification causes the prior notification to be discarded. This behavior of keeping only the newest notification is referred to as coalescing notifications. 
If the device remains offline for a long time, any notifications that were being stored for it are discarded.
Local and Push Notification Programming Guide, Quality of Service

所以要自己做驗證,確保推撥有成功。

比較 Naver LINE Android 和 iOS 版本對於離線訊息的處理,一旦接上網路,離線訊息一併收到數則。猜測可能的作法是:

1. Device offline
2. Send #1 msg, server enqueue 1 msg
3. Send #2 msg, server enqueue 2 msgs
4. Device online
5. Server send queue
6. Device splits msgs and local notificate one-by-one.

2013-03-28

2013-01-09

MD5 使用注意事項

在 Go  裡使用 md5 做校驗不難,如範例所寫:


若要在迴圈裡做上面的事情,可能會有這樣的寫法:


跑出來的結果是:


f1() 和 f2() 會有不同的結果是因為 io.WriteStringmd5.go 實作細節, WriteString 會找 Writer 的實作,而在 md5.go 的實作如下:


看見 += 就可以猜的出來是附加的方式,所以每次非得使用新的 instance,目前還沒發現能清除 digest 內容的方法。

2012-11-08

Go



這篇記錄最近一個多月學習 Go 的心得。Go 的語法相對於 Object-C 簡潔,後置的資料型態變數宣告,可以懶惰省去資料型態宣告。

有兩件事情是我碰壁許久:

json.Marshal



執行結果會得到:


p1 is: {"FirstName":"John","LastName":"Doe"}.
p2 is: {}.


要輸出的成員變數必須大寫開頭才行,下面的 package 方法同樣套用。


Exported Functions


隨意撰碼都需要模組分割,追問題容易,讀碼速度快。Go 使用資料夾結構定義 package 封裝,捨棄 MakefileHow to Write Go Code 闡述如何做到模組化,所有細節皆有提到就是 Function name 首字要大寫沒提到[1],所以一直看到

./main.go:10: cannot refer to unexported name foo.foo1

程式碼如下:

foo.go

main.go

這是最近的心得。


Updated:
1. 在 The Go Programming Language Specification 的 Exported identifiers 有提到這些細節。

2012-05-09

Toggle network for iOS Simulator

Network fault-tolerant is important to asynchronous request. For iOS simulator can not toggle the network easily, because of its network ability is binding to its level. So that if you turn your host network off, then your iOS simulator network is also down. However, the host lost all network connection.

The firewall rule can help me do that smart, just block the site and port you would. You can use the below script:



You can change the host you want block in line 3 banHost.

2012-04-24

Git hooks Xcode version

Xcode has two fields to set version information to app. Version is for releasing number, build is for building number, find more information in Information Property List Key Reference.


Since Xcode 4.0 starting to support git version control, so that we could bind git tag and commit hash text with Xcode field. The steps in the pseudo-code are below:


  1. $ git commit -am "COMMIT LOG"
  2. trigger post-commit hooking
  3. Setting the tag number and new commit hash to plist.


In the real code is like below:


Put the code in a file to PROJECT_FOLDER/.git/hooks/post-commit, and make it executable.