GitLab 定时备份

运行环境

  • docker
  • gitlab
  • crontab
  • rsync
  • sshpass

文件层级关系

  • /home
    • deployment
      • backup.sh
      • sync.sh
      • restore.sh

backup.sh

# 创建备份
gitlab-rake gitlab:backup:create
# 获取最新备份的文件名
backupName=$(ls -lt /home/gitlab/backup | grep gitlab_backup.tar | head -n 1 |awk '{print $9}');
# 获取文件名的前28位
backupName=${backupName:0:28}
# 调用同步脚本
sh /home/deployment/sync.sh $backupName
# 调用恢复脚本
sh /home/deployment/restore.sh $backupName

sync.sh

# 同步到云
# 备份文件名
backupName=$1
sshpass -p "密码" rsync --progress "/home/gitlab/backup/"$backupName"_gitlab_backup.tar" root@服务器ip:"/root/gitlab/"$backupName"_gitlab_backup.tar"

restore.sh

# 备份名称 例:backupName="1590975087_2020_06_01_11.9.8";
backupName=$1;
# 备份文件全名
backupFullName=$backupName"_gitlab_backup.tar"
# 复制到目录文件下
cp "/home/gitlab/backup/"$backupFullName /home/deployment/gitlab/data/backups/
# 设置权限
chmod 777 "/home/deployment/gitlab/data/backups/"$backupFullName
# 执行恢复
yes yes|docker exec -i gitlab gitlab-rake gitlab:backup:restore BACKUP=""$backupName

crontab

# 编辑
crontab -e
# 每天 23:00 执行
00 23 * * * /bin/sh /home/deployment/backup.sh
# 查看
crontab -l
# 重启 crontab
/bin/systemctl restart crond.service

工作示意图

  • GitLab [宿主机]
    • ⌚23:00调用backup.sh
      • restore.sh本地备份并恢复到docker备用gitlab上
      • sync.sh同步指定服务器
原文地址:https://www.cnblogs.com/linyisonger/p/13367794.html
参考文章地址:https://www.cnblogs.com/ssgeek/p/9392104.html
posted @ 2021-01-05 10:49  杰克马001  阅读(122)  评论(0编辑  收藏  举报