Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

Take your GitLab backup everyday if it works in Docker

Ercan, September 16, 2020April 24, 2021

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

Share on Social Media
x facebook linkedin reddit
DevOps Docker backupdevopsdockergitlab

Post navigation

Previous post
Next post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes