返回列表 发布新帖

gitlab 备份

949 5
发表于 2024-4-10 23:35:21 | 显示全部楼层 阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×

根据官方文档,本手册统计了三种常用的备份方式。

  • 备份到本地备份
  • 备份至对象存储s3
  • 备份到NFS

无论上述哪种备份方式,在备份数据的时候,GitLab都会在默认本地备份路径先备份一份,再上传到remote远端存储中。

1. 准备

1.1 机器上要安装 rsync

Omnibus安装包自带rsync,不需要单独安装。

使用源码方式安装的gitlab需要单独安装 rsync,比如 yum install rsync

1.2 会备份的内容:

  • Database
  • Attachments
  • Git repositories data
  • CI/CD job output logs
  • CI/CD job artifacts
  • LFS objects
  • Terraform states (introduced in GitLab 14.7)
  • Container Registry images
  • GitLab Pages content
  • Packages (introduced in GitLab 14.7)
  • Snippets
  • Group wikis

1.3 不会备份的内容

  • Mattermost data
  • Redis (and thus Sidekiq jobs)
  • 配置信息(gitlab-secrets.json,gitlab.rb等)

2. 本地备份

2.1 备份配置信息

/etc/gitlab/gitlab.rb中可以找到如下backup设置信息,默认是将备份保存在 /var/opt/gitlab/backups目录下,可以将其设置成其它路径。

## Backup settings
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"

backup_path 标识了备份文件所在的目录,触发备份时,会生成格式为:[TIMESTAMP]_[GITLAB_VERSION]_gitlab_backup.tar的备份文件。例如:

vagrant@gitlab-ee-13-12:/var/opt/gitlab$ sudo ls -lh backups/
total 3.4M
-rw------- 1 git git 3.4M Aug 10 13:02 1628571776_2021_08_10_13.12.9-ee_gitlab_backup.tar

2.2 备份数据

进入http://your_gitlab_ip/admin,查看当前Gitlab版本。

  • 如果是GitLab 12.2 或更新版本:

    sudo gitlab-backup create
  • 如果是GitLab 12.1 或更旧版本:

    gitlab-rake gitlab:backup:create
  • 15.5.0之前,备份程序不会检查当前是否有其他备份程序在运行中,强烈建议一次只运行一个备份程序。

输出示例:

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]

2.2.1 备份选项

  • 备份策略 COPY策略用于避免打tar包时数据发生变化,拷贝副本后再打包。常见策略举例:

    # 避免tar失败
    # 数据备份, STRATEGY=copy的作用是先把数据从相应位置汇总拷贝到一个临时位置,这样在tar的时候就是静态数据,避免在tar打包的过程中文件发生改变,而导致tar失败。
    # 不好的地方在于一倍的存储空间,因此此功能默认不开启,需要手工指定。
    gitlab-backup create STRATEGY=copy 
    
    # 避免时间戳文件名
    # 不想使用时间戳作为文件名,而是自定义:
    sudo gitlab-backup create BACKUP=dump 
    # 文件名:dump_gitlab_backup.tar
    
    # 确认tar包可以被rsync传送
    sudo gitlab-backup create BACKUP=dump GZIP_RSYNCABLE=yes
    
    # 避免使用压缩包。直接备份到文件,实际生成一个个小的压缩包,而不是大的压缩包。
    sudo gitlab-backup create SKIP=tar
  • 跳过某些数据类型 用于只备份某些类型的数据,比如:

    gitlab-backup create SKIP=db,uploads

    更多参数如下:

    • db (database)
    • uploads (attachments)
    • builds (CI job output logs)
    • artifacts (CI job artifacts)
    • lfs (LFS objects)
    • terraform_state (Terraform states)
    • registry (Container Registry images)
    • pages (Pages content)
    • repositories (Git repositories data)
    • packages (Packages)
  • 并发备份。13.3之后的版本支持并发备份,14.3之后支持并发恢复。

更多的备份选项参见备份选项.

2.2.2 硬盘空间占用

不同的备份策略和参数,会占用不同的空间大小。比如:

  • gitlab-backup create 最多同时有三份数据:本身、小包、大包
  • gitlab-backup create STRATEGY=copy 最多同时有三份数据:本身、副本、小包 ----> 本身、小包、大包
  • gitlab-backup create SKIP=tar 最多同时有两份数据:本身、小包
  • gitlab-backup create STRATEGY=copy SKIP=tar 最多同时有三份数据:本身、副本、小包

