gitlab服務有非常簡潔的每日備份命令,
從production的gitlab的每日備份中restore到backup環境也非常方便。
一、Production gitlab每日備份
1. Production gitlab環境上編寫腳本
cat /root/gitlab_bak.shgitlab-rake gitlab:backup:create? >? /var/opt/gitlab/backups/log/$(date +"%Y-%m-%d-%H:%M:%S").log
運行這個腳本,會生成tar包到gitlab配置文件中指定的backup路徑。
gitlab配置文件(/etc/gitlab/gitlab.rb)中配置backup路徑的部分如下:(默認路徑為/var/opt/gitlab/backups)
### Backup Settings
###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html# gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"?
2. 創建定時任務進行每日備份
0 2 * * * /root/gitlab_bak.sh
二、Backup的gitlab從每日備份中restore
1.?Backup gitlab環境上創建腳本
restore腳本如下:
cat gitlab_restore.sh#!/bin/bashbackup_file=$(ls -t /var/opt/gitlab/backups/*.tar|head -1)
restore_path=/var/opt/gitlab/backups
echo "$(date "+%Y-%m-%d %H:%M:%S") Start restoring from backup file: $backup_file"# stop db related services
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq# prepare the backupfiles to restore path
rm -rf ${restore_path}/*
cp ${backup_file} ${restore_path}/
chmod 777 -R ${restore_path}/*gitlab-rake gitlab:backup:restore force=yesgitlab-ctl reconfigure && gitlab-ctl restartrm -rf ${restore_path}/*echo "$(date "+%Y-%m-%d %H:%M:%S") End restoring, backup gitlab url: http://xxx"
需要把production備份的/var/opt/gitlab/backups/*.tar 拷貝到backup環境。
2. 運行腳本即可完成restore
注:
注意定期清理路徑:/var/opt/gitlab/git-data/repositories/+gitaly/tmp
這個路徑會占比較大的空間。可能會導致空間不足,restore失敗。