Gitlab 定时备份
2023-12-28 10:26:54
要求
1.为了能够备份和恢复,请确保你的系统上安装了Rsync
#Debian/Ubauntu
sudo apt-get install rsync
# RHEL/Centos
sudo yum install rsync
修改gitlab配置文件
vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" //gitlab默认备份目录,如有需要可更改
gitlab_rails['backup_archive_permissions'] = 0644 //生成的备份文件权限
gitlab_rails['backup_keep_time'] = 604800 //默认备份保留天数为7天(这里是604800秒)
使配置生效
gitlab-ctl reconfigure
创建备份目录并授权
mkdir /var/opt/gitlab/backups && chown -R git.git /var/opt/gitlab/backups/
重新生效Gitlabb配置
gitlab-ctl reconfigure
手动备份:
gitlab-rake gitlab:backup:create
查看备份
ll -sh /opt/gitlab_backups/
编写备份脚本
#!/usr/bin/bash
#获取当前时间
locale_date=`date +%Y-%m-%d.%H.%M.%S`
#远端IP备份主机ip
backup_host=10.10.111.223
#本地备份路径
backup_path=/var/opt/gitlab/backups
#日志路径
backup_log=/var/opt/gitlab/backups/logs/gitlab_back.log
#判断/var/opt/gitlab/backups目录是否存在,否则创建
if [ ! -d ${backup_path} ]; then
mkdir ${backup_path}
fi
#删除30天之前的备份数据
find ${backup_path} -mtime +5 -name "*" -exec rm -rf {} \;
#CRON=1 环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出
#BACKUP=${locale_date}改变backup文件名称 例: 2022-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
#拷贝配置文件至本地备份目录/var/opt/gitlab/backups
cp /etc/gitlab/gitlab-secrets.json ${backup_path}/${locale_date}_gitlab-secrets.json >> ${backup_log}
cp /etc/gitlab/gitlab.rb ${backup_path}/${locale_date}_gitlab.rb >> ${backup_log}
# 设置本地备份目录和远程目录
LOCAL_BACKUP_DIR="/var/opt/gitlab/backups/"
REMOTE_SERVER="root@10.10.111.223"
REMOTE_DIR="/opt/gitlab_backup/"
# 使用rsync同步目录
rsync -avz --progress $LOCAL_BACKUP_DIR $REMOTE_SERVER:$REMOTE_DIR
GItlab只能还原到与备份文件相同的gitlab版本
?[root@gitlabdev?~]# chmod +x /var/opt/gitlab/gitlab_backup.sh
加入定时任务:
结合crontab实施自动定时备份,比如每天0点、6点、12点、18点各备份一次crontab -e
?#添加定时任务
crontab -l
?#查看已添加定时任务
[root@gitlabdev ~]# crontab -l
0 0,6,12,18 * * * /bin/bash /home/mulan/gitlab-backup.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 /var/opt/gitlab/backups/*.tar|awk -F '/' '{print $4}'
echo -e "\n\033[36m请输入要恢复的文件或时间节点:\033[0m"
read input
gitlab_backup=${input%%_*}
gitlab_rb=/var/opt/gitlab/backups/${gitlab_backup}_gitlab.rb
secrets_json=/var/opt/gitlab/backups/${gitlab_backup}_gitlab-secrets.json
echo -e "\n\033[36m即将恢复以下文件:\033[0m"
echo "/var/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 /var/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
文章来源:https://blog.csdn.net/liufangaliya/article/details/135241001
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!