GitLab 简易指引(三):备份与恢复

本文为[原创]文章,转载请标明出处。
原文连接: https://weyunx.com/2019/01/30...
原文出自 微云的技术博客

GitLab 的备份工做主要包含配置文件备份和应用备份。git

配置文件备份

配置文件备份须要备份/etc/gitlab目录。web

# 压缩文件夹
sudo sh -c 'umask 0077; tar -cf $(date "+etc-gitlab-%s.tar") -C / etc/gitlab'

crontab中建立定时任务shell

sudo crontab -e -u root

新增一条:windows

# 天天1点执行
0 1 * * * umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab

也能够将语句写成脚本,经过脚本执行,好比备份的共享目录里。bash

强烈建议配置文件备份目录和应用备份目录分开!服务器

应用备份

GitLab 的备份命令以下:ide

# 本文的操做步骤仅适用于 Omnibus 一键安装方式安装的 GitLab,下同。
sudo gitlab-rake gitlab:backup:create

# 样例结果
Dumping database tables:
- Dumping table events... [DONE]
- Dumping table issues... [DONE]
- Dumping table keys... [DONE]
- Dumping table merge_requests... [DONE]
- Dumping table milestones... [DONE]
- Dumping table namespaces... [DONE]
- Dumping table notes... [DONE]
- Dumping table projects... [DONE]
- Dumping table protected_branches... [DONE]
- Dumping table schema_migrations... [DONE]
- Dumping table services... [DONE]
- Dumping table snippets... [DONE]
- Dumping table taggings... [DONE]
- Dumping table tags... [DONE]
- Dumping table users... [DONE]
- Dumping table users_projects... [DONE]
- Dumping table web_hooks... [DONE]
- Dumping table wikis... [DONE]
Dumping repositories:
- Dumping repository abcd... [DONE]
Creating backup archive: $TIMESTAMP_gitlab_backup.tar [DONE]
Deleting tmp directories...[DONE]
Deleting old backups... [SKIPPING]

执行后,备份的tar包放置在默认的备份目录/var/opt/gitlab/backups 下。gitlab

一样,咱们能够编辑/etc/gitlab/gitlab.rb来修改默认的备份目录。ui

# 默认的备份路径
gitlab_rails['backup_path'] = '/mnt/backups'

一样这里咱们强烈建议双机备份,官网提供了将备份上传到云以及上传到 mount 共享文件夹下,这里介绍一下上传到共享文件夹下的配置。 spa

好比我在本地 windows 环境下建立了一个共享文件夹gitlab_backups ,而后将文件夹挂载到服务器上:

# 在根目录下建立文件夹
mkdir gitlab_backups

# 挂载
mount -t cifs -o uid=996,gid=993,username=user,password=pass //22.189.30.101/gitlab_backups /gitlab_backups

其中uidgid是服务器上git用户的uidgid,若是不加上极可能会报权限异常。userpass就是你本地的用户名密码,后面的 ip 和目录就是本地的共享目录。

挂载成功后修改配置:

# 自动将备份文件上传
 gitlab_rails['backup_upload_connection'] = {
   :provider => 'Local',
   :local_root => '/gitlab_backups'
 }

 # 配置备份文件放至在挂载文件夹里的子目录名称,若是备份文件直接放至在挂载目录里,使用 ‘.’ 
 gitlab_rails['backup_upload_remote_directory'] = '.'

修改完成后执行sudo gitlab-ctl reconfigure使配置生效,此时再执行备份命令则会自动将备份文件复制到挂载的共享目录里。

一样,咱们能够加到定时任务中:

# 天天2点执行
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

备份文件清理

由于 GitLab 备份的文件较大,会占用过多的存储,咱们能够定时的进行清理,编辑 gitlab.rb,找到:

###! The duration in seconds to keep backups before they are allowed to be deleted
gitlab_rails['backup_keep_time'] = 604800

默认为保留7天,修改后执行 sudo gitlab-ctl reconfigure 便可。

GitLab 恢复

配置文件的恢复很简单,直接将备份文件替换,而后执行sudo gitlab-ctl reconfigure便可。

下面说一下应用备份的恢复:

首先是确认工做:

  • You have installed the exact same version and type (CE/EE) of GitLab Omnibus with which the backup was created.
  • You have run sudo gitlab-ctl reconfigure at least once.
  • GitLab is running. If not, start it using sudo gitlab-ctl start.

开始恢复:

# 复制备份文件
sudo cp 11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar /var/opt/gitlab/backups/

# 权限变动
sudo chown git.git /var/opt/gitlab/backups/11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar

# 中止服务
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

# 确认服务中止
sudo gitlab-ctl status

# 恢复
sudo gitlab-rake gitlab:backup:restore BACKUP=1493107454_2018_04_25_10.6.4-ce

# 重启和检查
sudo gitlab-ctl restart
sudo gitlab-rake gitlab:check SANITIZE=true

(完)

参考

相关文章
相关标签/搜索