记Gitlab备份与设置自动备份

这篇具有很好参考价值的文章主要介绍了记Gitlab备份与设置自动备份。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

今天给Gitlab做了一个备份,并且设置了每天自动备份,记录一下。

一、导出全部项目

由于Gitlab Web页面并没有自动备份的相关设置,只有各个项目有一个“导出项目”功能。为了保证安全,先把所有项目全部使用“导出项目”功能导出一遍,并复制出来。通过笔者之前的博文《gitlab迁移记》,可以知道Gitlab导出的项目是存储在/var/opt/gitlab/gitlab-rails/uploads/-/system/import_export_upload/export_file的。

二、设置备份配置

如果是在容器中运行的Gitlab,使用podman exec -it gitlab /bin/bash命令进入容器,修改/etc/gitlab/gitlab.rb,如果是在宿主机直接安装的Gitlab,则直接修改/etc/gitlab/gitlab.rb,打开如下配置:

### Backup Settings
###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html

gitlab_rails['manage_backup_path'] = true 
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
# gitlab_rails['backup_gitaly_backup_path'] = "/opt/gitlab/embedded/bin/gitaly-backup"

###! Docs: https://docs.gitlab.com/ee/raketasks/backup_restore.html#backup-archive-permissions
gitlab_rails['backup_archive_permissions'] = 0644 

# gitlab_rails['backup_pg_schema'] = 'public'

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

其中gitlab_rails['backup_keep_time']是设置备份保存时间,以秒为单位,6048007天。

修改好配置后,使用gitlab-ctl reconfigure重新生成配置,再使用gitlab-ctl restart重启gitlab。

三、手动备份

按官方的说法,GitLab 12.2 及之后版本执行
gitlab-rake gitlab:backup:create;GitLab 12.1及之前版本执行gitlab-backup create。不过笔者14.5.0的Gitlab执行的gitlab-rake gitlab:backup:create,也是可以的。

通过帮助可以看出gitlab-backup create就是gitlab-rake gitlab:backup:create的包装命令。

# gitlab-backup create --help
Usage: gitlab-backup create [OPTIONS]

  Create a new backup. Wrapper for `gitlab-rake gitlab:backup:create`.

OPTIONS:

  -h, --help    Display this help message and exits,

  Additional OPTIONS are passed to the underlying command.

也可以直接在宿主机中使用命令podman exec -it gitlab gitlab-backup create或者podman exec -it gitlab gitlab-rake gitlab:backup:create直接备份,不用进入容器中执行。

备份时大概输出情况:

root@10:/# gitlab-rake gitlab:backup:create

2023-06-13 03:34:02 +0000 -- Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2023-06-13 03:34:06 +0000 -- done
2023-06-13 03:34:06 +0000 -- Dumping repositories ...

2023-06-13 03:37:04 +0000 -- done
2023-06-13 03:37:04 +0000 -- Dumping uploads ... 
2023-06-13 03:51:10 +0000 -- done
2023-06-13 03:51:10 +0000 -- Dumping builds ... 
2023-06-13 03:51:10 +0000 -- done
2023-06-13 03:51:10 +0000 -- Dumping artifacts ... 
2023-06-13 03:51:10 +0000 -- done
2023-06-13 03:51:10 +0000 -- Dumping pages ... 
2023-06-13 03:51:10 +0000 -- done
2023-06-13 03:51:10 +0000 -- Dumping lfs objects ... 
2023-06-13 03:51:11 +0000 -- done
2023-06-13 03:51:11 +0000 -- Dumping container registry images ... 
2023-06-13 03:51:11 +0000 -- done
Creating backup archive: 1686628281_2023_06_13_14.5.0-ee_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
done
Deleting old backups ... done. (0 removed)
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.

注意后面的警告,需要手动备份gitlab.rbgitlab-secrets.json

备份路径即为前面配置中设定的路径,默认为/var/opt/gitlab/backups

从备份输出日志可以看到有Dumping uploads,即会备份uploads中的内容,而前面导出项目时,导出的文件就在uploads中,为避免把导出的文件也备份,可以先把导出目录/var/opt/gitlab/gitlab-rails/uploads/-/system/import_export_upload/export_file中的文件删除掉,再进行备份。

四、设置自动备份

手动备份还是比较麻烦,可以借助Linux的crontab设置为自动备份,比如可以设置每小时、每天、每月、每年、每周备份。
crontab配置可以分为系统级的配置和用户级的配置,任选一种即可。

1.系统级配置

crontab系统级配置,可以查看/etc/crontab

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

这个文件分别设置了每小时、每天、每周和每月要执行的任务:

  • /etc/cron.hourly:每小时的17分执行
  • /etc/cron.daily:每天的6点25分执行
  • /etc/cron.weekly:每周周日的6点47分执行
  • /etc/cron.monthly:每月1号的6点52分执行

可以设置Gitlab每天备份,那可以在/etc/cron.daily中添加一个文件gitlab-backup,内容如下:

#!/bin/sh
podman exec -it gitlab gitlab-backup create

如果是宿主机则为:

#!/bin/sh
gitlab-backup create

然后使用chmod +x gitlab-backup命令为其添加上可执行权限。

还有一个目录/etc/cron.d,是自定义计划任务的,格式同/etc/crontab配置。

2.用户级配置

使用crontab -e设置当前用户的计划任务,默认会在/tmp下创建一个文件,笔者的为/tmp/crontab.0kND4I/crontab

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

