從GitLab12.1開始,可以直接使用rake進行備份,如果之前的專案中使用的gitlab-rake進行備份和恢復的方式是否可用這個觀點進行瞭如下實驗,通過本文進行簡單記錄。
容器化方式,直接使用docker-compose,啓動如下服務
[root@host131 gitlab]# cat docker-compose.yml
version: '2'
services:
# Version Control service: Gitlab
gitlab:
image: gitlab/gitlab-ce:12.10.5-ce.0
ports:
- "32001:80"
- "30022:22"
- "30443:443"
volumes:
- ./log/:/var/log/gitlab
- ./data/:/var/opt/gitlab
- ./conf/:/etc/gitlab
restart: "no"
[root@host131 gitlab]#
修改root使用者,並建立如下專案
使用rsync將各數據卷拷貝至備份目標機器,首先同步數據目錄
[root@host131 ~]# rsync -r gitlab/data 192.168.163.132:/root/gitlab/data
[email protected]'s password:
skipping non-regular file "data/gitaly/gitaly.socket"
skipping non-regular file "data/gitaly/internal_sockets/internal.sock"
skipping non-regular file "data/gitaly/internal_sockets/ruby.0"
skipping non-regular file "data/gitaly/internal_sockets/ruby.1"
skipping non-regular file "data/gitlab-rails/sockets/gitlab.socket"
skipping non-regular file "data/gitlab-workhorse/socket"
skipping non-regular file "data/grafana/conf"
skipping non-regular file "data/grafana/public"
skipping non-regular file "data/nginx/logs"
skipping non-regular file "data/postgresql/.s.PGSQL.5432"
skipping non-regular file "data/redis/redis.socket"
[root@host131 ~]#
注意事前需要在host132備份目標機器建立對應根目錄
[root@host132 ~]# pwd
/root
[root@host132 ~]# ls
anaconda-ks.cfg
[root@host132 ~]# mkdir gitlab
[root@host132 ~]# ls gitlab/
[root@host132 ~]#
不然同步數據的時候會出現類似如下錯誤提示資訊
[root@host131 ~]# rsync -r gitlab/data 192.168.163.132:/root/gitlab/data
[email protected]'s password:
rsync: mkdir "/root/gitlab/data" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(657) [Receiver=3.1.2]
[root@host131 ~]#
[root@host131 ~]# rsync -r gitlab/conf 192.168.163.132:/root/gitlab/conf
[email protected]'s password:
[root@host131 ~]#
[root@host131 ~]# rsync -r gitlab/log 192.168.163.132:/root/gitlab/log
[email protected]'s password:
skipping non-regular file "log/gitlab-rails/sidekiq.log"
[root@host131 ~]#
PS: 其實也不用這麼費勁,直接打個打的tar扔過去就可以了
使用docker-compose啓動之後,介面顯示如下,看起來和沒有數據的方式一樣
[root@host131 ~]# docker exec -it gitlab_gitlab_1 sh
# gitlab-rake gitlab:backup:create
2020-08-11 23:26:51 +0000 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2020-08-11 23:26:56 +0000 -- done
2020-08-11 23:26:56 +0000 -- Dumping repositories ...
* root/testproject (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [SKIPPED]
[SKIPPED] Wiki
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping uploads ...
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping builds ...
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping artifacts ...
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping pages ...
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping lfs objects ...
2020-08-11 23:26:57 +0000 -- done
2020-08-11 23:26:57 +0000 -- Dumping container registry images ...
2020-08-11 23:26:57 +0000 -- [DISABLED]
Creating backup archive: 1597188417_2020_08_11_12.10.5_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.
#
# cd /var/opt/gitlab/backups
# ls
1597188417_2020_08_11_12.10.5_gitlab_backup.tar
# du -k *.tar
172 1597188417_2020_08_11_12.10.5_gitlab_backup.tar
#
可以看到數據較少,只有170K,而且提示了一個Warning資訊,說一些包含敏感數據的檔案gitlab.rb和gitlab-secrets.json檔案不再備份之中,需要手動備份和恢復。
使用restore執行恢復,提示如下錯誤資訊:
[root@host132 backups]# docker exec -it gitlab_gitlab_1 sh
# cd /var/opt/gitlab/backups
# ls
1597188417_2020_08_11_12.10.5_gitlab_backup.tar
# gitlab-rake gitlab:backup:restore BACKUP=1597188417
The backup file 1597188417_gitlab_backup.tar does not exist!
#
這是因爲後續tar檔名的格式發生了變化,裏面加入了GitLab的版本號,所以之前的命令早不到用於恢復的備份檔案,修改檔名如下:
# cp 1597188417_2020_08_11_12.10.5_gitlab_backup.tar 1597188417_gitlab_backup.tar
#
在恢復之前需要首先停止服務
# gitlab-ctl stop
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-exporter: 1s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: grafana: 1s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: postgres-exporter: 0s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 1s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: sshd: 0s, normally up
ok: down: unicorn: 0s, normally up
#
匯入的時候需要注意許可權是否正確否則容易出現如下錯誤
# gitlab-rake gitlab:backup:restore BACKUP=1597188417
Unpacking backup ... tar: 1597188417_gitlab_backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
unpacking backup failed
#
這是因爲rsync同步時的檔案許可權受umask影響的預設設定,修改即可
# ls -lrt
total 344
-rw------- 1 root root 174080 Aug 12 01:23 1597188417_2020_08_11_12.10.5_gitlab_backup.tar
-rw------- 1 root root 174080 Aug 12 01:26 1597188417_gitlab_backup.tar
# chmod 644 *
#
執行備份恢復出現如下錯誤:
# gitlab-rake gitlab:backup:restore BACKUP=1597188417
Unpacking backup ... done
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
2020-08-12 01:28:07 +0000 -- Cleaning the database ...
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:27:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/backup.rake:64:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
Tasks: TOP => gitlab:db:drop_tables
(See full trace by running task with --trace)
#
提示資訊顯示無法連線數據庫,理論上來說把PostgreSQL啓動起來可以繼續,這裏乾脆全部啓動
# gitlab-ctl start
ok: run: alertmanager: (pid 3009) 1s
ok: run: gitaly: (pid 3017) 0s
ok: run: gitlab-exporter: (pid 3030) 1s
ok: run: gitlab-workhorse: (pid 3033) 0s
ok: run: grafana: (pid 3042) 1s
ok: run: logrotate: (pid 3050) 0s
ok: run: nginx: (pid 3056) 0s
ok: run: postgres-exporter: (pid 3062) 1s
ok: run: postgresql: (pid 3078) 0s
ok: run: prometheus: (pid 3088) 1s
ok: run: redis: (pid 3096) 0s
ok: run: redis-exporter: (pid 3101) 1s
ok: run: sidekiq: (pid 3106) 0s
ok: run: sshd: (pid 3112) 1s
ok: run: unicorn: (pid 3114) 0s
#
# gitlab-rake gitlab:backup:restore BACKUP=1597188417
Unpacking backup ... done
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
2020-08-12 02:27:25 +0000 -- Cleaning the database ...
2020-08-12 02:27:34 +0000 -- done
2020-08-12 02:27:34 +0000 -- Restoring database ...
Restoring PostgreSQL database gitlabhq_production ... SET
SET
SET
SET
SET
set_config
------------
(1 row)
SET
SET
SET
SET
ERROR: relation "public.u2f_registrations" does not exist
ERROR: relation "public.timelogs" does not exist
...省略
ALTER TABLE
[DONE]
2020-08-12 02:28:19 +0000 -- done
2020-08-12 02:28:19 +0000 -- Restoring repositories ...
* root/testproject ... [DONE]
2020-08-12 02:28:20 +0000 -- done
2020-08-12 02:28:20 +0000 -- Restoring uploads ...
2020-08-12 02:28:20 +0000 -- done
2020-08-12 02:28:20 +0000 -- Restoring builds ...
2020-08-12 02:28:20 +0000 -- done
2020-08-12 02:28:20 +0000 -- Restoring artifacts ...
2020-08-12 02:28:20 +0000 -- done
2020-08-12 02:28:20 +0000 -- Restoring pages ...
2020-08-12 02:28:20 +0000 -- done
2020-08-12 02:28:20 +0000 -- Restoring lfs objects ...
2020-08-12 02:28:20 +0000 -- done
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
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.
Restore task is done.
#
整個過程中數據庫數據匯入最開始提示了relation不不存在的錯誤和需要手工處理gitlab.rb等檔案的Warning,其餘均顯示正常完成。首先在目標備份機上的登錄,可以看到已經不再提示需要進行密碼修改了,輸入備份側的使用者名稱和密碼資訊點選登錄按鈕
可以看到剛剛在host131的GitLab服務上建立的工程專案資訊也已經成功恢復了