补充说明一下,以上的 小包,指的是各个数据类型的压缩包,大包指的是备份完成后生成的统一的压缩包。

2.3 备份配置

在Omnibus GitLab 中所有的配置保存在/etc/gitlab。

配置文件包括:

  • GitLab two-factor authentication (2FA) user secrets (‘QR codes’)
  • GitLab CI ‘secure variables’

要备份所有配置,请执行:

sudo gitlab-ctl backup-etc <DIRECTORY>

若不指定DIRECTORY,GitLab会自动创建/etc/gitlab/config_backup/ 保存配置的备份文件。

注意:请不要将配置的备份与数据的备份保存在一起,以免数据泄露。如果早期版本没有上述命令,请手工备份/etc/gitlab目录下文件

tar -cvf gitlab-config.tar /etc/gitlab/

例子:

# sudo gitlab-ctl backup-etc
Could not find '/etc/gitlab/config_backup' directory. Creating.
Running configuration backup
Creating configuration backup archive: gitlab_config_1613725002_2021_02_19.tar
/etc/gitlab/
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/gitlab.rb
/etc/gitlab/trusted-certs/
Configuration backup archive complete: /etc/gitlab/config_backup/gitlab_config_1613725002_2021_02_19.tar

2.4 备份SSH

机器的SSH主机密钥保存在/etc/ssh/中。如果需要执行整个机器的还原,请备份这些密钥

3. 远程备份

GitLab支持使用对象存储服务来保存多种类型的数据。下面分别以常见的S3(minio)和NFS举例。

3.1 S3

3.1.1 修改配置信息

添加以下配置到 /etc/gitlab/gitlab.rb

gitlab_rails['backup_upload_connection'] = {
  'provider' => 'AWS',
  'aws_access_key_id' => 'YOUR-ACCESS-KEY-OR-USERNAME-HERE',
  'aws_secret_access_key' => 'YOUR-SECRET-KEY-HERE',
  'endpoint' => 'https://minio.yourdomain.com:9000',
  'path_style' => true
}
gitlab_rails['backup_upload_remote_directory'] = 'backup'

为了使配置生效需要再执行以下命令:

gitlab-ctl reconfigure

3.1.2 备份数据

2.2 备份数据

3.1.3 备份配置

2.3 备份配置

3.1.4 备份SSH

2.4 备份SSH

3.2 NFS

3.2.1 修改配置信息

添加以下配置到 /etc/gitlab/gitlab.rb

gitlab_rails['backup_upload_connection'] = {
  :provider => 'Local',
  :local_root => '/mnt/nfs_clientshare'
}

# 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'

为了使配置生效需要再执行以下命令:

gitlab-ctl reconfigure

3.2.2 备份数据

2.2 备份数据

3.2.3 备份配置

2.3 备份配置

3.2.4 备份SSH

2.4 备份SSH

Troubleshooting

Excon::Error::MethodNotAllowed: Expected(200) <=> Actual(405 Method Not Allowed)

这个问题出现在minio client和minio server端之间存在代理的情况下。 除了去掉代理外,还未尝试其它解决方法。出现该错误的原因应该跟代理的设置有关。

回复

使用道具 举报

评论5

岁岁欢Lv.1 发表于 2024-4-11 14:29:46 来自手机 | 显示全部楼层
请问现在gitlab支持增量备份吗
回复

使用道具 举报

机器人168号Lv.2 发表于 2024-8-16 14:29:31 | 显示全部楼层
回复

使用道具 举报

冀利斌ᵇᵉᵗᵗᵉʳLv.2 发表于 2024-8-18 23:06:33 | 显示全部楼层
赞赞赞!
回复

使用道具 举报

Cloud-NativeLv.1 发表于 2024-9-1 07:29:56 | 显示全部楼层
岁岁欢 发表于 2024-4-11 14:29
请问现在gitlab支持增量备份吗

支持的,官方文档有记录,前段时间刚实现
回复

使用道具 举报

穹岚Lv.1 发表于 2024-9-24 18:03:33 | 显示全部楼层
请问有没有大佬帮忙看下这个问题怎么解决啊 1727172095657.jpg
回复

使用道具 举报

意见建议

Email:forum@gitlab.cn
  • 关注公众号
  • 添加专业顾问
Copyright © 2001-2024 极狐GitLab论坛 版权所有 All Rights Reserved. 鄂ICP备2021008419号-1|鄂公网安备42018502006137号
关灯 快速发帖
扫一扫添加专业顾问
返回顶部
快速回复 返回顶部 返回列表