How to backup and restore Gitlab configuration on Ubuntu OS

How to backup and restore Gitlab configuration on Ubuntu OS

gitlab_config_backup

Here in this article we will see how we can backup the gitlab configuration data. This backup can later be utilized to restore the gitlab configuration data in case of any misconfiguration or configuration data loss.

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Test Environment

Ubuntu 20.04

Procedure

Step1: Manual backup of gitlab configuration

By default the Omnibus gitlab package installation stores the configuration data at the following ‘/etc/gitlab/’ location. We can take the tar backup of this folder path and store the backup in a different location. Ideally it is better to store the backup on a NFS mount point from a different server to avoid data loss.

gitadmin@gitserver:~$ sudo gitlab-ctl backup-etc --backup-path /home/gitadmin/gitlab_backup/
Could not find '/home/gitadmin/gitlab_backup' directory. Creating.
Running configuration backup
Creating configuration backup archive: gitlab_config_1654626300_2022_06_07.tar
/etc/gitlab/
/etc/gitlab/trusted-certs/
/etc/gitlab/gitlab.rb
/etc/gitlab/initial_root_password
/etc/gitlab/gitlab-secrets.json
Configuration backup archive complete: /home/gitadmin/gitlab_backup/gitlab_config_1654626300_2022_06_07.tar
gitadmin@gitserver:~$ sudo tar -tvf /home/gitadmin/gitlab_backup/gitlab_config_1654626300_2022_06_07.tar
tar: Removing leading `/' from member names
drwxrwxr-x root/root         0 2022-06-07 18:29 /etc/gitlab/
drwxr-xr-x root/root         0 2022-06-07 18:29 /etc/gitlab/trusted-certs/
-rw------- root/root    136773 2022-06-07 18:29 /etc/gitlab/gitlab.rb
-rw------- root/root       749 2022-06-07 18:29 /etc/gitlab/initial_root_password
-rw------- root/root     19205 2022-06-07 18:32 /etc/gitlab/gitlab-secrets.json

As you can see the backup appends today’s date and create a tar file backup.

Step2: Restore gitlab configuration backup

Before restoring the backup take a backup of the current configuration data which is having problem as shown below.

gitadmin@gitserver:~$ sudo mv /etc/gitlab /etc/gitlab_bad_config_$(date +%Y%m%d)

Now we can restore the backup that was taken in Step1 to restore the configuration data.

gitadmin@gitserver:/etc$ sudo tar -xvf /home/gitadmin/gitlab_backup/gitlab_config_1654626300_2022_06_07.tar -C /
gitadmin@gitserver:/etc$ ls -ltr /etc/gitlab
total 164
-rw------- 1 root root 136773 Jun  7 18:29 gitlab.rb
-rw------- 1 root root    749 Jun  7 18:29 initial_root_password
drwxr-xr-x 2 root root   4096 Jun  7 18:29 trusted-certs
-rw------- 1 root root  19205 Jun  7 18:32 gitlab-secrets.json

Step3: Run gitlab reconfigure

gitadmin@gitserver:/etc$ sudo gitlab-ctl reconfigure
...
Running handlers:
Running handlers complete
Cinc Client finished, 4/848 resources updated in 12 seconds

Warnings:
Environment variable LANG specifies a non-UTF-8 locale. GitLab requires UTF-8 encoding to function properly. Please check your locale settings.

gitlab Reconfigured!

Step4: Schedule a daily configuration backup

In order to take a daily backup of the configuration data, we can schedule a cronjob as shown below. This will take a backup every day at 5 am.

gitadmin@gitserver:~$ sudo crontab -l | grep -v ^#
0 5 * * * gitlab-ctl backup-etc --backup-path /home/gitadmin/gitlab_backup/

For now i update the cronjob as show below to take backup at 00:40 as shown below.

gitadmin@gitserver:~$ sudo crontab -l | grep -v ^#
40 0 * * * gitlab-ctl backup-etc --backup-path /home/gitadmin/gitlab_backup/
gitadmin@gitserver:~$ 
gitadmin@gitserver:~$ 
gitadmin@gitserver:~$ 
gitadmin@gitserver:~$ sudo ls -ltr /home/gitadmin/gitlab_backup/
total 160
-rw------- 1 root root 163840 Jun  7 23:55 gitlab_config_1654626300_2022_06_07.tar
gitadmin@gitserver:~$ 
gitadmin@gitserver:~$ sudo ls -ltr /home/gitadmin/gitlab_backup/
total 320
-rw------- 1 root root 163840 Jun  7 23:55 gitlab_config_1654626300_2022_06_07.tar
-rw------- 1 root root 163840 Jun  8 00:40 gitlab_config_1654629003_2022_06_08.tar

As you can see it has generated a new backup at 00:40 with today’s date.

Hope you enjoyed reading this article. Thank you..