Gitlab 定时备份

Posted 砖家组

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gitlab 定时备份相关的知识,希望对你有一定的参考价值。

要求

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_backup"         #293行      指定备份的路径
gitlab_rails[\'backup_archive_permissions\'] = 0644          #296行      备份文件的权限
gitlab_rails[\'backup_keep_time\'] = 7776000                 #301行      备份保留时间(保留90天 单位:秒

创建备份目录并授权:

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`
backup_host=192.168.101.133
backup_path=/opt/gitlab_backups/${locale_date}
#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

ssh root@${backup_host} ls ${backup_path} > /dev/null 2\\>\\&1
if [ $? -eq 0 ];then
        scp ${backup_path}_gitlab_backup.tar root@${backup_host}:${backup_path}
        scp /etc/gitlab/gitlab-secrets.json root@${backup_host}:${backup_path}
        scp /etc/gitlab/gitlab.rb root@${backup_host}:${backup_path}
else
        ssh root@${backup_host} mkdir -p ${backup_path}
        scp ${backup_path}_gitlab_backup.tar root@${backup_host}:${backup_path}
        scp /etc/gitlab/gitlab-secrets.json root@${backup_host}:${backup_path}
        scp /etc/gitlab/gitlab.rb root@${backup_host}:${backup_path}
fi

[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版本。

以上是关于Gitlab 定时备份的主要内容,如果未能解决你的问题,请参考以下文章

GitLab 远程 定时备份

GitLab 远程 定时备份

gitlab 自动备份与清理

gitlab宕机无法再次重启解决办法

gitlab的备份恢复

Gitlab 运维篇之——备份及恢复