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