gitlab的备份、恢复、迁移、升级

gitlab的备份、恢复、迁移、升级

一、备份

备份目录和gitlab.rb中定义的备份目录必须一致
GitLab的版本和备份文件中的版本必须一致,否则还原时会报错。

## 创建备份目录:
mkdir -p /data/backups/gitlabs

## 添加定时任务(每天凌晨2:00备份):
00 02 * * * /usr/bin/gitlab-rake gitlab:backup:create   

## gitlab自带备份命令
gitlab-rake gitlab:backup:create

## 配置文件中配置自定义目录:
vim /etc/gitlab.rb 
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/backups/gitlabs"    #备份的路径,可以自定义 目录一定要存在并且有权限写入。
gitlab_rails['backup_keep_time'] = 604800                #单位是秒,这里604800表示备份保留7天 #代表只保留7天

## 重新执行初始化操作:
gitlab-ctl reconfigure                                   # 如果修改了备份的路径,记得要执行一次初始化操作:

## 备份过程
当出现Backup XXXXXXXXX done.表示备份结束

[root@bj-hom-130 ~]# gitlab-rake gitlab:backup:create
2023-03-11 16:12:06 UTC -- Dumping database ... 
2023-03-11 16:12:10 UTC -- 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.
2023-03-11 16:12:10 UTC -- Backup 1678551126_2023_03_11_15.9.3 is done.
2023-03-12 00:12:10 +0800 -- Deleting backup and restore lock file

**** 注意:
“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.”

这部分表示 gitlab.rb 和 gitlab-secrets.json 两个文件包含敏感信息。未被备份到备份文件中。需要手动备份
备份tar包一定要放到备份路径下。恢复是删除原有数据,恢复备份tar包中的数据。
在新服务器恢复备份,一定要记得将 gitlab.rb 和 gitlab-secrets.json 手动复制到相应路径下。
gitlab.rb路径:/etc/gitlab/gitlab.rb
gitlab-secrets.json路径:/etc/gitlab/gitlab-secrets.json


## 备份其他文件(根据提示复制gitlab.rb + gitlab-secrets.json 两个文件到backup目录下):
cd /data/backups/gitlabs/
cp -rf /etc/gitlab/* ./

# 查看备份文件:
[root@bj-hom-130 ~]# ls /data/backups/gitlabs/
1678551126_2023_03_11_15.9.3_gitlab_backup.tar         # 1678551126_2023_03_11_15.9.3为事件戳

[root@bj-hom-130 ~]# date -d @1678551126
Sun Mar 12 00:12:06 CST 2023

二、 恢复

# 做一个有项目的备份:
/usr/bin/gitlab-rake gitlab:backup:create   

[root@bj-hom-130 gitlabs]# ll -al
-rw------- 1 git  git  419840 Mar 12 16:58 1678611534_2023_03_12_15.9.3_gitlab_backup.tar

# 把新建的项目删除:
*** 这个在删除之前 确认做过备份才可以.

## 恢复这个新的备份:
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
 
# 还原备份文件: 
gitlab-rake gitlab:backup:restore BACKUP=1678551126_2023_03_11_15.9.3

# 重启服务
gitlab-ctl restart

#  检查GitLab所有组件是否运行正常:
gitlab-rake gitlab:check SANITZE=true 
Gitlab恢复数据报错解决方法:
背景
在Gitlab迁移恢复数据出现must be owner of extension plpgsql解决方法;在做gitlab迁移时,按正常Gitlab备份数据gitlab-rake gitlab:backup:create,没问题,数据显示正常,但是在恢复的时候却出现报错。

Gitlab恢复数据出现错误提示:
[root@bj-hom-130 gitlabs]# gitlab-rake gitlab:backup:restore BACKUP=1678636407_2023_03_12_15.9.3
2023-03-12 16:15:12 UTC -- Unpacking backup ... 
2023-03-12 16:15:12 UTC -- Unpacking backup ... done
2023-03-12 16:15:12 UTC -- Restoring database ... 
2023-03-12 16:15:12 UTC -- Be sure to stop Puma, Sidekiq, and any other process that
connects to the database before proceeding. For Omnibus
installs, see the following link for more information:
https://docs.gitlab.com/ee/raketasks/backup_restore.html#restore-for-omnibus-gitlab-installations

Before restoring the database, we will remove all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.

Do you want to continue (yes/no)? yes
Removing all tables. Press `Ctrl-C` within 5 seconds to abort
2023-03-12 16:15:22 UTC -- Cleaning the database ... 
2023-03-12 16:15:25 UTC -- done
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
SET
SET
SET
SET
SET
 set_config 

## 解决方法:
1) 修改postgresql配置
$ 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

2)重启gitlab服务
$ gitlab-ctl restart
ok: run: logrotate: (pid 29367) 1s
ok: run: nginx: (pid 29371) 0s
ok: run: postgresql: (pid 29389) 0s
ok: run: redis: (pid 29391) 0s
ok: run: sidekiq: (pid 29404) 0s
ok: run: unicorn: (pid 29413) 0s

3) 修改gitlab账号为超级用户
su - gitlab-psql
/opt/gitlab/embedded/bin/psql -h 127.0.0.1 gitlabhq_production
psql (13.8)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

gitlabhq_production=# ALTER USER gitlab WITH SUPERUSER;
ALTER ROLE
gitlabhq_production=# \q

再次恢复数据已不报错

posted @ 2023-03-13 16:47  风满楼9527  阅读(998)  评论(0编辑  收藏  举报