清理 Rake 任务
- Tier: Free, Premium, Ultimate
- Offering: GitLab Self-Managed
GitLab 提供了用于清理 GitLab 实例的 Rake 任务。
移除未引用的 LFS 文件
请勿在 GitLab 升级后的 12 小时内运行此任务。这是为了确保所有后台迁移已完成,否则可能导致数据丢失。
当您从仓库历史记录中移除 LFS 文件后,这些文件会变为孤立状态并持续占用磁盘空间。通过此 Rake 任务,您可以移除数据库中的无效引用,从而允许对 LFS 文件进行垃圾回收。例如:
sudo gitlab-rake gitlab:cleanup:orphan_lfs_file_references PROJECT_PATH="gitlab-org/gitlab-foss"bundle exec rake gitlab:cleanup:orphan_lfs_file_references RAILS_ENV=production PROJECT_PATH="gitlab-org/gitlab-foss"您也可以使用 PROJECT_ID 替代 PROJECT_PATH 来指定项目。
例如:
$ sudo gitlab-rake gitlab:cleanup:orphan_lfs_file_references PROJECT_ID="13083"
I, [2019-12-13T16:35:31.764962 #82356] INFO -- : Looking for orphan LFS files for project GitLab Org / GitLab Foss
I, [2019-12-13T16:35:31.923659 #82356] INFO -- : Removed invalid references: 12默认情况下,此任务不会删除任何内容,仅显示可删除的文件引用数量。若要实际删除引用,请使用 DRY_RUN=false 运行命令。您还可以使用 LIMIT={number} 参数限制删除的引用数量。
此 Rake 任务仅移除对 LFS 文件的引用。未引用的 LFS 文件稍后会被垃圾回收(每日一次)。如需立即回收,请运行下文描述的 rake gitlab:cleanup:orphan_lfs_files。
立即移除未引用的 LFS 文件
未引用的 LFS 文件每日会被清理,但您也可以根据需要立即移除。要立即移除未引用的 LFS 文件:
sudo gitlab-rake gitlab:cleanup:orphan_lfs_filesbundle exec rake gitlab:cleanup:orphan_lfs_files示例输出:
$ sudo gitlab-rake gitlab:cleanup:orphan_lfs_files
I, [2020-01-08T20:51:17.148765 #43765] INFO -- : Removed unreferenced LFS files: 12清理项目上传文件
清理 GitLab 数据库中不存在的项目上传文件。
从文件系统清理项目上传文件
清理本地项目中 GitLab 数据库不存在的上传文件。该任务会尝试修复能找到对应项目的文件,否则将文件移至失物招领目录。要从文件系统清理项目上传文件:
sudo gitlab-rake gitlab:cleanup:project_uploadsbundle exec rake gitlab:cleanup:project_uploads RAILS_ENV=production示例输出:
$ sudo gitlab-rake gitlab:cleanup:project_uploads
I, [2018-07-27T12:08:27.671559 #89817] INFO -- : Looking for orphaned project uploads to clean up. Dry run...
D, [2018-07-27T12:08:28.293568 #89817] DEBUG -- : Processing batch of 500 project upload file paths, starting with /opt/gitlab/embedded/service/gitlab-rails/public/uploads/test.out
I, [2018-07-27T12:08:28.689869 #89817] INFO -- : Can move to lost and found /opt/gitlab/embedded/service/gitlab-rails/public/uploads/test.out -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/-/project-lost-found/test.out
I, [2018-07-27T12:08:28.755624 #89817] INFO -- : Can fix /opt/gitlab/embedded/service/gitlab-rails/public/uploads/foo/bar/89a0f7b0b97008a4a18cedccfdcd93fb/foo.txt -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/qux/foo/bar/89a0f7b0b97008a4a18cedccfdcd93fb/foo.txt
I, [2018-07-27T12:08:28.760257 #89817] INFO -- : Can move to lost and found /opt/gitlab/embedded/service/gitlab-rails/public/uploads/foo/bar/1dd6f0f7eefd2acc4c2233f89a0f7b0b/image.png -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/-/project-lost-found/foo/bar/1dd6f0f7eefd2acc4c2233f89a0f7b0b/image.png
I, [2018-07-27T12:08:28.764470 #89817] INFO -- : To cleanup these files run this command with DRY_RUN=false
$ sudo gitlab-rake gitlab:cleanup:project_uploads DRY_RUN=false
I, [2018-07-27T12:08:32.944414 #89936] INFO -- : Looking for orphaned project uploads to clean up...
D, [2018-07-27T12:08:33.293568 #89817] DEBUG -- : Processing batch of 500 project upload file paths, starting with /opt/gitlab/embedded/service/gitlab-rails/public/uploads/test.out
I, [2018-07-27T12:08:33.689869 #89817] INFO -- : Did move to lost and found /opt/gitlab/embedded/service/gitlab-rails/public/uploads/test.out -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/-/project-lost-found/test.out
I, [2018-07-27T12:08:33.755624 #89817] INFO -- : Did fix /opt/gitlab/embedded/service/gitlab-rails/public/uploads/foo/bar/89a0f7b0b97008a4a18cedccfdcd93fb/foo.txt -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/qux/foo/bar/89a0f7b0b97008a4a18cedccfdcd93fb/foo.txt
I, [2018-07-27T12:08:33.760257 #89817] INFO -- : Did move to lost and found /opt/gitlab/embedded/service/gitlab-rails/public/uploads/foo/bar/1dd6f0f7eefd2acc4c2233f89a0f7b0b/image.png -> /opt/gitlab/embedded/service/gitlab-rails/public/uploads/-/project-lost-found/foo/bar/1dd6f0f7eefd2acc4c2233f89a0f7b0b/image.png若使用对象存储,请运行一体化 Rake 任务确保所有上传文件已迁移至对象存储,且 uploads 文件夹中无残留文件。
从对象存储清理项目上传文件
将 GitLab 数据库中不存在的对象存储上传文件移至失物招领目录。要从对象存储清理项目上传文件:
sudo gitlab-rake gitlab:cleanup:remote_upload_filesbundle exec rake gitlab:cleanup:remote_upload_files RAILS_ENV=production示例输出:
$ sudo gitlab-rake gitlab:cleanup:remote_upload_files
I, [2018-08-02T10:26:13.995978 #45011] INFO -- : Looking for orphaned remote uploads to remove. Dry run...
I, [2018-08-02T10:26:14.120400 #45011] INFO -- : Can be moved to lost and found: @hashed/6b/DSC_6152.JPG
I, [2018-08-02T10:26:14.120482 #45011] INFO -- : Can be moved to lost and found: @hashed/79/02/7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451/711491b29d3eb08837798c4909e2aa4d/DSC00314.jpg
I, [2018-08-02T10:26:14.120634 #45011] INFO -- : To cleanup these files run this command with DRY_RUN=false$ sudo gitlab-rake gitlab:cleanup:remote_upload_files DRY_RUN=false
I, [2018-08-02T10:26:47.598424 #45087] INFO -- : Looking for orphaned remote uploads to remove...
I, [2018-08-02T10:26:47.753131 #45087] INFO -- : Moved to lost and found: @hashed/6b/DSC_6152.JPG -> lost_and_found/@hashed/6b/DSC_6152.JPG
I, [2018-08-02T10:26:47.764356 #45087] INFO -- : Moved to lost and found: @hashed/79/02/7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451/711491b29d3eb08837798c4909e2aa4d/DSC00314.jpg -> lost_and_found/@hashed/79/02/7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451/711491b29d3eb08837798c4909e2aa4d/DSC00314.jpg移除孤立工件文件
这些命令不适用于存储在对象存储上的工件。
当发现磁盘上的作业工件文件和/或目录数量超出预期时,可以运行:
sudo gitlab-rake gitlab:cleanup:orphan_job_artifact_files此命令会:
- 扫描整个工件文件夹。
- 检查哪些文件在数据库中仍有记录。
- 若未找到数据库记录,则从磁盘删除文件及目录。
默认情况下,此任务不会删除任何内容,仅显示可删除内容。若要实际删除文件,请使用 DRY_RUN=false 运行命令:
sudo gitlab-rake gitlab:cleanup:orphan_job_artifact_files DRY_RUN=false您还可以使用 LIMIT(默认 100)限制删除文件数量:
sudo gitlab-rake gitlab:cleanup:orphan_job_artifact_files LIMIT=100此命令最多从磁盘删除 100 个文件。您可使用此参数删除少量文件进行测试。
提供 DEBUG=1 会显示检测到的每个孤立文件的完整路径。
若安装了 ionice,任务会使用它确保命令不会对磁盘造成过大负载。您可通过 NICENESS 配置优先级。以下是有效级别,但请查阅 man 1 ionice 确认:
0或None1或Realtime2或Best-effort(默认)3或Idle
移除过期的 ActiveSession 查找键
要移除过期的 ActiveSession 查找键:
sudo gitlab-rake gitlab:cleanup:sessions:active_sessions_lookup_keysbundle exec rake gitlab:cleanup:sessions:active_sessions_lookup_keys RAILS_ENV=production容器注册表垃圾回收
容器注册表可能占用大量磁盘空间。要清理未使用的层,注册表包含垃圾回收命令。