GitLab定时备份及恢复

时间:2022-07-23
本文章向大家介绍GitLab定时备份及恢复,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

参考

Gitlab备份、迁移、恢复和升级

背景

项目中使用linux服务器作为Gitlab仓储,需要每天自动定时备份(数据备份到windows上),防止服务器磁盘损坏造成数据数据丢失。

操作步骤

1. GitLab备份操作

gitlab-rake gitlab:backup:create

执行之后会默认在/var/opt/gitlab/backups文件夹下产生备份文件

[root@master opt]# ls /var/opt/gitlab/backups/
1596532146_2020_08_04_13.1.4_gitlab_backup.tar  1596532193_2020_08_04_13.1.4_gitlab_backup.tar
2.配置linux访问windows免密登录

方便Shell脚本scpGitLab备份文件 详见: Win7安装OpenSSH服务,并配置linux对windows免密登录

3.编写Shell导出脚本

vim /opt/gitlabscp.sh

#!/bin/sh

scp_linux_path='/var/opt/gitlab/backups/'
scp_windows_uri='user@192.168.1.58:E:tmp'
scp_log_path='/opt/gitlabscp.log'

time=$(date +%Y-%m-%d %H:%M:%S)
gitlab-rake gitlab:backup:create
filename=`ls $scp_linux_path -t |head -n1|awk '{print $0}'`
echo [$time] $scp_linux_path$filename >> $scp_log_path
scp $scp_linux_path$filename $scp_windows_uri
4. 配置Linux定时任务

crontab -e

0 1 * * * /opt/gitlabscp.sh

如此配置可设置每天凌晨1点,定时从gitlab服务器向windows主机导出备份文件 注意: linux 的crontab cron表达式最小单位为分钟

5.gitlab恢复
  • 停止相关服务

gitlab-ctl stop unicorn gitlab-ctl stop sidekiq

  • 修改备份文件权限

chmod 777 /var/opt/gitlab/backups/1530156812_2018_06_28_10.8.4_gitlab_backup.tar

  • 执行恢复

gitlab-rake gitlab:backup:restore BACKUP=1530156812_2018_06_28_10.8.4

  • gitlab启动

gitlab-ctl start

  • 重新访问UI界面进行查看,恢复成功