一个月黑风高的夜晚紧急完成gitlab服务器数据迁移

前言

真是无巧不成书,白天刚刚讨论了一天 SVN 服务器迁移事情,晚上突然被告知 gitlab 服务无法访问了,赶紧连接到服务器上查看,好家伙,磁盘还剩下可怜的 98KSVN 不管了,先把 gitlab 搞定吧,毕竟第二天的游戏更新包都打不出来了,确认是磁盘不足问题后已经是9点半了,月亮都升起老高了,赶紧开始修复吧。

修复过程

关于修复过程,网上有很多文章写了具体的步骤,都非常的相似,基本来自于官方文档的翻译,不过抄来抄去的内容产生了一些错误,甚至是在关键的步骤上出错,所以在看这些文章是要特别注意,如果你的英文还不错的话,建议看一下官方文档

为了记录过程我也不免俗套的把这些步骤写一写,方便日后拿来即用,关于每步的作用我会进行简述,并且提一下注意事项。

总体步骤:在配置文件中把存放数据的路径改一个新目录,把原数据移动到新的目录,重启gitlab服务即可

备份数据

虽然只是做数据迁移,不涉及切换物理机,也不会更换服务版本,但是为了保险起见,还是先把数据备份一份,现在遇到的问题是 gitlab 是默认安装的,数据存放在 /var/opt/gitlab/git-data 目录下,仓库存放在子目录 repositories 里面,这个目录一般空间都不大,很容易出现磁盘满的情况,目录结构如下:

git-data
└── repositories
    ├── +gitaly
    │   ├── cache
    │   ├── state
    │   └── tmp
    └── @hashed
        ├── 19
        ├── 35
        ├── 3f
        ├── 45
        ├── 4a
        ├── 4b
        ├── ...
        └── f5

真正的仓库数据就在 @hashed 目录下众多子目录下,再往下走就是一个个 .git目录了,结构与我们本地的 .git 目录一致。

备份的命令需要记录一下:

gitlab-rake gitlab:backup:create

备份的路径可以在配置文件 /etc/gitlab/gitlab.rb 中修改:

[root@code-server ~]# vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups"    // gitlab备份目录
gitlab_rails['backup_archive_permissions'] = 0644       // 生成的备份文件权限
gitlab_rails['backup_keep_time'] = 7776000              // 备份保留天数为90天

备份从21:30开始一直到凌晨1点左右结束,200G 数据用时3个半小时,真是太慢了,大部分的时间都卡在备份了 Dumping lfs objects

[root@gitlab-server ~]# gitlab-rake gitlab:backup:create
2022-07-25 21:27:34 +0800 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2022-07-25 21:27:42 +0800 -- done
2022-07-25 21:27:42 +0800 -- Dumping repositories ...
 * xxxx/project (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x) ...
 * xxxx/project (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x) ... [DONE]
 * xxxx/project.wiki (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x.wiki) ...
 * xxxx/project.wiki (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x.wiki) ... [EMPTY] [SKIPPED]
 * xxxx/project.design (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x.design) ...
 * xxxx/project.design (@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab3x.design) ... [EMPTY] [SKIPPED]
 ...
 ...
2022-07-25 22:16:14 +0800 -- done
2022-07-25 22:16:14 +0800 -- Dumping uploads ...
2022-07-25 22:16:15 +0800 -- done
2022-07-25 22:16:15 +0800 -- Dumping builds ...
2022-07-25 22:16:15 +0800 -- done
2022-07-25 22:16:15 +0800 -- Dumping artifacts ...
2022-07-25 22:16:15 +0800 -- done
2022-07-25 22:16:15 +0800 -- Dumping pages ...
2022-07-25 22:16:15 +0800 -- done
2022-07-25 22:16:15 +0800 -- Dumping lfs objects ...
2022-07-26 00:41:39 +0800 -- done
2022-07-26 00:41:39 +0800 -- Dumping container registry images ...
2022-07-26 00:41:39 +0800 -- [DISABLED]
Creating backup archive: 1658767299_2022_07_26_xx.xx.xx-ee_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.

