原有的Gitlab
概述: 数据目录挂载在外部的/data/server/gitlab/
下,主要涉及到的有状态内容为/etc/gitlab
下的配置、/var/log/gitlab
下的日志、/var/opt/gitlab
下的数据
过程: 通过备份命令gitlab-rake gitlab:backup:create SKIP=artifacts
创建备份, 在新的服务器上拉起Gitlab后进入维护模式并解决了sql的报错与配置文件恢复
docker-compose.yml配置
gitlab_web:
image: 'gitlab/gitlab-ce:13.11'
restart: always
container_name: gitlab
hostname: '10.10.1.x'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url '10.10.1.x:8939'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
ports:
- '8939:8939'
- '2222:22'
volumes:
- '/data/server/gitlab/config:/etc/gitlab'
- '/data/server/gitlab/logs:/var/log/gitlab'
- '/data/server/gitlab/data:/var/opt/gitlab'
启动命令
docker-compose up -d
备份命令
备份机制: crontab -l
crontab -e
备份脚本: 10 3 * * * /usr/bin/bash /root/gitlab_bak.sh >> /root/cron.log 2>&1
/root/gitlab_bak.sh:
#!/bin/bash
docker exec gitlab gitlab-rake gitlab:backup:create SKIP=artifacts &&\
rm -f /data/server/gitlab/data/backups/*$(date -d -2day +%Y_%m_%d)* &&\
scp /data/server/gitlab/data/backups/*$(date -d -1day +%Y_%m_%d)* root@10.10.1.xx:/backups/gitlab/
新服务器的Gitlab
docker-compose.yml配置与原有配置一致, 需要额外挂载:
- 一份备份的文件,原始位置为
/data/server/gitlab/data/backups/
( 例:1735759346_2025_01_01_13.11.3_gitlab_backup.tar
) 到容器内容,也可以在容器启动后通过docker cp
进去 - 配置文件
gitlab.rb
, 原始位置为/data/server/gitlab/config:/etc/gitlab
- 配置文件
gitlab-secrets.json
, 原始位置为/data/server/gitlab/config:/etc/gitlab
启动gitLab容器后进入维护模式:
docker-compose up -d && sleep 10 &&\
docker exec -it gitlab gitlab-ctl stop unicorn &&\
docker exec -it gitlab gitlab-ctl stop sidekiq
配置pg的权限(避免报错Restoring PostgreSQL database gitlabhq_production ... ERROR: must be owner of extension pg_trgm...
)
按照步骤如果直接执行数据恢复,可能会报如下错误:
Restoring PostgreSQL database gitlabhq_production ... ERROR: must be owner of extension pg_trgm
ERROR: must be owner of extension btree_gist
ERROR: must be owner of extension btree_gist
ERROR: must be owner of extension pg_trgm
解决方法( 参考gitLab数据备份和迁移及PostgreSQL异常处理):
# 进入容器内部执行
docker exec -it gitlab bash
# 修改数据库配置listen_addresses = ''===>listen_addresses = '*'
vim /var/opt/gitlab/postgresql/data/postgresql.conf
listen_addresses = '*'
# 增加信任策略
vim /var/opt/gitlab/postgresql/data/pg_hba.conf
local all all trust
host all all 127.0.0.1/32 trust
# 设置数据库权限(通过`gitlab-psql -d gitlabhq_production`链接数据库后执行`ALTER USER gitlab WITH SUPERUSER;`并通过`\q`退出)
$ gitlab-psql -d gitlabhq_production
gitlabhq_production=# ALTER USER gitlab WITH SUPERUSER;
ALTER ROLE
gitlabhq_production=# \q
# 重启gitlab服务
gitlab-ctl restart
执行数据恢复
docker exec -it gitlab gitlab-ctl stop unicorn
docker exec -it gitlab gitlab-ctl stop sidekiq
# 进入容器内部执行
docker exec -it gitlab bash
# 可能会涉及备份文件权限问题,添加权限
chown git:git /var/opt/gitlab/backups/1735759346_2025_01_01_13.11.3_gitlab_backup.tar
chmod 777 /var/opt/gitlab/backups/1735759346_2025_01_01_13.11.3_gitlab_backup.tar
# 最好后台执行,恢复过程较久(依据数据量大小,当前参考为5G的数据,恢复约1h),以1735759346_2025_01_01_13.11.3_gitlab_backup.tar为例,无需指定`_gitlab_backup.tar`,BACKUP的工作目录在/var/opt/gitlab/backups
gitlab-rake gitlab:backup:restore BACKUP=1735759346_2025_01_01_13.11.3
恢复配置文件和密钥
# 进入容器内部执行
docker exec -it gitlab bash
# 备份
cp /etc/gitlab/gitlab-secrets.json /etc/gitlab/gitlab-secrets.json.bak && cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.bak
# 覆盖
cp gitlab-secrets.json /etc/gitlab/gitlab-secrets.json
cp gitlab.rb /etc/gitlab/gitlab.rb
# 重新配置 GitLab
gitlab-ctl reconfigure
gitlab-ctl restart