Gerrit 數據庫遷移 H2 遷移爲 mysql(包括 changes):
歷史環境:
當前系統安裝 Gerrit 爲 2.11,且數據庫使用 H2;
但隨着開發規模擴大,H2 數據庫逐漸無法滿足上百個開發的提交;
因此決定將 Gerrit 的 H2 數據庫更換爲 Mysql;
需要用到一個工具 h2tomysql_for_gerrit ,網上說這個工具只是在 gerrit 2.9.1 上測試通過;
我本地是用的 2.11 版本也可以通過
下載地址:
https://github.com/uppet/h2tomysql_for_gerrit
需要注意的是:下載後要編譯裏面的java文件(可以用 eclipse build)
從理論上分析需要的步驟:
分兩種情況:
1. 在同一臺服務器上遷移:
a. 先 stop 原先的 gerrit 服務;
b. 安裝新的 gerrit 站點服務(安裝時設置 mysql 數據庫),
需要與原先的不同站點目錄,例如原先爲 gerrit_site ,新服務爲 gerrit_site2;
不要啓動該 gerrit 服務,如果啓動了,就停止;
c. 拷貝原 gerrit 站點中的所有 git 庫到新的 gerrit 指向的git庫目錄(如果新舊 gerrit 的代碼庫目錄一致,則跳過),
注意:不包括 All-Projects 和 All-Users ;
d. 通過 h2tomysql_for_gerrit 這個工具,將原始 gerrit 的 H2 數據導入到新站點的 Mysql 數據庫中;
e. 由於數據庫已經被更新,因此需要更新新 gerrit 中 All-Project 的 groups 文件中的 Administrator UUID;
並且,需要更新 data-gerrit/All-Projects.git/refs/meta/config 中的 hash 值爲更新上述group文件之後的commit點;
f. 進入新 gerrit 站點目錄 gerrit_site2,需要重建索引(這步尤其重要,否則 changes 不會被顯示出來):
java -jar bin/gerrit.war reindex
這個操作比較耗時,主要取決於當前包含的git代碼庫和changes;
g. 啓動新 gerrit 服務;
h. 清除舊的 gerrit 站點;
2. 在不同服務器上遷移:
原理與同一臺服務器上一樣,只是需要注意兩點:
a. 將舊 gerrit 的服務站點目錄,例如 gerrit_site 拷貝到新的服務器;
b. 將舊 gerrit 服務器中所有的 git 代碼庫拷貝到新 gerrit 服務器中指定目錄(All-Projects 和 All-Users)
其他的步驟都跟在同一個服務器操作完全一致
具體實施步驟:
以本地爲例,新舊 gerrit 處於不同服務器中(本人親測成功):
服務器 A 爲 Gerrit 2.11,H2
服務器 B 爲 Gerrit 2.11,Mysql
上述新舊 gerrit 服務器請優先安裝
(注意,一定要保證兩個服務器的 gerrit 版本一致,因爲不同 gerrit 版本的數據庫結構可能是不一樣的)
所有 Gerrit 的安裝配置步驟略過,安裝過程沒有特殊情況,可以參考網上其他文章。
開始執行:
1. 在服務器 B 上安裝相同版本的 gerrit+mysql
2. 在 A 上將 gerrit_site 目錄(目錄下所有文件),scp 到 B 上;
3. 拷貝 A 上的所有 git 庫到 B 上 gerrit 指定的 git 代碼庫目錄(注意不要拷貝原始 gerirt 的 All-Projects 和 All-Users);
4. 在 B 上 stop gerrit 服務;
5. 下載 h2tomysql_for_gerrit 工具到任意目錄,並通過 eclipse 編譯其中的 java 文件,然後會生成 bin 目錄:
bin/
└── com
└── ucweb
└── gerrit
└── tools
└── converter
├── GerritDatabaseConverter.class
├── H2DatabaseSession.class
├── H2ToMySQLLauncher.class
├── IDatabaseSession.class
├── MySQLDatabaseSession.class
└── Util.class
6. 命令行進入 h2tomysql_for_gerrit 目錄,執行命令:
java -cp "bin:lib/*" com.ucweb.gerrit.tools.converter.H2ToMySQLLauncher [path/to/old/gerrit.config] [path/to/new/gerrit.config]
此命令將會將舊 gerrit H2 中的數據導入到新 gerrit mysql 中(需要注意最後兩個參數的順序);
錯誤提示:
a. Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-174]
在執行上述命令時必須要將所有 gerrit 服務關閉;
b. Access denied for user 'gerrit'@'127.0.0.1' (using password: YES)
由於 mysql 數據庫是需要指定賬號密碼的,而在 gerrit.config 中默認是沒有密碼的明文顯示,因此需要在 gerrit.config 中指定mysql的賬號密碼:
[database]
type = mysql
hostname = localhost
database = gerrit
username = gerrit
password = <password>
出現如下提示,表示執行成功:
Feeding data into patch_set_approvals
Feeding data into patch_sets
Feeding data into schema_version
Feeding data into starred_changes
Feeding data into submodule_subscriptions
Feeding data into system_config
Fixing increment into change_message_id
Fixing increment into account_id
Fixing increment into change_id
Special fix for account_group_id
Special fix for account_id
Special fix for uuid of admin
Done tranfer database data from H2 to MySQL.
REMEMBER to update `groups' file in meta/config tag of `All-Project' to make Administrator UUID correct.
請尤其注意最後一行提示 "REMEMBER to update `groups' file in meta/config tag of `All-Project' to make Administrator UUID correct."
由於更新之後數據庫中所有數據都是舊 gerrit 服務器中的數據,因此對於新 gerrit 中的 All-Project 這個 git 庫中的內容是需要手動更新的;
具體更新方法稍後再提。
7. 需要重建索引表(這一步非常重要):
進入 B 服務器的 Gerrit 站點目錄(gerrit_site),執行命令:
java -jar gerrit-site/bin/gerrit.war reindex #注意不要完全複製,用自己實際情況的目錄
此步驟一般不會有太大的問題,主要就是耗時,視當前 git 庫的多少和 changes 數目定。
提示:可以使用 --threads 50 參數指定進程數。
8. 更新 B 服務器的 All-Projects(針對步驟6中的最後說明):
此庫主要保存的是 All-Projects 設置的權限配置,以及各個 group 的UUID
更新(只有1個文件):
groups -------各個groups的UUID ,這個文件暫時先不更新
project.config 項目的權限配置,這個文件如果沒有改動,也可跳過
具體操作:
分別 clone 新舊 Gerrit 服務器上對應的 All-Projects 庫,然後將舊的同步到新的上;
修改完成,然後 commit push (在新的gerrit中)
記錄下最後一次 commit 的 hash 值,可以通過 git log 查看
將這個 hash 值更新到新 gerrit git 代碼庫中的:
All-Projects.git/refs/meta/config (注意此處不是剛纔clone的庫)文件中,刪除原有內容,直接替換爲新的 hash。
9. 啓動 B 服務器(新)Gerrit 服務;
注意,別忘記拷貝用戶密碼文件,如果你用的是 http 認證,則 gerrit_site/etc/passwords .
10. 就可以登錄新的 Gerrit 服務了,一切數據都完好;
最後,擴展一下:
如果既要遷移數據庫,又要升級 gerrit 版本,那麼只能分爲兩步走:
1. 先在同一個 gerrit 版本中遷移數據庫(H2 --> Mysql);
2. 然後再安裝新的 gerrit 版本;
本地試驗從 Gerrit 2.11 + H2 ---> Gerrit 2.14.5 + Mysql 是可以成功的。
寫得比較囉嗦,希望可以幫到大家,歡迎指正!