要求

1.为了能够备份和恢复,请确保你的系统上安装了Rsync

#Debian/Ubauntu
sudo apt-get install rsync
# RHEL/Centos
sudo yum install rsync

2.配置了与备份目标机器之间的免密认证

修改gitlab配置文件:

vim /etc/gitlab/gitlab.rb

#指定备份后数据存放的路径、权限、时间配置
gitlab_rails['manage_backup_path'] = true #292行 开启备份功能
gitlab_rails['backup_path'] = "/opt/gitlab_backups" #293行 指定备份的路径
gitlab_rails['backup_archive_permissions'] = 0644 #296行 备份文件的权限
gitlab_rails['backup_keep_time'] = 7776000 #301行 备份保留时间(保留90天 单位:秒
注意备份路径,备份主机要与本机一致,修改后记得执行gitlab-ctl reconfigure

创建备份目录并授权:

mkdir /opt/gitlab_backups && chown -R git.git /opt/gitlab_backups/

重新生效Gitlabb配置:

gitlab-ctl reconfigure

手动备份:

[root@gitlabdev ~]# gitlab-backup create
2021-06-15 10:37:09 +0800 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2021-06-15 10:37:12 +0800 -- done
2021-06-15 10:37:12 +0800 -- Dumping repositories ...
* eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ...
* eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ... [DONE]
* eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ...
* eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ... [EMPTY] [SKIPPED]
* eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ...
* eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ... [EMPTY] [SKIPPED]
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping uploads ...
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping builds ...
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping artifacts ...
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping pages ...
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping lfs objects ...
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping container registry images ...
2021-06-15 10:37:15 +0800 -- [DISABLED]
Creating backup archive: 1623724635_2021_06_15_13.12.3_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... 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.
ps:这里提示 gitlab.rb 和 gitlab-secrets.json 包涵敏感数据需要手动备份

查看备份:

ll -sh /opt/gitlab_backups/

编写备份脚本,结合crontab实施自动定时备份,比如每天0点、6点、12点、18点各备份一次

编写备份脚本:

#!/usr/bin/bash

#获取当前时间
locale_date=`date +%Y-%m-%d.%H.%M.%S` #远端IP备份主机ip
backup_host=192.168.101.133 #本地备份路径
backup_path=/opt/gitlab_backups #日志路径
backup_log=/opt/gitlab_backups/gitlab_back.log #CRON=1 环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出
#BACKUP=${locale_date}改变backup文件名称 例: 2021-06-15_11:22:52_gitlab_backup.tar /opt/gitlab/bin/gitlab-backup create BACKUP=${locale_date} CRON=1
if [ $? -eq 0 ];then
echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建成功." >> ${backup_log}
else
echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建失败." >> ${backup_log}
exit 1
fi #判断/opt/gitlab_backups目录是否存在,否则创建
if [ ! -d ${backup_path} ]; then
mkdir ${backup_path}
fi #拷贝配置文件至本地备份目录/opt/gitlab_backups
cp -af /etc/gitlab/gitlab-secrets.json ${backup_path}/${locale_date}_gitlab-secrets.json >> ${backup_log}
cp -af /etc/gitlab/gitlab.rb ${backup_path}/${locale_date}_gitlab.rb >> ${backup_log} #同步本地 /opt/gitlab_backups目录到远端/opt/目录下
rsync -avzPr --delete /opt/gitlab_backups root@${backup_host}:/opt/ >> ${backup_log}

[root@gitlabdev ~]# chmod +x /opt/gitlab_backups/gitlab_back.sh

加入定时任务:

crontab -e #添加定时任务
crontab -l #查看已添加定时任务
[root@gitlabdev ~]# crontab -l
0 0,6,12,18 * * * /bin/bash /opt/gitlab_backups/gitlab_back.sh > /dev/null 2>&1
GItlab只能还原到与备份文件相同的gitlab版本。

备份恢复脚本

#!/bin/bash

