> For the complete documentation index, see [llms.txt](https://xn--6o0a585a.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xn--6o0a585a.gitbook.io/devops/fu-wu/gitlab-qian-yi-shu-ju.md).

# GitLab迁移数据

#### 一、备份数据安装相同版本gitlab

备份时需要保持gitlab处于正常运行状态\
保持要迁移的 gitlab 和目前 gitlab 版本一致

```bash
# 执行如下命令开始备份
gitlab-rake gitlab:backup:create
# 备份文件在
cd /var/opt/gitlab/backups/
```

#### `Yum`安装`GitLab` <a href="#toc-heading-11" id="toc-heading-11"></a>

下载指定版本的 `gitlab`，可以在清华大学镜像站去选择：[地址](https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/)

```bash
wget http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.4.2-ce.0.el7.x86_64.rpm
```

```bash
$ rpm -i gitlab-ce-14.4.2-ce.0.el7.x86_64.rpm
```

#### 配置 `GitLab`站点 `Url`和端口号 <a href="#toc-heading-12" id="toc-heading-12"></a>

`GitLab`默认的配置文件路径是 `/etc/gitlab/gitlab.rb`

默认的站点`Url`配置项是：`external_url 'http://gitlab.example.com`

这里我将`GitLab`站点Url修改为 `http://127.0.0.1:8000` 也可以用域名代替 `IP`，这里根据自己需求来即可

```bash
# 修改配置文件
sudo vi /etc/gitlab/gitlab.rb
# 配置首页地址（大约在第15行）
external_url 'http://127.0.0.1:8000'
# 开放端口号
firewall-cmd --zone=public --add-port=8000/tcp --permanent
# 重启防火墙
systemctl restart firewalld
# 查看是否成功
firewall-cmd --zone=public --query-port=8000/tcp
```

启动并访问`GitLab`

```bash
# 重新配置并启动
sudo gitlab-ctl reconfigure
# 完成后将会看到如下输出
Running handlers:
Running handlers complete
Chef Infra Client finished, 10/776 resources updated in 45 seconds
gitlab Reconfigured!
# 启动 gitlab
gitlab-ctl restart
# 查看启动详细信息
systemctl status gitlab-runsvdir.service
```

`将设置的域名DNS解析到服务器IP，或者修改本地host将域名指向服务器IP`

进入首页，随后进行登录，管理员账号默认用户名是`root`。

初始化密码可以在 `GitLab`初始化文件查看

```bash
$ cat /etc/gitlab/initial_root_password
# 复制Password后面的内容即可
Password: E+EA7WZie9zJbMQ2gwISeVN/We9DBZmYsMFpbjzhYcc=
```

#### 二、数据恢复

```csharp
#停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
#修改权限，如果是从本服务器恢复可以不修改
chmod 777 /var/opt/gitlab/backups/1530156812_2021_06_28_10.8.4_gitlab_backup.tar
#从1530156812_2021_06_28_10.8.4编号备份中恢复
gitlab-rake gitlab:backup:restore BACKUP=1530156812_2021_06_28_10.8.4
#按照提示输入两次yes并回车
# 启动GitLab   
gitlab-ctl start
```

#### 三、自动备份与清理

```ruby
# 系统定时备份任务
crontab -e
# 每天2点定时备份
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
# 重启crontab
systemctl restart crond
# 定时清理备份
# gitLab已经支持，配置gitlab来实现自动清理功能
vim /etc/gitlab/gitlab.rb
# 将backup_keep_time的配置取消注释，根据需要设置自动清理多少天前的备份，设置备份保留7天（7*3600*24=604800）
gitlab_rails['backup_keep_time'] = 604800
#重新加载gitlab的配置文件
gitlab-ctl reconfigure
```
