首页 > 代码库 > Gitlab日常备份和恢复
Gitlab日常备份和恢复
按照官方安装文档安装完成之后的/home/git目录结构如下,这里我大致翻译官方的内容,部分加上自己的话,英语功力捉急,请见谅
|-- home
| |-- git
| |-- .ssh
| |-- gitlab
| |-- gitlab-satellites
| |-- gitlab-shell
| |-- repositories
* `/home/git/.ssh` - ssh设定的目录. gitlab-shell管理着其中的 `authorized_keys`.
* `/home/git/gitlab` - GitLab核心部分
* `/home/git/gitlab-satellites` - 可以视为临时目录,通过web ui的提交请求文件以及检出版本库都会存放在这个位置
* `/home/git/gitlab-shell` - gitlab的核心插件组件. 包括ssh协议克隆和其他一些功能.
* `/home/git/repositories` - 原始版本库的所有项目组织的名称空间,也就是所有仓库的存储位置,所以这个目录里的数据非常重要,注意备份 **这是项目的关键数据. Keep a backup**
*Note: gitlab-satellites 和 repositories的路径都被定义在gitlab中的 `config/gitlab.yml` 和 gitlab-shell.* 中的`config.yml`
创建为所有版本库的存档,就是备份啦. 存储路径在gitlab中的 `config/gitlab.yml`
文件命名 `[TIMESTAMP]_gitlab_backup.tar`.
```
bundle exec rake gitlab:backup:create RAILS_ENV=production
```
类似如下:
```
Dumping database tables:
- Dumping table events... [DONE]
- Dumping table issues... [DONE]
- Dumping table keys... [DONE]
- Dumping table merge_requests... [DONE]
- Dumping table milestones... [DONE]
- Dumping table namespaces... [DONE]
- Dumping table notes... [DONE]
- Dumping table projects... [DONE]
- Dumping table protected_branches... [DONE]
- Dumping table schema_migrations... [DONE]
- Dumping table services... [DONE]
- Dumping table snippets... [DONE]
- Dumping table taggings... [DONE]
- Dumping table tags... [DONE]
- Dumping table users... [DONE]
- Dumping table users_projects... [DONE]
- Dumping table web_hooks... [DONE]
- Dumping table wikis... [DONE]
Dumping repositories:
- Dumping repository abcd... [DONE]
Creating backup archive: $TIMESTAMP_gitlab_backup.tar [DONE]
Deleting tmp directories...[DONE]
Deleting old backups... [SKIPPING]
```
### 利用备份文件恢复
```
bundle exec rake gitlab:backup:restore RAILS_ENV=production
```
选项:
```
BACKUP=timestamp_of_backup (required if more than one backup exists)
```
类似这样:
```
Unpacking backup... [DONE]
Restoring database tables:
-- create_table("events", {:force=>true})
-> 0.2231s
[...]
- Loading fixture events...[DONE]
- Loading fixture issues...[DONE]
- Loading fixture keys...[SKIPPING]
- Loading fixture merge_requests...[DONE]
- Loading fixture milestones...[DONE]
- Loading fixture namespaces...[DONE]
- Loading fixture notes...[DONE]
- Loading fixture projects...[DONE]
- Loading fixture protected_branches...[SKIPPING]
- Loading fixture schema_migrations...[DONE]
- Loading fixture services...[SKIPPING]
- Loading fixture snippets...[SKIPPING]
- Loading fixture taggings...[SKIPPING]
- Loading fixture tags...[SKIPPING]
- Loading fixture users...[DONE]
- Loading fixture users_projects...[DONE]
- Loading fixture web_hooks...[SKIPPING]
- Loading fixture wikis...[SKIPPING]
Restoring repositories:
- Restoring repository abcd... [DONE]
Deleting tmp directories...[DONE]
```
### 配置计划任务
```
cd /home/git/gitlab
sudo -u git -H editor config/gitlab.yml # 开启多久自动备份的时间
sudo -u git crontab -e # git用户的计划任务
```
增加如下的条目:
```
# 每天凌晨2点进行一次全备份
0 2 * * * cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:create RAILS_ENV=production
```
Gitlab日常备份和恢复