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服务器

[root@localhost ~]#  yum -y install subversion
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
[root@localhost ~]# mkdir -p /application/svndata/yunjisuan
[root@localhost ~]# mkdir -p /application/svnpasswd
#创建yunjisuan项目主分支,开发分支,测试分子
[root@localhost ~]# svnadmin create /application/svndata/yunjisuan/master
[root@localhost ~]# svnadmin create /application/svndata/yunjisuan/dev
[root@localhost ~]# svnadmin create /application/svndata/yunjisuan/test
[root@localhost ~]# cd /application/svndata/yunjisuan/master/conf/
[root@localhost conf]# cp svnserve.conf{,.bak}
[root@localhost conf]# vim svnserve.conf
[root@SVN 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

#将配置文件覆盖另外两个分支
[root@localhost conf]#  /bin/cp svnserve.conf /application/svndata/yunjisuan/dev/conf/
[root@localhost conf]# /bin/cp svnserve.conf /application/svndata/yunjisuan/test/conf/
[root@localhost conf]# cp /application/svndata/yunjisuan/master/conf/authz /application/svnpasswd/
[root@localhost conf]# cp /application/svndata/yunjisuan/master/conf/passwd /application/svnpasswd/
[root@localhost conf]#  cd /application/svnpasswd/
[root@localhost svnpasswd]#  vim passwd 
yunjisuan = 123456
benet = 123456
stu001 = 123456
stu002 = 123456
[root@SVN svnpasswd]# vim authz 
[root@SVN 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测试分支代码提交者---开发经理
[root@localhost svnpasswd]# svnserve -d -r /application/svndata/
[root@localhost 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报错)
[root@localhost ~]# yum -y install subversion-perl perl-Digest-MD5
#创建Git工作目录并克隆SVN分支代码到本地
[root@localhost ~]# mkdir -p /backup
[root@localhost ~]# cd /backup
#将SVN服务器master分支代码克隆到本地Git工作目录
[root@localhost 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工作目录
[root@localhost 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工作目录
[root@localhost 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
[root@localhost backup]# ls
dev  master  test
[root@localhost 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项目里。分支名称不变
在这里插入图片描述

[root@localhost ~]# cd /backup/
[root@localhost backup]# ls
dev  master  test
[root@localhost backup]# cd master/
[root@localhost master]# ls
111.txt
[root@localhost master]# git remote add origin http://192.168.146.132:8888/test/demo.git
#将master目录代码推送到GitLab仓库的Demo项目里
[root@localhost 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项目里
[root@localhost master]# cd /backup/dev/
[root@localhost dev]# git branch 
* master
[root@localhost dev]# git branch dev
[root@localhost dev]# git checkout dev
Switched to branch 'dev'
[root@localhost dev]# git branch 
* dev
  master
[root@localhost dev]# ls
asde.txt
[root@localhost dev]# git add *
[root@localhost dev]# git commit -m "dev branch commit"
On branch dev
nothing to commit, working tree clean
[root@localhost dev]# git remote -v
[root@localhost dev]# git remote add origin http://192.168.146.132:8888/root/demo.git
[root@localhost 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项目里
[root@localhost dev]# cd /backup/test/
[root@localhost test]# git branch
* master
[root@localhost test]# git branch test 
[root@localhost test]# git checkout test
Switched to branch 'test'
[root@localhost test]# git branch 
  master
* test
[root@localhost test]# git add *
[root@localhost test]# git commit -m "test branch commit"
On branch test
nothing to commit, working tree clean
[root@localhost test]# git remote -v
[root@localhost test]# git remote add origin http://192.168.146.132:8888/root/demo.git
[root@localhost 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上操作
[root@localhost gitlab]# mkdir -p backups
[root@localhost gitlab]# cd backups/
[root@localhost backups]# pwd
/var/opt/gitlab/backups
#执行命令生成备份压缩包
[root@localhost backups]# gitlab-rake gitlab:backup:create
#查看备份包
[root@localhost backups]# ls
1545670181_2018_12_24_11.2.3_gitlab_backup.tar
[root@localhost 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上操作
[root@localhost backups]# mkdir -p /backup
#修改如下配置文件
[root@localhost backups]# cd /etc/gitlab/
[root@localhost gitlab]# ls
gitlab.rb  gitlab-secrets.json  trusted-certs
[root@localhost gitlab]# cp gitlab.rb{,.bak}
[root@localhost gitlab]# ls
gitlab.rb  gitlab.rb.bak  gitlab-secrets.json  trusted-certs
[root@localhost gitlab]# vim gitlab.rb
 299  gitlab_rails['backup_path'] = "/backup"

#重载GitLab配置文件
[root@localhost gitlab]# gitlab-ctl reconfigure
#再次进行备份尝试
[root@localhost gitlab]# cd /backup/
[root@localhost backup]# ls
[root@localhost backup]# gitlab-rake gitlab:backup:create
[root@localhost backup]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar

(3) GitLab的自动备份
将命令写成定时任务,每天凌晨两点执行一次备份操作
(4) 设置备份过期时间
设置只保存最近7天的备份,编辑/etc/gitlab/gitlab.rb配置文件,找到如下配置并修改

#在Git02上操作
[root@localhost backup]# vim /etc/gitlab/gitlab.rb
307  gitlab_rails['backup_keep_time'] = 604800
  1. copy老服务器上面的备份文件到新服务器
    (1) 确保新GitLab服务器和老GitLab服务器版本相同
    在G01上和G02上都部署同版本的GitLab服务器
    (2) copy老服务器上面的备份文件到新服务器
#在Git02上操作
[root@localhost backup]# cd /backup/
[root@localhost backup]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar
[root@localhost 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的备份目录下
[root@localhost ~]# mv 1545670768_2018_12_24_11.2.3_gitlab_backup.tar /var/opt/gitlab/backups/
[root@localhost ~]# cd /var/opt/gitlab/backups/
[root@localhost backups]# ls
1545670768_2018_12_24_11.2.3_gitlab_backup.tar
#给备份文件增加777权限
[root@localhost backups]# chmod 777 1545670768_2018_12_24_11.2.3_gitlab_backup.tar 
[root@localhost 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上操作
#停止相关数据连接服务
[root@localhost backups]# gitlab-ctl stop unicorn
ok: down: unicorn: 1s, normally up
[root@localhost backups]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

(3)执行命令从备份文件中恢复GitLab
第三步:执行命令从备份文件中恢复GitLab
gitlab-rake gitlab:backup:restore BACKUP=备份文件编号

#在Git01上操作
#执行命令恢复数据
[root@localhost backups]# gitlab-rake gitlab:backup:restore BACKUP=1545670768_2018_12_24_11.2.3
说明:
恢复过程中出现两个(yes/no),输入两个yes即可

(4)重新启动GitLab
第四步:启动GitLab

#在Git01上操作
[root@localhost 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和端口不同外,项目内容完全一致
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43304804/article/details/85226916
今日推荐