1. 程式人生 > >SVN——GitLab——GitLab資料遷移

SVN——GitLab——GitLab資料遷移

專案案例一:生產環境SVN資料遷移到測試環境GitLab

主機名 IP地址 備份 特殊要求
SVN 192.168.146.133 SVN伺服器 無
Git01 192.168.146.131 Git客戶端 無
Git02 192.168.146.132 GitLab伺服器01 記憶體2G
在這裡插入圖片描述

  1. 案例背景
    公司近期對於"版本管理工具是否進行切換SVN–>Git"的問題進行了討論,於是對svn和git進行了相關研究,進而梳理出了Git的特點(優,缺點),最後將Git與SVN進行了對比,對比結果詳細見下方內容。
    在這裡插入圖片描述
    在這裡插入圖片描述

  2. 部署SVN伺服器

[[email protected]
~]# yum -y install subversion Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile [[email protected] ~]# mkdir -p /application/svndata/yunjisuan [[email protected] ~]# mkdir -p /application/svnpasswd #建立yunjisuan專案主分支,開發分支,測試分子 [[email protected] ~]# svnadmin create /application/svndata/yunjisuan/master [
[email protected]
~]# svnadmin create /application/svndata/yunjisuan/dev [[email protected] ~]# svnadmin create /application/svndata/yunjisuan/test [[email protected] ~]# cd /application/svndata/yunjisuan/master/conf/ [[email protected] conf]# cp svnserve.conf{,.bak} [[email protected] conf]# vim svnserve.conf [
[email protected]
conf]# sed -n '19p;20p;27p;34p' svnserve.conf anon-access = none auth-access = write password-db = /application/svnpasswd/passwd authz-db = /application/svnpasswd/authz #將配置檔案覆蓋另外兩個分支 [[email protected] conf]# /bin/cp svnserve.conf /application/svndata/yunjisuan/dev/conf/ [[email protected] conf]# /bin/cp svnserve.conf /application/svndata/yunjisuan/test/conf/ [[email protected] conf]# cp /application/svndata/yunjisuan/master/conf/authz /application/svnpasswd/ [[email protected] conf]# cp /application/svndata/yunjisuan/master/conf/passwd /application/svnpasswd/ [[email protected] conf]# cd /application/svnpasswd/ [[email protected] svnpasswd]# vim passwd yunjisuan = 123456 benet = 123456 stu001 = 123456 stu002 = 123456 [[email protected] svnpasswd]# vim authz [[email protected] svnpasswd]# egrep -v "#|^$" authz [aliases] [groups] testgroup = stu001,stu002 [yunjisuan/master:/] yunjisuan = rw #master主分支程式碼提交者---開發經理 benet = r [yunjisuan/dev:/] benet = rw #dev開發分支程式碼提交者---普通程式設計師 yunjisuan = rw #dev開發分支程式碼下載者---開發經理 [yunjisuan/test:/] @testgroup = r #test測試分支程式碼下載者----測試人員 yunjisuan = rw #test測試分支程式碼提交者---開發經理 [[email protected] svnpasswd]# svnserve -d -r /application/svndata/ [[email protected] svnpasswd]# ps -ef | grep svn root 1468 1 0 09:01 ? 00:00:00 svnserve -d -r /application/svndata/ root 1470 1233 0 09:01 pts/0 00:00:00 grep --color=auto svn

在宿主機建立三個svn目錄checkout程式碼,如下圖
在這裡插入圖片描述
在這裡插入圖片描述

#在Git01上操作
#安裝git-svn支援程式(沒有這兩個包git-svn報錯)
[[email protected] ~]# yum -y install subversion-perl perl-Digest-MD5
#建立Git工作目錄並克隆SVN分支程式碼到本地
[[email protected] ~]# mkdir -p /backup
[[email protected] ~]# cd /backup
#將SVN伺服器master分支程式碼克隆到本地Git工作目錄
[[email protected] backup]# git svn clone --no-metadata svn://192.168.238.144/yunjisuan/master/ /backup/master/
Initialized empty Git repository in /backup/master/.git/
Authentication realm: <svn://192.168.238.144:3690> e855b85e-53fa-45de-b0b7-e13cab0e422e
Password for 'root': 				#輸入root登陸密碼
Authentication realm: <svn://192.168.238.144:3690> e855b85e-53fa-45de-b0b7-e13cab0e422e
Username: yunjisuan				#輸入svn的master分支的授權賬戶名
Password for 'yunjisuan': 			#輸入svn的master分支的授權賬戶密碼
	A	MySQL.txt
