AMP AMP

How to schedule Gitlab backup using Cron to a custom location on Ubuntu 21.04

To schedule Gitlab backup using Cron to custom location on Ubuntu 21.04

Introduction:

GitLab is an open-source DevOps tool that manages Git repositories in a web console for planning, monitoring, and securing projects.

Prerequisite:

Gitlab CE 14.4

Installation Procedure:

Step 1: Check the OS version and Gitlab installed version by using the following command

root@linuxhelp:~# gitlab-rake gitlab:env:info

System information
System:		Ubuntu 21.04
Current User:	git
Using RVM:	no
Ruby Version:	2.7.4p191
Gem Version:	3.1.4
Bundler Version:2.1.4
Rake Version:	13.0.6
Redis Version:	6.0.14
Git Version:	2.33.0.
Sidekiq Version:6.2.2
Go Version:	unknown

GitLab information
Version:	14.4.0
Revision:	51b27ab5805
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	PostgreSQL
DB Version:	12.7
URL:		http://gitlab.example.com
HTTP Clone URL:	http://gitlab.example.com/some-group/some-project.git
SSH Clone URL:	git@gitlab.example.com:some-group/some-project.git
Using LDAP:	no
Using Omniauth:	yes
Omniauth Providers: 

GitLab Shell
Version:	13.21.1
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
GitLab Shell path:		/opt/gitlab/embedded/service/gitlab-shell
Git:		/opt/gitlab/embedded/bin/git

Step 2: Take Gitlab Backup to the default backup location

root@linuxhelp:~# gitlab-ctl backup-etc
Running configuration backup
Creating configuration backup archive: gitlab_config_1636752504_2021_11_13.tar
/etc/gitlab/
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/trusted-certs/
/etc/gitlab/gitlab.rb
Configuration backup archive complete: /etc/gitlab/config_backup/gitlab_config_1636752504_2021_11_13.tar
Keeping all older configuration backups

Step 3: List the taken backup in the default backup location

root@linuxhelp:~# ls -l /etc/gitlab/config_backup/
total 152
-rw------- 1 root root 153600 Nov 13 02:58 gitlab_config_1636752504_2021_11_13.tar

Step 4: Making two directories for backup

root@linuxhelp:~# mkdir /backup1 /backup2

Step 5: Taking Gitlab Backup to the custom backup location

root@linuxhelp:~# gitlab-ctl backup-etc --backup-path /backup1
WARNING: /backup1 may be read by non-root users
Running configuration backup
Create configuration backup archive: gitlab_config_1636752618_2021_11_13.tar
/etc/gitlab/
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/trusted-certs/
/etc/gitlab/gitlab.rb
/etc/gitlab/config_backup/
/etc/gitlab/config_backup/gitlab_config_1636752504_2021_11_13.tar
Configuration backup archive complete: /backup1/gitlab_config_1636752618_2021_11_13.tar
Keeping all older configuration backups

Step 6: List the taken backup in the custom backup location

root@linuxhelp:~# ls -l /backup1
total 300
-rw------- 1 root root 307200 Nov 13 03:00 gitlab_config_1636752618_2021_11_13.tar

Step 7: Schedule the cron jobs in Crontab file with using Nano editor

root@linuxhelp:~# crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]: 

# crontab: installing new crontab
#Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
#indicating with different fields when the task will be run
#and what command to run for the task
#To define the time you can provide concrete values for
#minute (m), hour (h), day of month (dom), month (mon),
#and day of week (dow) or use '*' in these fields (for 'any').
#Notice that tasks will be started based on the cron's system
#daemon's notion of time and timezones.
#Output of the crontab jobs (including errors) is sent through
#email to the user the crontab file belongs to (unless redirected).
#For example, you can run a backup of all your user accounts
#at 5 a.m every week with:
#0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#For more information see the manual pages of crontab(5) and cron(8)
#m h  dom mon dow   command
15 3 */1 */1 * gitlab-ctl backup-etc --backup-path /backup1 && cd /backup1 && cp $(ls -t | head -n1) /backup2

Step 8: List the taken backup taken by the cron at backup2 directory

root@linuxhelp:~# ls -l /backup2
total 300
-rw------- 1 root root 307200 Nov 13 03:15 gitlab_config_1636753503_2021_11_13.tar

By this to schedule Gitlab backup using Cron to custom location on Ubuntu 21.04 comes to an end

FAQ
Q
What is the default backup location for Gitlab?
A
The default backup location for Gitlab/etc/gitlab/config_backup/
Q
What are the functions of GitLab?
A
GitLab does Project management, issue tracking, branches, code reviews, and continuous integration.
Q
How to check the Gitlab version in CLI?
A
You can check the Gitlab version in CLI by "gitlab-rake gitlab:env:info"
Q
How to set a cron job for a specific user?
A
To set cron job for a specific user by "crontab -e -u "
Q
What is the command for backup Gitlab in the custom directory?
A
The command for backup Gitlab in the custom directory is "gitlab-ctl backup-etc --backup-path "