上传文件迁移 Rake 任务

有一个用于在不同存储类型之间迁移上传的 Rake 任务。

迁移到对象存储

为上传文件配置对象存储后,使用此任务将现有上传文件从本地存储迁移到远程存储。

所有处理都在后台 worker 中完成,并且无停机时间

多合一的 Rake 任务

GitLab 提供了一个包装器 Rake 任务,可以一步将所有上传的文件(例如头像、徽标、附件和网站图标)迁移到对象存储。包装器任务调用单独的 Rake 任务来逐一迁移属于这些类别中的每一个文件。

单独的 Rake 任务文档在下一部分。

要将所有上传文件从本地存储迁移到对象存储,请运行:

Omnibus 安装实例

gitlab-rake "gitlab:uploads:migrate:all"

源安装实例

sudo RAILS_ENV=production -u git -H bundle exec rake gitlab:uploads:migrate:all

您可以选择使用 PostgreSQL 控制台 跟踪进度并验证所有包是否成功迁移:

  • 用于 Omnibus GitLab 实例:sudo gitlab-rails dbconsole
  • 用于源安装实例:sudo -u git -H psql -d gitlabhq_production

验证下面的objectstg(其中store=2)具有所有产物的计数:

gitlabhq_production=# SELECT count(*) AS total, sum(case when store = '1' then 1 else 0 end) AS filesystem, sum(case when store = '2' then 1 else 0 end) AS objectstg FROM uploads;

total | filesystem | objectstg
------+------------+-----------
   2409 |          0 |      2409

验证 uploads 文件夹中的磁盘上没有文件:

sudo find /var/opt/gitlab/gitlab-rails/uploads -type f | grep -v tmp | wc -l

单独的 Rake 任务

如果您已运行多合一的 Rake 任务,无需运行以下单独的任务。

Rake 任务使用三个参数来查找要迁移的上传文件:

参数 类型 描述
uploader_class string 要从中迁移的上传者的类型。
model_class string 要从中迁移的模型的类型。
mount_point string/symbol 上传器所挂载的模型列的名称。
note这些参数主要是极狐GitLab 结构的内部参数,您可能需要参考下面的任务列表。运行这些单独的任务后,我们建议您运行多合一的 rake 任务,迁移未包含在所列类型中的任何上传。

此任务还接受一个环境变量,您可以使用它来覆盖默认批量大小:

变量 类型 描述
BATCH integer 指定批次的大小。默认为 200。

下面显示了如何为各种上传类型运行 gitlab:uploads:migrate

Omnibus 安装实例

# gitlab-rake gitlab:uploads:migrate[uploader_class, model_class, mount_point]

# Avatars
gitlab-rake "gitlab:uploads:migrate[AvatarUploader, Project, :avatar]"
gitlab-rake "gitlab:uploads:migrate[AvatarUploader, Group, :avatar]"
gitlab-rake "gitlab:uploads:migrate[AvatarUploader, User, :avatar]"

# Attachments
gitlab-rake "gitlab:uploads:migrate[AttachmentUploader, Note, :attachment]"
gitlab-rake "gitlab:uploads:migrate[AttachmentUploader, Appearance, :logo]"
gitlab-rake "gitlab:uploads:migrate[AttachmentUploader, Appearance, :header_logo]"

# Favicon
gitlab-rake "gitlab:uploads:migrate[FaviconUploader, Appearance, :favicon]"

# Markdown
gitlab-rake "gitlab:uploads:migrate[FileUploader, Project]"
gitlab-rake "gitlab:uploads:migrate[PersonalFileUploader, Snippet]"
gitlab-rake "gitlab:uploads:migrate[NamespaceFileUploader, Snippet]"
gitlab-rake "gitlab:uploads:migrate[FileUploader, MergeRequest]"

# Design Management design thumbnails
gitlab-rake "gitlab:uploads:migrate[DesignManagement::DesignV432x230Uploader, DesignManagement::Action, :image_v432x230]"

源安装实例

对每个任务使用 RAILS_ENV=production

# sudo -u git -H bundle exec rake gitlab:uploads:migrate

# Avatars
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AvatarUploader, Project, :avatar]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AvatarUploader, Group, :avatar]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AvatarUploader, User, :avatar]"

# Attachments
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AttachmentUploader, Note, :attachment]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AttachmentUploader, Appearance, :logo]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[AttachmentUploader, Appearance, :header_logo]"

# Favicon
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[FaviconUploader, Appearance, :favicon]"

# Markdown
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[FileUploader, Project]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[PersonalFileUploader, Snippet]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[NamespaceFileUploader, Snippet]"
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[FileUploader, MergeRequest]"

# Design Management design thumbnails
sudo -u git -H bundle exec rake "gitlab:uploads:migrate[DesignManagement::DesignV432x230Uploader, DesignManagement::Action]"

迁移到本地存储

如果您出于任何原因需要禁用对象存储,您必须首先将数据从对象存储迁移回本地存储。

caution需要延长停机时间,因此迁移期间不会在对象存储中创建新文件。

多合一的 Rake 任务

极狐GitLab 提供了一个包装器 Rake 任务,可以一步将所有上传的文件(例如,头像、徽标、附件和网站图标)迁移到本地存储。包装器任务调用单独的 Rake 任务来逐一迁移属于这些类别中的每一个的文件。

有关这些 Rake 任务的详细信息,请参阅 单独的 Rake 任务,记住本例中的任务名称是 gitlab:uploads:migrate_to_local

要将上传文件从对象存储迁移到本地存储:

  1. gitlab.rbuploads 设置下禁用 direct_uploadbackground_upload

    gitlab_rails['uploads_object_store_direct_upload'] = false
    gitlab_rails['uploads_object_store_background_upload'] = false
    

    保存文件并重新配置极狐GitLab

  2. 运行 Rake 任务:

    Omnibus 安装实例

    gitlab-rake "gitlab:uploads:migrate_to_local:all"
    

    源安装实例

    sudo RAILS_ENV=production -u git -H bundle exec rake gitlab:uploads:migrate_to_local:all
    

运行 Rake 任务后,您可以通过撤消配置对象存储说明中描述的更改来禁用对象存储。