添加如下一行进行测试:

40-50 * * * * echo "测试" >> ~/a.txt

即在每个小时的40~50分钟时,添加“测试”到~/a.txt文件。

查看计划任务crontab -l

记Gitlab备份与设置自动备份
如果是要添加root用户的计划任务,使用sudo crontab -e -u root,查看使用sudo crontab -l -u root

回到Gitlab备份,非root用户,有sudo权限,则可以使用:

0 2 * * * sudo podman exec -it gitlab gitlab-backup create

每天凌晨2点备份。

欢迎点赞收藏,转载请注明出处!文章来源地址https://www.toymoban.com/news/detail-493133.html

到了这里,关于记Gitlab备份与设置自动备份的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Centos使用Docker搭建自己的Gitlab社区版16.8.0-ce.0(设置汉化 修改密码 设置SSH秘钥 添加拉取命令端口号 备份至网盘和恢复)

    根据我的经验 部署Gitlab(社区版) 至少需要2核4g的服务器 带宽3~4M 前端自动化(其一)部署gitlab(本章!) 前端自动化(其二)部署jenkins、配置环境、连接gitlab 前端自动化(其三)持续集成和持续部署 1. 在自己电脑上安装终端:宝塔ssl终端 或者 FinalShell,根据喜好安装即

    2024年01月23日
    浏览(35)
  • 将某个GitLab上的项目自动同步到另一个GitLab账户下的仓库

    引言 我们在进行项目迁移,或者是给甲乙外包写项目,需要迁移代码或者同步更新时,所处于的GitLab地址是不一样的,那么我们如何进行同步呢?我们可能第一时间会想到,先拉A地址的代码到本地,然后覆盖提交到B地址,可是如果每天都需要进行这个操作,或者说是实时更

    2024年01月18日
    浏览(36)
  • gitlab镜像仓库设置及自动同步代码到服务器实现方式

    最近新接了一个需求,有两个不同的gitlab仓库,隶属于不同的部门,我们需要将其他部门的gitlab代码仓库实时或定时同步到我们的gitlab代码仓库上. 网上调研了下实现方案, 大概有两种 方案一 直接设置镜像仓库 方案二 通过gitlab webhooks + jenkins + rsync或者git命令同步 进入代码仓库之

    2024年02月03日
    浏览(40)
  • 思科路由器交换机IOS文件自动备份与还原设置教程

    思科IOS配置文件的自动备份与回退需要使用到IOS的archive归档配置模式功能。 一般来说,只有IOS的版本号在12.3(4) 与其之后的版本才带有归档配置模式功能。 思科IOS配置文件自动备份全部命令如下 : Router Routerenable Router#configure terminal Router(config)#archive Router(config-archive)#write-

    2024年02月05日
    浏览(40)
  • linux中用shell脚本定时备份重要日志等文件【命令结果输出也在内。按月份自动创建时间自动规划到月份中】、linux中用shell脚本备份文件并自动scp汇总到一个服务器上

    现在甲方有一个需求:要求每月备份系统日志 问题: 1、有37台服务器需要备份且汇总到一

    2024年02月19日
    浏览(44)
  • Gitlab 备份和恢复

    gitlab可以算作公司最重要的知识资产之一,虽然由于其设计,每个开发本地可能都存在着负责项目的全部提交记录,但一旦gitlab发生故障,轻则浪费大量时间进行项目重建,重则丢失项目代码数据。未雨绸缪,提前进行gitlab备份和数据恢复演练,本文记录gitlab的定时备份方案

    2024年02月07日
    浏览(27)
  • GitLab备份升级

    数据备份(默认的备份目录在/var/opt/gitlab/backups/下,生成一个以时间节点命名的tar包。) 新建repo源,升级新版本的gitlab 查看版本号: 升级必须按照版本号一步一步依次升级 官网文档 第一步:停止服务 第二步:启动数据库 第三步:开始安装升级(比如当前版本是11.8.6需要升级

    2024年02月14日
    浏览(31)
  • Gitlab 数据备份

    1、Gitlab 创建备份 1.1、手动备份 创建备份文件 使用一条命令即可创建完整的Gitlab备份。 gitlab-rake gitlab:backup:create 使用命令会在/var/opt/gitlab/backups目录下创建一个压缩包,这个压缩包就是Gitlab整个的完整部分。 [root@server ~]# gitlab-rake gitlab:backup:create 生成完后,/var/opt/gitlab/bac

    2023年04月21日
    浏览(23)
  • Gitlab 备份与恢复

    1、备份数据(手动备份) 2、备份数据(定时任务备份) 1、还原数据 1.1、复制数据到新Gitlab默认备份目录 1.2、设置备份文件权限 1.3、使用如下命令恢复数据 1.3.1、停止 Gitlab 1.3.2、还原数据 1.3.3、重启 Gitlab 1.4、打开web页面,查看数据是否正常 说明:输入账号密码,查看数

    2024年02月14日
    浏览(28)
  • gitlab【安装部署、备份与恢复】

    【1】安装依赖 【2】安装软件包 【3】修改配置 【4】初始化gitlab、后续修改了配置也需要初始化 【5】查看安装是否报错 【6】验证邮箱是否正常  【7】浏览器测试访问   【8】汉化、需要软件包,版本一致  【9】数据备份 【10】数据恢复  

    2024年02月13日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包