yidinghan / blog

文章记录
28 stars 2 forks source link

GitLab Service Migration #4

Open yidinghan opened 6 years ago

yidinghan commented 6 years ago

Article Reference Link

前文

Gitlab 服务数据迁移,可能几百年不会遇上一次,但也难免会碰上一次

其实数据迁移很可以很简单的拆分成两步

所以接下来的内容,可以不限于用在数据迁移上,还可以用在灾备,升级测试等等

记录下碰上的一些问题,以及需要注意的地方

正文

下面假设

数据备份

通过调用备份命令,产出备份文件

Docker Compose 版本

在 host 上面,Gitlab 的备份目录就在 /gitlab/backups ,在 container 里面是 /home/git/data/backups

在 docker-compose.yml 同级执行创建备份的命令,过一会,就可以在备份目录下面看见备份文件

$ docker-compose run --rm gitlab app:rake gitlab:backup:create
+ case ${1} in
+ initialize_system
+ map_uidgid
++ id -u git
+ USERMAP_ORIG_UID=1000
++ id -g git
+ USERMAP_ORIG_GID=1000
+ USERMAP_GID=1000
+ USERMAP_UID=1000
+ [[ 1000 != 1000 ]]
+ [[ 1000 != 1000 ]]
+ initialize_logdir

......

Creating backup archive: 1488873276_2017_03_07_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
Deleting old backups ... done. (0 removed)
$ ls /gitlab/backups/
1488873276_2017_03_07_gitlab_backup.tar

单独打包 SSH host keys

具体的原因,官方也说了,完整上下文看这里

Your machines SSH host keys are stored in a separate location at /etc/ssh/. Be sure to also backup and restore those keys to avoid man-in-the-middle attack warnings if you have to perform a full machine restore.

简单来说,这个 host key 就是机器的身份证,正常情况下每个机器都不一样。

但是在迁移过程中,若不把老机器的身份证带到新机器上,那客户端肯定就会有各种 error warning。

因为对客户端而言,校验服务器身份证是必然的安全需要。而新老机器肯定存在 id 不一,所以在客户端看来它两就是不一样。

为了解决这个问题,打包 host key,放到新机器上是上佳选择。

$ ls ssh/
ssh_host_dsa_key  ssh_host_dsa_key.pub  ssh_host_ecdsa_key  ssh_host_ecdsa_key.pub  ssh_host_ed25519_key  ssh_host_ed25519_key.pub  ssh_host_key  ssh_host_key.pub  ssh_host_rsa_key  ssh_host_rsa_key.pub
$ sudo tar -zcvf /gitlab-ssh-keys.tar.gz ssh/*

把这个 gitlab-ssh-keys.tar.gz 文件搞到新机器上解压恢复就好了。

数据恢复

Docker Compose 版本

目录环境和创建备份是一样的,只要备份文件夹下面由备份文件就可以进行恢复

在 docker-compose.yml 同级执行恢复命令,过一会,就可以在看到列出的可选备份文件

$ docker-compose run --rm gitlab app:rake gitlab:backup:restore
+ case ${1} in
+ initialize_system
+ map_uidgid
++ id -u git
+ USERMAP_ORIG_UID=1000
++ id -g git
+ USERMAP_ORIG_GID=1000
+ USERMAP_GID=1000
+ USERMAP_UID=1000
+ [[ 1000 != 1000 ]]
+ [[ 1000 != 1000 ]]
+ initialize_logdir
+ echo 'Initializing logdir...'
Initializing logdir...

......

++ ls /home/git/data/backups
++ grep _gitlab_backup
++ sort -r
+ for b in '$(ls ${GITLAB_BACKUP_DIR} | grep _gitlab_backup | sort -r)'
++ date --date=@1488878217_2017_03_07 '+%d %b, %G - %H:%M:%S %Z'
date: invalid date '@1488878217_2017_03_07'
+ echo '‣ 1488878217_2017_03_07_gitlab_backup.tar (created at )'
‣ 1488878217_2017_03_07_gitlab_backup.tar (created at )
+ echo

+ read -p 'Select a backup to restore: ' file
Select a backup to restore:
把选中的备份文件的文件名输入,就可以开始备份了,会有很多交互提问,一一填写即可
+ read -p 'Select a backup to restore: ' file
Select a backup to restore: 1488878217_2017_03_07_gitlab_backup.tar
+ [[ -z 1488878217_2017_03_07_gitlab_backup.tar ]]
+ [[ ! -f /home/git/data/backups/1488878217_2017_03_07_gitlab_backup.tar ]]
+ BACKUP=1488878217_2017_03_07
+ echo 'Running raketask gitlab:backup:restore...'
Running raketask gitlab:backup:restore...
+ exec_as_git bundle exec rake gitlab:backup:restore BACKUP=1488878217_2017_03_07
++ whoami
+ [[ root == git ]]
+ sudo -HEu git bundle exec rake gitlab:backup:restore BACKUP=1488878217_2017_03_07
Unpacking backup ... done
Before restoring the database we recommend removing all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.
Do you want to continue (yes/no)? yes
Removing all tables. Press Ctrl-C within 5 seconds to abort

......

Restoring lfs objects ...
done
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes
Deleting tmp directories ... done
done
done
done
done
done
done
$ ls /gitlab/backups/
1488878217_2017_03_07_gitlab_backup.tar

恢复 SSH host keys

$ tar -zxvf gitlab-ssh-keys.tar.gz
$ ls ssh/
ssh_host_dsa_key  ssh_host_dsa_key.pub  ssh_host_ecdsa_key  ssh_host_ecdsa_key.pub  ssh_host_ed25519_key  ssh_host_ed25519_key.pub  ssh_host_key  ssh_host_key.pub  ssh_host_rsa_key  ssh_host_rsa_key.pub
$ sudo cp ssh/* /gitlab/ssh/