r1 = d80216bc4bf313e22268ccf5e9dd49632d538049 (refs/remotes/git-svn)
Checked out HEAD:
  svn://192.168.238.144/yunjisuan/master r1
#將SVN伺服器dev分支程式碼克隆到本地Git工作目錄
[[email protected] backup]# git svn clone --no-metadata svn://192.168.238.144/yunjisuan/dev/ /backup/dev/
Initialized empty Git repository in /backup/dev/.git/
Authentication realm: <svn://192.168.238.144:3690> c274266c-9497-426c-91f3-ce8682f3df7c
Password for 'root': 
Authentication realm: <svn://192.168.238.144:3690> c274266c-9497-426c-91f3-ce8682f3df7c
Username: yunjisuan 
Password for 'yunjisuan': 
	A	dev.txt
r1 = b6c7497814662dcb3e518669ae043976962190a4 (refs/remotes/git-svn)
Checked out HEAD:
  svn://192.168.238.144/yunjisuan/dev r1
#將SVN伺服器test分支程式碼克隆到本地Git工作目錄
[[email protected] backup]# git svn clone --no-metadata svn://192.168.238.144/yunjisuan/test/ /backup/test/
Initialized empty Git repository in /backup/test/.git/
Authentication realm: <svn://192.168.238.144:3690> a8e75531-683f-4797-842c-ad649c9cf184
Password for 'root': 
Authentication realm: <svn://192.168.238.144:3690> a8e75531-683f-4797-842c-ad649c9cf184
Username: yunjisuan
Password for 'yunjisuan': 
	A	111.txt
r1 = 93837f2b17fedd83c1e3768891e8b49d57264f22 (refs/remotes/git-svn)
Checked out HEAD:
  svn://192.168.238.144/yunjisuan/test r1
[[email protected] backup]# ls
dev  master  test
[[email protected] backup]# tree
.
├── dev
│   └── dev.txt
├── master
│   └── MySQL.txt
└── test
    └── 111.txt

3 directories, 3 files

在GitLab上建立一個組,在組裡新增3個成員(主程式設計師,開發者,報告者)
GitLab預設情況下,master分支是受到保護的(只能主程式設計師推送和合並)
主程式設計師=產品經理:推送和合並受保護分支
開發者=開發人員:推送不受保護分支
報告者=測試人員:只能clone程式碼
在GitLab這個組裡建立一個專案叫做Demo
通過主程式設計師賬號將從SVN克隆下來的master分支,dev分支,test分支的程式碼推送到Demo專案裡。分支名稱不變
在這裡插入圖片描述

[[email protected] ~]# cd /backup/
[[email protected] backup]# ls
dev  master  test
[[email protected] backup]# cd master/
[[email protected] master]# ls
111.txt
[[email protected] master]# git remote add origin http://192.168.146.132:8888/test/demo.git
#將master目錄程式碼推送到GitLab倉庫的Demo專案裡
[[email protected] master]# git push -u origin master
Username for 'http://192.168.146.132:8888': root
Password for 'http://[email protected]:8888': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.146.132:8888/root/demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
#將dev目錄程式碼推送到GitLab倉庫的Demo專案裡
[[email protected] master]# cd /backup/dev/
[[email protected] dev]# git branch 
* master
[[email protected] dev]# git branch dev
[[email protected] dev]# git checkout dev
Switched to branch 'dev'
[[email protected] dev]# git branch 
* dev
  master
[[email protected] dev]# ls
asde.txt
[[email protected] dev]# git add *
[[email protected] dev]# git commit -m "dev branch commit"
On branch dev
nothing to commit, working tree clean
[[email protected] dev]# git remote -v
[[email protected] dev]# git remote add origin http://192.168.146.132:8888/root/demo.git
[[email protected] dev]# git push -u origin dev
Username for 'http://192.168.146.132:8888': root
Password for 'http://[email protected]:8888': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.146.132:8888/root/demo/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://192.168.146.132:8888/root/demo.git
 * [new branch]      dev -> dev
