对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。
备份
gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份时间戳
从gitlab 9.2版本开始,时间戳格式由EPOCH_YYYY_MM_DD更改为EPOCH_YYYY_MM_DD_Gitlab-version。
备份文件将保存在gitlab.yml文件中定义的backup_path中,文件名为TIMESTAMP_gitlab_backup.tar,TIMESTAMP为备份时的时间戳。
- 使用omnibus软件包安装的
sudo gitlab-rake gitlab:backup:create
- 使用源码安装的
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production
- 在docker中运行的gitlab
docker exec -t <container name> gitlab-rake gitlab:backup:create
备份策略选项
该选项对gitlab 8.17及以上版本有效。
默认的备份策略是使用linux的tar/gzip命令。这在大多数情况下是没有问题的,但是当数据在打包过程中发生改变时,将会有错误抛出file changed as we read it,这会导致备份进程失败。
位了解决这个问题,8.17引入了一个名为copy的备份策略,就是在调用tar、gzip时将数据拷贝到一个临时位置。不过也引入了另一个问题,将额外占用一倍的磁盘空间。
要使用复制策略而不是默认流策略,可以指定STRATEGY = copy。例如,sudo gitlab-rake gitlab:backup:create STRATEGY = copy。
排除特定目录
可以通过加环境变量skip来选择要备份的内容。可用的选项有:
-
db(数据库) -
uploads(附件) -
repositories(Git repositories 数据) -
builds(CI job output logs) -
artifacts(CI job artifacts) -
lfs(LFS objects) -
registry(Container Registry images) -
pages(Pages content)
指定多个选项使用逗号分隔。
omnibus版本安装
sudo gitlab-rake gitlab:backup:create SKIP=db,uploads
源码安装
sudo -u git -H bundle exec rake gitlab:backup:create SKIP=db,uploads RAILS_ENV=production
备份文件上传到云
这里就不介绍了,支持aws、google、openstack swift和rackspace。
上传到本地挂载目录
omnibus版本
gitlab_rails[\'backup_upload_connection\'] = {
:provider => \'Local\',
:local_root => \'/mnt/backups\'
}
# The directory inside the mounted folder to copy backups to
# Use \'.\' to store them in the root directory
gitlab_rails[\'backup_upload_remote_directory\'] = \'gitlab_backups\'
源码安装
backup: # snip upload: # Fog storage connection settings, see http://fog.io/storage/ . connection: provider: Local local_root: \'/mnt/backups\' # The directory inside the mounted folder to copy backups to # Use \'.\' to store them in the root directory remote_directory: \'gitlab_backups\'
备份配置文件
需要对/etc/gitlab/gitlab.rb 和 /etc/gitlab/gitlab-secrets.json(Omnibus), or /home/git/gitlab/config/secrets.yml (source)进行配置,来保存数据库加密秘钥。
使用crontab定时备份
omnibus版本
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
源码安装
# Create a full backup of the GitLab repositories and SQL database every day at 4am 0 4 * * * cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:create RAILS_ENV=production CRON=1
环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出。
建议将/etc/gitlab备份到安全的地方。如果要还原gitlab应用程序,还需要还原gitlab-secrets.json。如果没有,那么使用双重身份验证的GitLab用户将无法访问GitLab服务器,而存储在GitLab中的“安全变量”将被丢失。
omnibus 的所有配置都存储在/etc/gitlab中,只需备份此目录。
sudo sh -c \'umask 0077; tar -cf $(date \"+etc-gitlab-%s.tar\") /etc/gitlab\'
使用crontab
0 2 * * * umask 0077; tar cfz /secret/gitlab/backups/$(date \"+etc-gitlab-\\%s.tgz\") /etc/gitlab
服务器的ssh主机密钥存储在/etc/ssh/目录中,如果必须执行完整的服务器还原,请确保备份和还原这些密钥,以避免中间人攻击的警告。
恢复
只能还原到与备份文件相同的gitlab版本。
源码安装的
sudo service gitlab stop bundle exec rake gitlab:backup:restore RAILS_ENV=production
omnibus版本:
首先有安装与备份文件相同的gitlab。
执行gitlab-ctl reconfigure
如果gitlab没有运行,需执行gitlab-ctl start。
确保备份文件位于gitlab_rails[\’backup_path\’]。
sudo gitlab-ctl stop unicorn sudo gitlab-ctl stop sidekiq # Verify sudo gitlab-ctl status # This command will overwrite the contents of your GitLab database! sudo gitlab-rake gitlab:backup:restore BACKUP=1493107454_2017_04_25_9.1.0 sudo gitlab-ctl start sudo gitlab-rake gitlab:check SANITIZE=true

