Take your GitLab backup everyday if it works in Docker

I’m using GitLab in docker (because I love containerization) and I’m really okay with that because I can move the GitLab environment everywhere if I need and I can feel more elastic when I’m using docker.

GitLab backups are really so important because you are keeping all of the source code and maybe wiki maybe more than all of the source code and backup is the first thing if you have a service running at a critical level.

Make sure about your docker run command


I run my GitLab in docker with these command set:

[ercan@gitlab ~]$ docker run --detach \
  --hostname gitlab.ercanermis.com \
  --publish 443:443 --publish 80:80 --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume /srv/gitlab/config:/etc/gitlab \
  --volume /srv/gitlab/logs:/var/log/gitlab \
  --volume /srv/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

First of all, we need to get to know what’s the docker container name of the GitLab. In my setup, I’m using GitLab Community Edition and I gave the name as a “gitlab” for the docker. If you don’t know your gitlab docker’s name, there is an easy way to learn what it is.

[ercan@gitlab ~]$ docker ps -a | grep gitlab-ce

The last column shows the docker container name. Here is the output:

9c787206fbda gitlab/gitlab-ce:latest "/assets/wrapper" 8 hours ago Up 8 hours (healthy) 0.0.0.0:22->22/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp gitlab

Take a backup and know the backup path

Actually GitLab backup is really easy. Here is the command for the trigger backup mechanism of GitLab.

docker exec gitlab gitlab-rake gitlab:backup:create DIRECTORY=gitlab

Trigger command isn’t enough because you should take a backup of gitlab.rb and gitlab-secrets.json files are located in /srv/gitlab/config/ path. I prefer to copy (with cp command) these files to /srv/gitlab/data/backups/ path because gitlab-rake is exporting to the same directory.

My GitLab Docker Backup backup bash script looks like this:

#!/bin/bash
#Author: Ercan Ermis - https://ercanermis.com

docker exec gitlab gitlab-rake gitlab:backup:create DIRECTORY=gitlab
cp /srv/gitlab/config/gitlab.rb /srv/gitlab/data/backups/$(date +%F_%H-%M)_gitlab.rb
cp /srv/gitlab/config/gitlab-secrets.json /srv/gitlab/data/backups/$(date +%F_%H-%M)_gitlab-secrets.json

Which files do you have after run backup bash?

gitlab is generating the backup file name like1600262658_2020_09_16_13.3.6_gitlab_backup.tar It means epochtime_year_month_day_gitlabVersion_gitlab_backup.tar That’s the reason why I use the $date function when copying gitlab.rb and gitlab-secrets.json to the /srv/gitlab/data/backups/ folder.

I’m using aws S3 service to keeping my backups securely and redundant and when the backup is completed and ready in /srv/gitlab/data/backups/ folder, I’m syncing backed up files to s3 with aws s3 sync /srv/gitlab/data/backups/ s3://ercanermis-backup/gitlab/ command and deleting the backup file on the server.

You can also use your own solution to keep your backup redundant and securely like SMB, sFTP or another block storage solution from another provider.

Final: Let’s Automate Backup

Here is the final bash for automating GitLab Docker Backup located in /opt/gitlab-backup.sh

#!/bin/bash
#Author: Ercan Ermis - https://ercanermis.com

docker exec gitlab gitlab-rake gitlab:backup:create DIRECTORY=gitlab
cp /srv/gitlab/config/gitlab.rb /srv/gitlab/data/backups/$(date +%F_%H-%M)_gitlab.rb
cp /srv/gitlab/config/gitlab-secrets.json /srv/gitlab/data/backups/$(date +%F_%H-%M)_gitlab-secrets.json
aws s3 sync /srv/gitlab/data/backups/ s3://ercanermis-backup/gitlab/ && rm -rf /srv/gitlab/data/backups/*

After create your .sh file, you can use your crontab for trigger run your own /opt/gitlab-backup.sh