修改存放目录

这一步注意创建新目录后修改权限,并修改配置文件

[root@gitlab ~]# mkdir -p /data/gitlab-data
[root@gitlab ~]# chown -R git:root /data/gitlab-data
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb
# 把注释取消然后指定新的仓库存储位置,原配置是# git_data_dirs({ "default" => { "path" => "/mnt/nas/git-data" } })
git_data_dirs({
    
     "default" => {
    
     "path" => "/data/gitlab-data" } })

/data/gitlab-data 是新挂载的目录,目标路径和其子路径必须不能为软链接。

关闭服务

迁移数据前关闭服务主要是怕别人再使用,其实已经凌晨应该没有人用了,为了保险起见还是先关了吧

gitlab-ctl stop

关闭时会自动关闭多个服务项

[root@gitlab-server gitlab]# gitlab-ctl stop
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-exporter: 0s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: grafana: 0s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: puma: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up

迁移数据

rsync -av /var/opt/gitlab/git-data/repositories /data/gitlab-data/

末尾的/要有,这一点很多文章都提到了,但是它们给的例子中有没有,很容易造成迷惑,这一步其实就是把 repositories 完整的拷贝到 /data/gitlab-data/ 目录下,并保留原文件和目录的属性,不加 / 就少了一级目录,不过也有文章让使用 cp 或者 mv 命令迁移数据,虽然也能达到目的,但是不如 rsync 合适,他们无法保留原属性。

200G 数据拷贝了半小时,比备份操作快多了

刷新配置

gitlab-ctl reconfigure

这一步我在执行前以为瞬间就能完成,运行后才发现这个命令检查了很多数据,在控制台输出了一大堆内容,可以注意一下有没有报错信息。

启动服务

gitlab-ctl start

启动之后需要等大概一分钟才能访问,否则会报服务器内部错误,应该是有些服务还没完全开始工作吧

[root@gitlab-server gitlab]# gitlab-ctl start
ok: run: alertmanager: (pid 17961) 1s
ok: run: gitaly: (pid 17984) 0s
ok: run: gitlab-exporter: (pid 18023) 0s
ok: run: gitlab-workhorse: (pid 18025) 1s
ok: run: grafana: (pid 18054) 0s
ok: run: logrotate: (pid 18063) 1s
ok: run: nginx: (pid 18082) 0s
ok: run: node-exporter: (pid 18111) 1s
ok: run: postgres-exporter: (pid 18120) 0s
ok: run: postgresql: (pid 18129) 0s
ok: run: prometheus: (pid 18136) 1s
ok: run: puma: (pid 18151) 0s
ok: run: redis: (pid 18157) 1s
ok: run: redis-exporter: (pid 18173) 0s
ok: run: sidekiq: (pid 18183) 1s

总结

  • gitlab 服务器配置文件是 /etc/gitlab/gitlab.rb
  • gitlab 配置文中设置备份目录 gitlab_rails['manage_backup_path'],存储目录 git_data_dirs
  • gitlab-rake gitlab:backup:create 用于备份
  • gitlab 关闭组件 gitlab-ctl stop,开启组件 gitlab-ctl start,重启组件 gitlab-ctl restart
  • gitlab-ctl reconfigure 用于刷新配置
  • gitlab 迁移数据命令 rsync -av /var/opt/gitlab/git-data/repositories /data/gitlab-data/

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

曾经幅员辽阔,东征西讨的元朝占据了欧亚大陆,但持续时间不过百年,在历史长河中只是星星点点,若要在这长河中留下痕迹,必然要做出影响全人类的重大成就,而你我绝大多数都是普通人,承认自己是普通人的那一刻你已经成长了~

猜你喜欢

转载自blog.csdn.net/shihengzhen101/article/details/126075098