跳到主要内容

第5章 GitLab系统维护

1. GitLab 数据备份

1.1 备份方式

备份说明

GitLab 提供了内置的备份工具,可以完整备份所有项目数据、数据库、配置文件等。

# 创建备份
gitlab-backup create

# 查看备份文件
ll /var/opt/gitlab/backups/

1.2 定时备份

# 添加定时任务
crontab -e

# 每天凌晨2点自动备份
0 2 * * * /opt/gitlab/bin/gitlab-backup create CRON=1

2. GitLab 数据恢复

2.1 恢复步骤

恢复注意事项

恢复前请确保 GitLab 版本与备份时的版本一致,并停止相关服务。

# 停止数据写入服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

# 恢复数据(替换为实际的时间戳)
gitlab-backup restore BACKUP=1498472447_2024_06_30_16.1.0

# 重启所有服务
gitlab-ctl restart

# 检查应用状态
gitlab-rake gitlab:check SANITIZE=true

3. 优化启动速度

GitLab 默认开启了很多组件,但是有很多我们并不需要的监控组件:

[root@gitlab ~]# gitlab-ctl status
run: alertmanager: (pid 9622) 11s; run: log: (pid 9621) 11s
run: gitaly: (pid 9601) 11s; run: log: (pid 9600) 11s
run: gitlab-monitor: (pid 9604) 11s; run: log: (pid 9603) 11s
run: gitlab-workhorse: (pid 9599) 11s; run: log: (pid 9598) 11s
run: grafana: (pid 9619) 11s; run: log: (pid 9618) 11s
run: logrotate: (pid 9616) 11s; run: log: (pid 9605) 11s
run: nginx: (pid 9602) 11s; run: log: (pid 9596) 11s
run: node-exporter: (pid 9617) 11s; run: log: (pid 9606) 11s
run: postgres-exporter: (pid 9624) 11s; run: log: (pid 9623) 11s
run: postgresql: (pid 9614) 11s; run: log: (pid 9613) 11s
run: prometheus: (pid 9612) 11s; run: log: (pid 9611) 11s
run: redis: (pid 9594) 11s; run: log: (pid 9593) 11s
run: redis-exporter: (pid 9610) 11s; run: log: (pid 9609) 11s
run: sidekiq: (pid 9607) 11s; run: log: (pid 9597) 11s
run: unicorn: (pid 9615) 11s; run: log: (pid 9608) 11s

3.1 精简配置

性能优化建议

通过禁用不必要的监控组件,可以显著减少内存占用和启动时间。

精简之后的配置:

[root@gitlab ~]# grep "^[a-Z]" /etc/gitlab/gitlab.rb 
external_url 'http://10.0.0.200'
grafana['enable'] = false
prometheus['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_exporter['enable'] = false

3.2 应用配置

# 重新配置 GitLab
gitlab-ctl reconfigure

# 检查状态
gitlab-ctl status

4. 常用维护命令

4.1 健康检查

# 检查 GitLab 健康状态
gitlab-rake gitlab:check

# 检查环境信息
gitlab-rake gitlab:env:info

# 检查 Git 配置
gitlab-rake gitlab:check:git_config

4.2 清理缓存

# 清理缓存
gitlab-rake cache:clear

# 清理会话
gitlab-rake gitlab:cleanup:sessions:all

4.3 重置密码

# 进入 Rails 控制台
gitlab-rails console

# 查找用户并重置密码
user = User.find_by(username: 'root')
user.password = 'new_password'
user.password_confirmation = 'new_password'
user.save!

更新: 2024-06-30 21:33:02