local_ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
echo -e "本机IP:${local_ip} \n" echo -e "\033[36m发现以下下备份文件:\n \033[0m"
ls -lt /opt/gitlab_backups/*.tar|awk -F '/' '{print $4}'
echo -e "\n\033[36m请输入要恢复的文件或时间节点:\033[0m"
read input gitlab_backup=${input%%_*}
gitlab_rb=/opt/gitlab_backups/${gitlab_backup}_gitlab.rb
secrets_json=/opt/gitlab_backups/${gitlab_backup}_gitlab-secrets.json echo -e "\n\033[36m即将恢复以下文件:\033[0m"
echo "/opt/gitlab_backups/${gitlab_backup}_gitlab_backup.tar"
echo ${gitlab_rb}
echo ${secrets_json} sed -i "s#\(^external_url .*\)#external_url 'http://${local_ip}' #g" ${gitlab_rb} chown -Rf git:git /opt/gitlab_backups #/bin/cp -f /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb-`date +%Y-%m-%d_%H_%M_%S`-backup
#/bin/cp -f /etc/gitlab/gitlab-secrets.json /etc/gitlab/gitlab-secrets.json-`date +%Y-%m-%d_%H_%M_%S`-backup
#/bin/cp -f ${gitlab_rb} /etc/gitlab/gitlab.rb
#/bin/cp -f ${secrets_json} /etc/gitlab/gitlab-secrets.json
#/opt/gitlab/bin/gitlab-ctl reconfigure
echo -e "\n\033[36m停止数据库服务\033[0m" /opt/gitlab/bin/gitlab-ctl stop unicorn
/opt/gitlab/bin/gitlab-ctl stop puma
/opt/gitlab/bin/gitlab-ctl stop sidekiq echo -e "\n\033[36m开始恢复${gitlab_backup}备份:\033[0m"
/opt/gitlab/bin/gitlab-backup restore BACKUP=${gitlab_backup} echo -e "\n\033[36m备份本机配置文件\033[0m"
/bin/cp -f /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb-`date +%Y-%m-%d_%H_%M_%S`-backup
/bin/cp -f /etc/gitlab/gitlab-secrets.json /etc/gitlab/gitlab-secrets.json-`date +%Y-%m-%d_%H_%M_%S`-backup
ls -lt /etc/gitlab/|grep `date +%Y-%m-%d_%H_%M_%S` echo -e "\n\033[36m覆盖本机配置文件\033[0m"
/bin/cp -f ${gitlab_rb} /etc/gitlab/gitlab.rb
/bin/cp -f ${secrets_json} /etc/gitlab/gitlab-secrets.json
echo "/etc/gitlab/gitlab.rb"
echo "/etc/gitlab/gitlab-secrets.json" echo -e "\n\033[36m重新加载配置文件并重启服务\033[0m"
/opt/gitlab/bin/gitlab-ctl reconfigure
/opt/gitlab/bin/gitlab-ctl restart

Gitlab 定时备份的更多相关文章

  1. gitlab 远程 定时备份

    =============================================== 2017/11/1_第2次修改                       ccb_warlock 更新 ...

  2. gitlab 本地 定时备份

    =============================================== 20171015_第1次修改                       ccb_warlock === ...

  3. Gitlab备份,Crontab定时备份

    1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...

  4. Centos 8 上定时备份Gitlab ,脚本实现定时备份,备份恢复

    定时备份 要求 为了能够备份和恢复,请确保你的系统上安装了Rsync yum install rsync -y 配置备份目标机器免密认证 执行ssh-keygen -t rsa 生成私钥和公钥 ssh ...

  5. gitlab自动备份和定时删除

    GitLab数据手动备份1.GitLab默认备份目录为/var/opt/gitlab/backups,可以修改/etc/gitlab/gitlab.rb里面的默认存放备份文件目录,这里使用默认备份目录 ...

  6. GitLab - GitLab的备份与还原

    1 - GitLab配置文件 GitLab默认的配置文件路径:/etc/gitlab/ /etc/gitlab/gitlab.rb:主配置文件,包含外部URL.仓库目录.备份目录等 /etc/gitl ...

  7. Gitlab日常维护(三)之Gitlab的备份、迁移、升级

    一.Gitlab的备份 使用Gitlab一键安装包安装Gitlab非常简单, 同样的备份恢复与迁移也非常简单. 使用一条命令即可创建完整的Gitlab备份 [root@gitlab ~]# gitla ...

  8. linux定时备份mysql并同步到其它服务器

    数据在任何一家公司里面都是最核心的资产,定期备份则是为了保证数据库出现问题的时候能够及时回滚到最近的备份点,将损失缩小到最小 这篇文章将会两部分来说明:1.mysql的定期备份:2.同步到其它服务器 ...

  9. Linux下使用crontab定时备份日志

    上周学习了Linux,其中有使用crontab定时备份日志的内容,现把主要步骤记录如下: 首先需要备份的日志的源目录位于/opt/lampp/logs/access_log 备份到/tmp/logs下 ...

随机推荐

  1. Jira&Confluence服务器安装

    1.Mysql安装 参考https://confluence.atlassian.com/doc/database-setup-for-mysql-128747.html 创建相应的数据库 CREAT ...

  2. [转载]屏幕PPI、分辨率到底需要多大才能满足?

    屏幕PPI.分辨率到底需要多大才能满足? 郝蛋儿 江湖骗子 13 人赞同了该文章 最近想买一个43寸的电视,720P和1080P差了500大洋.我不禁纠结了起来.看网上争得面红耳赤,有的人说不如108 ...

  3. 《Matlab实用案例》系列Matlab从入门到精通实用100例案例教程目录(持续更新)

    目录 1. 专栏简介 2. 专栏地址 3. 专栏目录 1. 专栏简介 2. 专栏地址 「 刘一哥与GIS的故事 」之<Matlab使用案例> 3. 专栏目录 [MATLAB统计分析与应用1 ...

  4. ChromiumWebBrowser 禁用右键菜单

    /// <summary>    /// 禁用嵌套页面的右键菜单    /// </summary>    public class MenuHandler : IContex ...

  5. Jenkins 基础篇 - 任务创建

    前面了解了 Jenkins 上各种任务的区别后,我们就来实践应用下,先创建一个[文件夹]类型的任务,将我们目前的一些基础的演示任务[移动]到文件夹里面去,这样可以先做个简单的分类. 新建一个[文件夹] ...

  6. NGINX缓存使用官方指南

    我们都知道,应用程序和网站一样,其性能关乎生存.但如何使你的应用程序或者网站性能更好,并没有一个明确的答案.代码质量和架构是其中的一个原因,但是在很多例子中我们看到,你可以通过关注一些十分基础的应用内 ...

  7. Python+Selenium+Appium+API学习使用过的命令

    adb devices 查看连接电脑的手机设备 weditor 启动uiautomatorviewer2元素定位工具 以下2个命令作用一样 adb shell dumpsys activity | f ...

  8. 设计模式Copy-on-write

    1.Copy-on-Write 又称COW,写时复制 String的replace()方法,没有修改内部的value数组,而是新创建了一个不可变对象 这种方法在解决不可变对象时,经常使用 这其实就是一 ...

  9. DDD中聚合、聚合根的含义以及作用

    聚合与聚合根的含义 聚合: 聚合往往是一些实体为了某项业务而聚类在一起形成的集合 , 举个例子, 社会是由一个个的个体组成的,象征着我们每一个人.随着社会的发展,慢慢出现了社团.机构.部门等组织,我们 ...

  10. CVD和ALD薄膜沉积技术应用领域

    CVD和ALD薄膜沉积技术应用领域 显示 用于OLED.QD-OLED.甚至未来QLED的薄膜封装,通过有机/无机叠层结构的保护,水汽渗透率WVTR可降至10-5g/m2/day,保证OLED或者量子 ...