[GitLab] 백업과 복구

이번 포스트에서는 Docker를 이용한 GitLab의 백업과 복구를 해보려합니다.
어렵고 복잡할 것 같았는데 의외로 간단히 복구가 되네요.

뭔가 3부작 느낌인데..

아래의 내용은 GitLab 백업 공식 페이지를 참고했습니다.

백업 파일 생성

먼저 실행중인 Docker GitLab의 데이터 백업 파일을 생성합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% docker exec -t gitlab gitlab-backup create
2020-06-04 01:45:41 +0000 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2020-06-04 01:46:12 +0000 -- done
2020-06-04 01:46:12 +0000 -- Dumping repositories ...
* devsawd/hello-world (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [DONE]
[SKIPPED] Wiki
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping uploads ...
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping builds ...
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping artifacts ...
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping pages ...
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping lfs objects ...
2020-06-04 01:46:13 +0000 -- done
2020-06-04 01:46:13 +0000 -- Dumping container registry images ...
2020-06-04 01:46:13 +0000 -- [DISABLED]
Creating backup archive: 1591235174_2020_06_04_13.0.3_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.

제가 생성한 hello-world 프로젝트도 보입니다.

백업 파일 확인

저는 docker-compose.yml에 volumes를 이용해 backups 폴더를 지정해놓았습니다.
이부분에 대해 이해가 안되는 분들은 Docker를 이용해 GitLab을 설치해보자를 참고해주세요

1
2
volumes:
- '$GITLAB_HOME/gitlab/backups:/var/opt/gitlab/backups'

backups 폴더로 이동해 백업 데이터가 잘 생성되었는지 확인해봅니다.

1
2
3
4
% cd backups
backups % ls
1591235174_2020_06_04_13.0.3_gitlab_backup.tar
backups %

GitLab 컨테이너 중지

백업 파일이 정상적으로 생성되었다면 실행중인 컨테이너를 종료합니다.

1
% docker stop gitlab

백업 파일 임시이동

백업파일을 임시적으로 이동시킵니다.
저는 하나 상위 폴더인 gitlab 폴더로 이동시켰습니다.

1
backups % mv 1591235174_2020_06_04_13.0.3_gitlab_backup.tar ../

데이터 파일 삭제

주의!! 아래의 명령어는 모든 데이터를 삭제합니다.

1
2
backups % cd ..
% rm -rf config backups data logs

GitLab 컨테이너 실행

1
2
% export GITLAB_HOME=$HOME // Linux의 경우 /src
% docker-compose up -d

백업 파일 backups 폴더로 복구

docker ps 명령어를 이용해 gitlab 컨테이너가 정상적으로 실행됐다면
gitlab 폴더로 임시 이동시켰던 백업파일을 다시 backups 폴더로 이동시킵니다.

1
% mv 1591235174_2020_06_04_13.0.3_gitlab_backup.tar backups

복구 시작

복구 실행 명령어를 실행합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
% docker exec -it gitlab gitlab-backup restore
Unpacking backup ... done
Before restoring the database, we will remove 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


2020-06-04 01:59:16 +0000 -- done
2020-06-04 01:59:16 +0000 -- Restoring repositories ...
* devsawd/hello-world ... [DONE]
2020-06-04 01:59:16 +0000 -- done
2020-06-04 01:59:16 +0000 -- Restoring uploads ...
2020-06-04 01:59:17 +0000 -- done
2020-06-04 01:59:17 +0000 -- Restoring builds ...
2020-06-04 01:59:17 +0000 -- done
2020-06-04 01:59:17 +0000 -- Restoring artifacts ...
2020-06-04 01:59:17 +0000 -- done
2020-06-04 01:59:17 +0000 -- Restoring pages ...
2020-06-04 01:59:17 +0000 -- done
2020-06-04 01:59:17 +0000 -- Restoring lfs objects ...
2020-06-04 01:59:17 +0000 -- done
This task will now rebuild the authorized_keys file.
You will lose any data stored in the authorized_keys file.
Do you want to continue (yes/no)? yes

Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need to restore these files manually.
Restore task is done.

devsawd/hello-world 프로젝트도 복구되었다는 로그가 보입니다.

로그인 후 확인

root, devsawd 계정도 잘 살아있고 프로젝트도 정상적으로 복구되었습니다!

마무리

명령어 몇번과 tar 복구파일 하나만으로 GitLab의 데이터들을 복구할 수 있었습니다.
일정 기간을 가지고 스냅샷을 찍듯 데이터를 백업하는 방법도 있는데…
~ 언제 하지? ~