经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
Gitlab 定时备份
来源:cnblogs  作者:砖家组组长  时间:2021/6/21 9:43:09  对本文有异议

要求

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

  1. #Debian/Ubauntu
  2. sudo apt-get install rsync
  3. # RHEL/Centos
  4. sudo yum install rsync

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

修改gitlab配置文件:

vim /etc/gitlab/gitlab.rb

  1. #指定备份后数据存放的路径、权限、时间配置
  2. gitlab_rails['manage_backup_path'] = true #292行 开启备份功能
  3. gitlab_rails['backup_path'] = "/opt/gitlab_backups" #293行 指定备份的路径
  4. gitlab_rails['backup_archive_permissions'] = 0644 #296行 备份文件的权限
  5. 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

手动备份:

  1. [root@gitlabdev ~]# gitlab-backup create
  2. 2021-06-15 10:37:09 +0800 -- Dumping database ...
  3. Dumping PostgreSQL database gitlabhq_production ... [DONE]
  4. 2021-06-15 10:37:12 +0800 -- done
  5. 2021-06-15 10:37:12 +0800 -- Dumping repositories ...
  6. * eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ...
  7. * eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ... [DONE]
  8. * eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ...
  9. * eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ... [EMPTY] [SKIPPED]
  10. * eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ...
  11. * eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ... [EMPTY] [SKIPPED]
  12. 2021-06-15 10:37:15 +0800 -- done
  13. 2021-06-15 10:37:15 +0800 -- Dumping uploads ...
  14. 2021-06-15 10:37:15 +0800 -- done
  15. 2021-06-15 10:37:15 +0800 -- Dumping builds ...
  16. 2021-06-15 10:37:15 +0800 -- done
  17. 2021-06-15 10:37:15 +0800 -- Dumping artifacts ...
  18. 2021-06-15 10:37:15 +0800 -- done
  19. 2021-06-15 10:37:15 +0800 -- Dumping pages ...
  20. 2021-06-15 10:37:15 +0800 -- done
  21. 2021-06-15 10:37:15 +0800 -- Dumping lfs objects ...
  22. 2021-06-15 10:37:15 +0800 -- done
  23. 2021-06-15 10:37:15 +0800 -- Dumping container registry images ...
  24. 2021-06-15 10:37:15 +0800 -- [DISABLED]
  25. Creating backup archive: 1623724635_2021_06_15_13.12.3_gitlab_backup.tar ... done
  26. Uploading backup archive to remote storage ... skipped
  27. Deleting tmp directories ... done
  28. done
  29. done
  30. done
  31. done
  32. done
  33. done
  34. done
  35. Deleting old backups ... done. (0 removed)
  36. Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
  37. and are not included in this backup. You will need these files to restore a backup.
  38. Please back them up manually.
  39. Backup task is done.
ps:这里提示 gitlab.rb 和 gitlab-secrets.json 包涵敏感数据需要手动备份

查看备份:

ll -sh /opt/gitlab_backups/

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

编写备份脚本:

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

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

加入定时任务:

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

备份恢复脚本

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

原文链接:http://www.cnblogs.com/afei654138148/p/14884439.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号