Gitlab備份和恢復操作記錄

散盡浮華發表於2017-11-12

 

前面已經介紹了Gitlab環境部署記錄,這裡簡單說下Gitlab的備份和恢復操作記錄:

1)Gitlab的備份目錄路徑設定

[root@code-server ~]# vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups"    //gitlab備份目錄
gitlab_rails['backup_archive_permissions'] = 0644       //生成的備份檔案許可權
gitlab_rails['backup_keep_time'] = 7776000              //備份保留天數為3個月(即90天,這裡是7776000秒)

[root@code-server ~]# mkdir -p /data/gitlab/backups
[root@code-server ~]# chown -R git.git /data/gitlab/backups
[root@code-server ~]# chmod -R 777 /data/gitlab/backups
 
如上設定了gitlab備份目錄路徑為/data/gitlab/backups,最後使用下面命令過載gitlab配置檔案,是上述修改生效!
root@code-server ~]# gitlab-ctl reconfigure

2)GItlab備份操作(使用備份命令"gitlab-rake gitlab:backup:create")

手動備份gitlab
[root@code-server backups]# gitlab-rake gitlab:backup:create
Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
 * treesign/treesign ... [DONE]
 * gateway/gateway ... [DONE]
 * treesign/treesign-doc ... [SKIPPED]
 * qwsign/qwsign ... [DONE]
 * qwsign/qwsign-doc ... [DONE]
 * test/test ... [DONE]
done
Dumping uploads ... 
done
Dumping builds ... 
done
Dumping artifacts ... 
done
Dumping pages ... 
done
Dumping lfs objects ... 
done
Dumping container registry images ... 
[DISABLED]
Creating backup archive: 1510471890_2017_11_12_9.4.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 ... done. (0 removed)

然後檢視下備份檔案(檔案許可權是設定好的644)
[root@code-server backups]# ll
total 244
-rw-r--r-- 1 git git 245760 Nov 12 15:33 1510472027_2017_11_12_9.4.5_gitlab_backup.tar

編寫備份指令碼,結合crontab實施自動定時備份,比如每天0點、6點、12點、18點各備份一次
[root@code-server backups]# pwd
/data/gitlab/backups
[root@code-server backups]# vim gitlab_backup.sh 
#!/bin/bash
/usr/bin/gitlab-rake gitlab:backup:create CRON=1

注意:環境變數CRON=1的作用是如果沒有任何錯誤發生時, 抑制備份指令碼的所有進度輸出

[root@code-server backups]# crontab -l
0 0,6,12,18 * * * /bin/bash -x /data/gitlab/backups/gitlab_backup.sh > /dev/null 2>&1

3)Gitlab恢復操作

GItlab只能還原到與備份檔案相同的gitlab版本。
假設在上面gitlab備份之前建立了test專案,然後不小心誤刪了test專案,現在就進行gitlab恢復操作:
 
1)停止相關資料連線服務
[root@code-server backups]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@code-server backups]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
[root@code-server backups]# gitlab-ctl status
run: gitaly: (pid 98087) 1883s; run: log: (pid 194202) 163003s
run: gitlab-monitor: (pid 98101) 1883s; run: log: (pid 194363) 163002s
run: gitlab-workhorse: (pid 98104) 1882s; run: log: (pid 194362) 163002s
run: logrotate: (pid 98117) 1882s; run: log: (pid 5793) 160832s
run: nginx: (pid 98123) 1881s; run: log: (pid 194359) 163002s
run: node-exporter: (pid 98167) 1881s; run: log: (pid 194360) 163002s
run: postgres-exporter: (pid 98173) 1881s; run: log: (pid 194204) 163003s
run: postgresql: (pid 98179) 1880s; run: log: (pid 194365) 163002s
run: prometheus: (pid 98187) 1880s; run: log: (pid 194364) 163002s
run: redis: (pid 98230) 1879s; run: log: (pid 194358) 163002s
run: redis-exporter: (pid 98234) 1879s; run: log: (pid 194208) 163003s
down: sidekiq: 8s, normally up; run: log: (pid 194437) 163001s
down: unicorn: 21s, normally up; run: log: (pid 194443) 163001s
 
2)現在通過之前的備份檔案進行恢復(必須要備份檔案放到備份路徑下,這裡備份路徑我自定義的/data/gitlab/backups,預設的是/var/opt/gitlab/backups)
[root@code-server backups]# pwd
/data/gitlab/backups
[root@code-server backups]# ll
total 244
-rw-r--r-- 1 git git 245760 Nov 12 15:33 1510472027_2017_11_12_9.4.5_gitlab_backup.tar
 
