gitlab备份和恢复
环境:
Os:Centos 7
gitlab:17
1.执行备份命令
前提是gitlab要正常运行的状态下执行
[root@node1 ~]# gitlab-backup create
2025-03-21 02:45:08 UTC -- Dumping database ...
2025-03-21 02:45:08 UTC -- Dumping PostgreSQL database gitlabhq_production ...
2025-03-21 02:45:18 UTC -- [DONE]
2025-03-21 02:45:18 UTC -- Dumping database ... done
2025-03-21 02:45:18 UTC -- Dumping repositories ...
{"command":"create","gl_project_path":"root/project_test","level":"info","msg":"started create","pid":4485,"relative_path":"@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git","storage_name":"default","time":"2025-03-21T02:45:19.296Z"}
{"command":"create","gl_project_path":"root/project_test","level":"info","msg":"completed create","pid":4485,"relative_path":"@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git","storage_name":"default","time":"2025-03-21T02:45:19.792Z"}
{"command":"create","gl_project_path":"root/project_test.wiki","level":"info","msg":"started create","pid":4485,"relative_path":"@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki.git","storage_name":"default","time":"2025-03-21T02:45:19.792Z"}
{"command":"create","gl_project_path":"root/project_test.wiki","level":"info","msg":"completed create","pid":4485,"relative_path":"@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki.git","storage_name":"default","time":"2025-03-21T02:45:19.862Z"}
2025-03-21 02:45:19 UTC -- Dumping repositories ... done
2025-03-21 02:45:19 UTC -- Dumping uploads ...
2025-03-21 02:45:19 UTC -- Dumping uploads ... done
2025-03-21 02:45:19 UTC -- Dumping builds ...
2025-03-21 02:45:19 UTC -- Dumping builds ... done
2025-03-21 02:45:19 UTC -- Dumping artifacts ...
2025-03-21 02:45:19 UTC -- Dumping artifacts ... done
2025-03-21 02:45:19 UTC -- Dumping pages ...
2025-03-21 02:45:19 UTC -- Dumping pages ... done
2025-03-21 02:45:19 UTC -- Dumping lfs objects ...
2025-03-21 02:45:19 UTC -- Dumping lfs objects ... done
2025-03-21 02:45:19 UTC -- Dumping terraform states ...
2025-03-21 02:45:19 UTC -- Dumping terraform states ... done
2025-03-21 02:45:19 UTC -- Dumping container registry images ... [DISABLED]
2025-03-21 02:45:19 UTC -- Dumping packages ...
2025-03-21 02:45:19 UTC -- Dumping packages ... done
2025-03-21 02:45:19 UTC -- Dumping ci secure files ...
2025-03-21 02:45:20 UTC -- Dumping ci secure files ... done
2025-03-21 02:45:20 UTC -- Dumping external diffs ...
2025-03-21 02:45:20 UTC -- Dumping external diffs ... done
2025-03-21 02:45:20 UTC -- Creating backup archive: 1742525108_2025_03_21_17.1.1_gitlab_backup.tar ...
2025-03-21 02:45:20 UTC -- Creating backup archive: 1742525108_2025_03_21_17.1.1_gitlab_backup.tar ... done
2025-03-21 02:45:20 UTC -- Uploading backup archive to remote storage ... [SKIPPED]
2025-03-21 02:45:20 UTC -- Deleting old backups ... [SKIPPED]
2025-03-21 02:45:20 UTC -- Deleting tar staging files ...
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/db
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/repositories
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/ci_secure_files.tar.gz
2025-03-21 02:45:20 UTC -- Cleaning up /var/opt/gitlab/backups/external_diffs.tar.gz
2025-03-21 02:45:20 UTC -- Deleting tar staging files ... done
2025-03-21 02:45:20 UTC -- Deleting backups/tmp ...
2025-03-21 02:45:20 UTC -- Deleting backups/tmp ... done
2025-03-21 02:45:20 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.
2025-03-21 02:45:20 UTC -- Backup 1742525108_2025_03_21_17.1.1 is done.
2025-03-21 02:45:20 UTC -- Deleting backup and restore PID file ... done
2.查看备份文件
[root@node1 ~]# cd /var/opt/gitlab/backups
[root@node1 backups]# ls
1742525108_2025_03_21_17.1.1_gitlab_backup.tar
备份目录可以从配置文件中查看
[root@node1 backups]# cat /etc/gitlab/gitlab.rb|grep backup_path
# gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
# gitlab_rails['backup_gitaly_backup_path'] = "/opt/gitlab/embedded/bin/gitaly-backup"
3.指定备份名称备份
[root@node1 backups]#gitlab-backup create BACKUP=dump20250321
[root@node1 backups]# ls -al
total 524
drwx------ 2 git root 44 Mar 21 10:57 .
drwxr-xr-x 21 root root 4096 Mar 19 17:23 ..
-rw------- 1 git git 532480 Mar 21 10:57 dump20250321_gitlab_backup.tar
[root@node1 backups]# pwd
/var/opt/gitlab/backups
这个时候名字就以备份时的名称命名了
4.GitLab 原地恢复
模拟删除项目

1.停止服务
gitlab-ctl stop puma
gitlab-ctl stop sidekiq
其他服务无需停止
2.恢复
[root@node1 backups]# gitlab-backup restore BACKUP=dump20250321_gitlab_backup
2025-03-21 03:21:37 UTC -- The backup file dump20250321_gitlab_backup_gitlab_backup.tar does not exist!
2025-03-21 03:21:37 UTC -- Deleting backup and restore PID file ... done
<备份文件前缀> 为文件名的时间戳部分,例如 1672232546,或是备份时指定的名称如:BACKUP=dump20250321
恢复时会提示确认覆盖现有数据,输入 yes 确认.
[root@node1 backups]# gitlab-backup restore BACKUP=dump20250321
2025-03-21 03:23:03 UTC -- Unpacking backup ...
2025-03-21 03:23:03 UTC -- Unpacking backup ... done
2025-03-21 03:23:03 UTC -- Restoring database ...
2025-03-21 03:23:03 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:
http://192.168.1.105:8888/help/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
2025-03-21 03:23:19 UTC -- Removing all tables. Press `Ctrl-C` within 5 seconds to abort
2025-03-21 03:23:24 UTC -- Cleaning the database ...
2025-03-21 03:23:30 UTC -- done
2025-03-21 03:23:30 UTC -- 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
This task will now rebuild the authorized_keys file.
You will lose any data stored in the authorized_keys file.
Do you want to continue (yes/no)? yes
2025-03-21 03:24:31 UTC -- Deleting tar staging files ...
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/db
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/repositories
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/ci_secure_files.tar.gz
2025-03-21 03:24:31 UTC -- Cleaning up /var/opt/gitlab/backups/external_diffs.tar.gz
2025-03-21 03:24:31 UTC -- Deleting tar staging files ... done
2025-03-21 03:24:31 UTC -- Deleting backups/tmp ...
2025-03-21 03:24:31 UTC -- Deleting backups/tmp ... done
2025-03-21 03:24:31 UTC -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need to restore these files manually.
2025-03-21 03:24:31 UTC -- Restore task is done.
2025-03-21 03:24:31 UTC -- Deleting backup and restore PID file ... done
3.启动服务
[root@node1 backups]#gitlab-ctl restart
说明:
1.异地恢复的话需要将配置文件拷贝到目的机器 /etc/gitlab/gitlab.rb
浙公网安备 33010602011771号