gitlab备份修改配置文件和单个项目备份
2019-06-28 本文已影响0人
黑客不黑_
gitlab备份分为热备份和冷备份
- 热备份:通过gitlab-rake gitlab:backup:create命令备份全部gitlab项目到gitlab服务器,同时可以创建脚本文件结合crontab定时任务定时备份,
但是当gitlab项目越来越多时,文件多达十几G,全部备份就有点浪费资源,而且也不能备份太频繁。所以结合冷备份。 - 冷备份:通过gitlab WEBUI导出已经开发完成的项目文件,这些开发完成的基本不用更新变动或者变得小,所以可以通过导出的方式备份到异地备份。所以我最后采用的是crontab定时备份一次热备份和冷备份开发完的项目文件。
一、备份全部
默认配置文件为/etc/gitlab/gitlab.rb
可是无论无何找不到这个文件,通过find命令搜索全盘找到不少gitlab.rb文件,可是都不是主配置文件。
后面在下面路径下找到了配置文件gitlab.yml。正好有修改服务器ip和配置所有配置的文件,当我看到那个服务器IP我就知道是这个配置文件了,那就是这个了。而且其他数据库、redis等配置文件都在这里。
[root@k8s-node2 etc]# pwd
/var/opt/gitlab/gitlab-rails/etc
[root@k8s-node2 etc]# ls
database.yml gitlab_shell_secret gitlab_workhorse_secret gitlab.yml rack_attack.rb resque.yml secrets.yml unicorn.rb
再看下gitlab.yml文件,当/etc/gitlab/gitlab.rb文件,那么你的主配置文件肯定是在/var/opt/gitlab/gitlab-rails/etc/gitlab.yml 。
通过搜索backup就可以得到备份文件配置,更改path路径即可。
## GitLab settings
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
host: 172.16.3.42
port: 80
https: false
## Backup settings
backup:
path: "/home/data/gitlab" # Relative paths are relative to Rails.root (default: tmp/backups/)
archive_permissions: # Permissions for the resulting backup.tar file (default: 0600)
keep_time: # default: 0 (forever) (in seconds)
pg_schema: # default: nil, it means that all schemas will be backed up
upload:
# Fog storage connection settings, see http://fog.io/storage/ .
connection:
# The remote 'directory' to store your backups. For S3, this would be the bucket name.
remote_directory:
multipart_chunk_size:
encryption:
storage_class:
同时给备份路径更改目录所有者为git账户,不然会报错。
cd /home/data/gitlab
chown git:root gitlab/
[root@k8s-node2 data]# ll
total 0
drwx--x--x 11 root root 135 May 16 17:11 docker
drwxr-xr-x 4 git root 225 Jun 28 11:21 gitlab
可以备份了,备份命令为:
gitlab-rake gitlab:backup:create
备份完成就可以在备份路径下看到一个当前日期下的tar压缩文件,可以看到文件已经很大了。
[root@k8s-node2 gitlab]# pwd
/home/data/gitlab
[root@k8s-node2 gitlab]# ls
1561692095_2019_06_28_11.4.5_gitlab_backup.tar
[root@k8s-node2 gitlab]# du -h 1561692095_2019_06_28_11.4.5_gitlab_backup.tar
13G 1561692095_2019_06_28_11.4.5_gitlab_backup.tar
编辑备份脚本
#!/bin/bash
/usr/bin/gitlab-rake gitlab:backup:create CRON=1
注意:环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出
# crontab -l
0 0,6,12,18 * * * /bin/bash -x /data/gitlab/backups/gitlab_backup.sh > /dev/null 2>&1
gitlan如何恢复呢?
gitlab-rake gitlab:backup:restore BACKUP=1561692095_2019_06_28_11.4.5
- 注意Gitlab的恢复操作会先将当前所有的数据清空,然后再根据备份数据进行恢复
迁移也是一样的操作,不过gitlab迁移要求gitlab的版本一致。
二、单个项目文件备份
进入gitlab,进入设置-->export project-->expand-->export project。


下载得到tar.gz包。
如何导入:
新建项目,选择import project,选择gitlab export。

设置项目名选择文件导入导入项目即可。
