记Gitlab备份与设置自动备份

28 篇文章 2 订阅
11 篇文章 1 订阅

今天给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

在这里插入图片描述
如果是要添加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点备份。

欢迎点赞收藏,转载请注明出处!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一份 GitLab 自动备份脚本及增量备份的方案。 1. 自动备份脚本 首先,您需要创建一个脚本文件,例如 `gitlab_backup.sh`,并将其放置在 GitLab 服务器上。以下是一个简单的备份脚本示例: ```bash #!/bin/bash # 设置备份目录 BACKUP_DIR="/mnt/backups/gitlab" # 创建备份目录 mkdir -p $BACKUP_DIR # 执行备份命令 gitlab-rake gitlab:backup:create BACKUP=timestamp # 将备份文件移动到备份目录 mv /var/opt/gitlab/backups/* $BACKUP_DIR/ ``` 该脚本将会执行 GitLab备份命令,将备份文件移动到指定目录中。 2. 增量备份方案 要执行增量备份,您需要使用 GitLab 的 `gitlab-backup` 命令的 `SKIP` 参数。该参数将跳过已备份的数据,只备份最新的更改。 以下是一个示例脚本,可以在每天执行一次增量备份: ```bash #!/bin/bash # 设置备份目录 BACKUP_DIR="/mnt/backups/gitlab" # 创建备份目录 mkdir -p $BACKUP_DIR # 获取最近备份的时间戳 LAST_BACKUP=$(ls -Art $BACKUP_DIR | tail -n 1 | cut -d'_' -f2) # 执行备份命令 gitlab-rake gitlab:backup:create BACKUP=timestamp SKIP=$LAST_BACKUP # 将备份文件移动到备份目录 mv /var/opt/gitlab/backups/* $BACKUP_DIR/ ``` 该脚本将检查备份目录中最新的备份文件的时间戳,并将其传递给 `gitlab-backup` 命令的 `SKIP` 参数。这将导致 GitLab备份自上次备份以来更改的数据。 希望这些信息能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值