Gitlab的恢復操作會先將當前所有的資料清空,然後再根據備份資料進行恢復
[root@code-server backups]# gitlab-rake gitlab:backup:restore BACKUP=1510472027_2017_11_12_9.4.5
Unpacking backup ... done
Before restoring the database we recommend removing 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)?
........
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
WARNING:  no privileges were granted for "public"
GRANT
[DONE]
done
Restoring repositories ...
 * treesign/treesign ... [DONE]
 * gateway/gateway ... [DONE]
 * treesign/treesign-doc ... [DONE]
 * qwsign/qwsign ... [DONE]
 * qwsign/qwsign-doc ... [DONE]
 * test/test ... [DONE]
Put GitLab hooks in repositories dirs [DONE]
done
Restoring uploads ...
done
Restoring builds ...
done
Restoring artifacts ...
done
Restoring pages ...
done
Restoring lfs objects ...
done
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes
 
 
Deleting tmp directories ... done
done
done
done
done
done
done
done
[root@code-server backups]#
 
最後再次啟動Gitlab
[root@code-server backups]# gitlab-ctl start
ok: run: gitaly: (pid 98087) 2138s
ok: run: gitlab-monitor: (pid 98101) 2138s
ok: run: gitlab-workhorse: (pid 98104) 2137s
ok: run: logrotate: (pid 98117) 2137s
ok: run: nginx: (pid 98123) 2136s
ok: run: node-exporter: (pid 98167) 2136s
ok: run: postgres-exporter: (pid 98173) 2136s
ok: run: postgresql: (pid 98179) 2135s
ok: run: prometheus: (pid 98187) 2135s
ok: run: redis: (pid 98230) 2134s
ok: run: redis-exporter: (pid 98234) 2134s
ok: run: sidekiq: (pid 104494) 0s
ok: run: unicorn: (pid 104497) 1s
[root@code-server backups]# gitlab-ctl status
run: gitaly: (pid 98087) 2142s; run: log: (pid 194202) 163262s
run: gitlab-monitor: (pid 98101) 2142s; run: log: (pid 194363) 163261s
run: gitlab-workhorse: (pid 98104) 2141s; run: log: (pid 194362) 163261s
run: logrotate: (pid 98117) 2141s; run: log: (pid 5793) 161091s
run: nginx: (pid 98123) 2140s; run: log: (pid 194359) 163261s
run: node-exporter: (pid 98167) 2140s; run: log: (pid 194360) 163261s
run: postgres-exporter: (pid 98173) 2140s; run: log: (pid 194204) 163262s
run: postgresql: (pid 98179) 2139s; run: log: (pid 194365) 163261s
run: prometheus: (pid 98187) 2139s; run: log: (pid 194364) 163261s
run: redis: (pid 98230) 2138s; run: log: (pid 194358) 163261s
run: redis-exporter: (pid 98234) 2138s; run: log: (pid 194208) 163262s
run: sidekiq: (pid 104494) 4s; run: log: (pid 194437) 163260s
run: unicorn: (pid 104497) 4s; run: log: (pid 194443) 163260s

恢復命令完成後,可以check檢查一下恢復情況
[root@code-server backups]# gitlab-rake gitlab:check SANITIZE=true
Checking GitLab Shell ...

GitLab Shell version >= 5.3.1 ? ... OK (5.3.1)
Repo base directory exists?
default... yes
Repo storage directories are symlinks?
default... no
Repo paths owned by git:root, or git:git?
default... yes
Repo paths access is drwxrws---?
default... yes
hooks directories in repos are links: ... 
5/1 ... ok
6/2 ... ok
5/3 ... repository is empty
12/4 ... ok
12/5 ... ok
Running /opt/gitlab/embedded/service/gitlab-shell/bin/check
Check GitLab API access: OK
Access to /var/opt/gitlab/.ssh/authorized_keys: OK
Send ping to redis server: OK
gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes
Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Checking Reply by email ...

Reply by email is disabled in config/gitlab.yml

Checking Reply by email ... Finished

Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Git configured correctly? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config up to date? ... yes
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory exists? ... yes
Uploads directory has correct permissions? ... yes
Uploads directory tmp has correct permissions? ... yes
Init script exists? ... skipped (omnibus-gitlab has no init script)
Init script up-to-date? ... skipped (omnibus-gitlab has no init script)
Projects have namespace: ... 
5/1 ... yes
6/2 ... yes
5/3 ... yes
12/4 ... yes
12/5 ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.3.3 ? ... yes (2.3.3)
Git version >= 2.7.3 ? ... yes (2.13.4)
Active users: ... 11

Checking GitLab ... Finished
 
然後稍等一會(如果啟動gitlab後,訪問出現500,這是因為redis等程式還沒完全啟動,等一會兒訪問就ok了),再次登入Gitlab,就會發現之前誤刪除的test專案已經恢復了!
 
另外:Gitlab遷移與恢復一樣,但是要求兩個GitLab版本號一致

相關文章