Branch dev set up to track remote branch dev from origin.
#將test目錄程式碼推送到GitLab倉庫的Demo專案裡
[[email protected] dev]# cd /backup/test/
[[email protected] test]# git branch
* master
[[email protected] test]# git branch test 
[[email protected] test]# git checkout test
Switched to branch 'test'
[[email protected] test]# git branch 
  master
* test
[[email protected] test]# git add *
[[email protected] test]# git commit -m "test branch commit"
On branch test
nothing to commit, working tree clean
[[email protected] test]# git remote -v
[[email protected] test]# git remote add origin http://192.168.146.132:8888/root/demo.git
[[email protected] test]# git push -u origin test
Username for 'http://192.168.146.132:8888': root
Password for 'http://[email protected]:8888': 
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 246 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for test, visit:
remote:   http://192.168.146.132:8888/root/demo/merge_requests/new?merge_request%5Bsource_branch%5D=test
remote: 
To http://192.168.146.132:8888/root/demo.git
 * [new branch]      test -> test
Branch test set up to track remote branch test from origin.

在這裡插入圖片描述#### 專案案例二:測試環境GitLab資料遷移到生產環境GitLab

  1. 案例背景
    前段時間,在某臺CentOS伺服器上搭建了GitLab環境,並且其他開發組的同仁都陸陸續續把程式碼從svn遷移到了GitLab,但是之前的CentOS伺服器並不是搭建在公司的機房環境,而是搭建在辦公室的某臺閒置的電腦上,因此為了保證資料安全性,領導要求將之前的GitLab資料全部重新遷移到公司機房的伺服器上去。
主機名 IP地址 備份 特殊要求
Git01 192.168.146.131 GitLab伺服器01 記憶體2G
Git02 192.168.146.132 GitLab伺服器02 記憶體2G
  1. GitLab的資料備份
    (1) 建立備份檔案
    首先,我們得把老伺服器上的Gitlab整體備份,使用Gitlab一鍵安裝包安裝Gitlab非常簡單,同樣的備份恢復與遷移也非常簡單。使用一條命令即可建立完整的Gitlab備份。

gitlab-rake gitlab:backup:create

使用以上命令會在/var/opt/gitlab/backups目錄下建立一個名稱類似為1537115457_2018_09_17_11.2.3_gitlab_backup.tar的壓縮包,這個壓縮包就是Gitlab整個的完整部分,其中開頭1537115457_2018_09_17\是備份建立的日期,11.2.3是Gitlab的版本號
/etc/gitlab/gitlab.rb配置檔案需要備份
/var/opt/gitlab/nginx/conf nginx配置檔案
/etc/postfix/main.cf postfix 郵件配置備份

#在Git02上操作
[[email protected] gitlab]# mkdir -p backups
[[email protected] gitlab]# cd backups/
[[email protected] backups]# pwd
/var/opt/gitlab/backups
#執行命令生成備份壓縮包
[[email protected] backups]# gitlab-rake gitlab:backup:create
#檢視備份包
[[email protected] backups]# ls
1545670181_2018_12_24_11.2.3_gitlab_backup.tar
[[email protected] backups]# ll
total 80
-rw-------. 1 git git 81920 Dec 24 11:49 1545670181_2018_12_24_11.2.3_gitlab_backup.tar	#最後一次備份

(2)更改Gitlab備份目錄
GitLab配置檔案路徑:/etc/gitlab/gitlab.rb

#在Git02上操作
[[email protected] backups]# mkdir -p /backup
#修改如下配置檔案
[[email protected] backups]# cd /etc/gitlab/
[[email protected] gitlab]# ls
gitlab.rb  gitlab-secrets.json  trusted-certs
[[email protected] gitlab]# cp gitlab.rb{,.bak}
[[email protected] gitlab]# ls
gitlab.rb  gitlab.rb.bak  gitlab-secrets.json  trusted-certs
[[email protected] gitlab]# vim gitlab.rb
 299  gitlab_rails['backup_path'] = "/backup"

#過載GitLab配置檔案
[[email protected] gitlab]# gitlab-ctl reconfigure
#再次進行備份嘗試
[[email protected] gitlab]# cd /backup/
[[email protected] backup]# ls
[[email protected] backup]# gitlab-rake gitlab:backup:create
[[email protected] backup]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar

(3) GitLab的自動備份
將命令寫成定時任務,每天凌晨兩點執行一次備份操作
(4) 設定備份過期時間
設定只儲存最近7天的備份,編輯/etc/gitlab/gitlab.rb配置檔案,找到如下配置並修改

#在Git02上操作
[[email protected] backup]# vim /etc/gitlab/gitlab.rb
307  gitlab_rails['backup_keep_time'] = 604800
  1. copy老伺服器上面的備份檔案到新伺服器
    (1) 確保新GitLab伺服器和老GitLab伺服器版本相同
    在G01上和G02上都部署同版本的GitLab伺服器
    (2) copy老伺服器上面的備份檔案到新伺服器
#在Git02上操作
[[email protected] backup]# cd /backup/
[[email protected] backup]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar
[[email protected] backup]# scp 1545670768_2018_12_24_11.2.3_gitlab_backup.tar [email protected]:/root/		#Git01的IP地址
The authenticity of host '192.168.146.132 (192.168.146.132)' can't be established.
ECDSA key fingerprint is SHA256:14NKIeIbfU5Cx3usA72K/AmnoeDt/UyH+SII8+rV1dA.
ECDSA key fingerprint is MD5:60:ab:53:4a:f8:fc:74:2b:93:1e:6f:b3:4c:e4:33:de.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.146.132' (ECDSA) to the list of known hosts.
[email protected]'s password: 
1545670768_2018_12_24_11.2.3_gitlab_backup.tar    100%   80KB   5.3MB/s   00:00    

  1. 從備份資料中恢復GitLab
    (1)將備份檔案許可權修改為777
    第一步:將備份檔案許可權修改為777,不然可能恢復的時候會出現許可權不夠,不能解壓的問題。
#在Git01上操作
#將拷貝過來的備份檔案移動到新GitLab的備份目錄下
[[email protected] ~]# mv 1545670768_2018_12_24_11.2.3_gitlab_backup.tar /var/opt/gitlab/backups/
[[email protected] ~]# cd /var/opt/gitlab/backups/
[[email protected] backups]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar
#給備份檔案增加777許可權
[[email protected] backups]# chmod 777 1545670768_2018_12_24_11.2.3_gitlab_backup.tar 
[[email protected] backups]# ll 1545670768_2018_12_24_11.2.3_gitlab_backup.tar 
-rwxrwxrwx. 1 root root 81920 Dec 24 12:03 1545670768_2018_12_24_11.2.3_gitlab_backup.tar

(2)執行命令停止相關資料連線服務
第二步:執行命令停止相關資料連線服務

#在Git01上操作
#停止相關資料連線服務
[[email protected] backups]# gitlab-ctl stop unicorn
ok: down: unicorn: 1s, normally up
[[email protected] backups]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

(3)執行命令從備份檔案中恢復GitLab
第三步:執行命令從備份檔案中恢復GitLab
gitlab-rake gitlab:backup:restore BACKUP=備份檔案編號

#在Git01上操作
#執行命令恢復資料
[[email protected] backups]# gitlab-rake gitlab:backup:restore BACKUP=1545670768_2018_12_24_11.2.3
說明:
恢復過程中出現兩個(yes/no),輸入兩個yes即可

(4)重新啟動GitLab
第四步:啟動GitLab

#在Git01上操作
[[email protected] backups]# gitlab-ctl start
ok: run: alertmanager: (pid 1440) 29683s
ok: run: gitaly: (pid 1424) 29684s
ok: run: gitlab-monitor: (pid 1429) 29684s
ok: run: gitlab-workhorse: (pid 1420) 29684s
ok: run: logrotate: (pid 29683) 1656s
ok: run: nginx: (pid 1428) 29684s
ok: run: node-exporter: (pid 1426) 29684s
ok: run: postgres-exporter: (pid 1431) 29684s
ok: run: postgresql: (pid 1434) 29684s
ok: run: prometheus: (pid 1437) 29683s
ok: run: redis: (pid 1410) 29684s
ok: run: redis-exporter: (pid 1439) 29683s
ok: run: sidekiq: (pid 31634) 0s
ok: run: unicorn: (pid 31641) 1s

(5)進行Web訪問檢查
最終我們發現,兩臺GitLab除了IP和埠不同外,專案內容完全一致
在這裡插入圖片描述