Gitlab docker container - backup and restore

Posted on Mon 17 April 2017 by Pavlo Khmel

Two examples:

  • backup and restore GitLab docker container with volumes
  • backup and restore only useful data

Gitlab is running in Docker container on server a.example.com. Container was create by command:

docker run --detach --hostname gitlab.example.com   
--publish 192.168.0.101:443:443   
--publish 192.168.0.101:80:80   
--publish 192.168.0.101:2222:22   
--name gitlab-example --restart always   
--volume /storage/gitlab/config:/etc/gitlab   
--volume /storage/gitlab/logs:/var/log/gitlab   
--volume /storage/gitlab/data:/var/opt/gitlab   
gitlab/gitlab-ce:latest

Full backup

You need to complete 4 steps:

  • backup container gitlab-example
  • backup volume /etc/gitlab
  • backup volume /var/log/gitlab
  • backup volume /var/opt/gitlab

Commands:

docker stop gitlab-example
docker commit gitlab-example local/gitlab-example-container-20170417
docker save local/gitlab-example-container-20170417 > /root/gitlab-example-container-20170417.tar
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu tar cvf /backup/gitlab-example-volume-etc-20170417.tar /etc/gitlab
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu tar cvf /backup/gitlab-example-volume-log-20170417.tar /var/log/gitlab
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu tar cvf /backup/gitlab-example-volume-opt-20170417.tar /var/opt/gitlab

Full restore

Restore on second server b.example.com. Example CentOS 7.3:

yum -y install epel-release
yum -y install docker-io
systemctl enable docker
systemctl start docker

Upload 4 files from server a.example.com to b.example.com:

gitlab-example-container-20170417.tar
gitlab-example-volume-etc-20170417.tar
gitlab-example-volume-log-20170417.tar
gitlab-example-volume-opt-20170417.tar

Load image:

docker load -i gitlab-example-container-20170417.tar 

Create container:

docker create --hostname gitlab.example.com   
--publish 192.168.0.102:443:443   
--publish 192.168.0.102:80:80   
--publish 192.168.0.102:2222:22   
--name gitlab-example --restart always   
--volume /storage/gitlab/config:/etc/gitlab   
--volume /storage/gitlab/logs:/var/log/gitlab   
--volume /storage/gitlab/data:/var/opt/gitlab   
local/gitlab-example-container-20170417

Restore volumes:

mkdir -p /storage/gitlab/config
mkdir -p /storage/gitlab/logs
mkdir -p /storage/gitlab/data
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu bash -c "cd /etc && tar xvf /backup/gitlab-example-volume-etc-20170417.tar --strip 1"
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu bash -c "cd /var/log && tar xvf /backup/gitlab-example-volume-log-20170417.tar --strip 2"
docker run --rm --volumes-from gitlab-example -v $(pwd):/backup ubuntu bash -c "cd /var/opt && tar xvf /backup/gitlab-example-volume-opt-20170417.tar --strip 2"

Start container:

docker start gitlab-example

Backup and restore only useful data

Backup cron job daily:

# crontab -l
0 7 * * * /bin/docker exec -i gitlab-example /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

By default backup files will be saved inside container to: /var/opt/gitlab/backups/

On host system (in my example) backup is located on host system: /storage/gitlab/data/backups/

Restore

Find available backup files:

# docker exec -it gitlab-example ls /var/opt/gitlab/backups/
1492443013_2017_04_17_gitlab_backup.tar

Restore backup with time stamp 1492443013_2017_04_17

docker exec -it gitlab-example /opt/gitlab/bin/gitlab-rake gitlab:backup:restore BACKUP=1492443013_2017_04_17