Automatic Delete Gitlab Backup

Sameer

I am taking backup of whole gitlab everything including projects, branches and everything in backup . tar file with the help of jenkins everyday.

I want to delete the backup periodically. Like I want to delete the backup after every 30 days. I want the last backup to be deleted automatically.

Thanks for taking the time to be thorough in your request, it really helps! :blush:

Why? Jenkins (not that I remember much about it) is for CI/CD not backing up other stuff. And GitLab comes with gitlab-backup that makes a nice backup, and configuration options for running it periodically and for removing backups made using it that have reached a certain age - the check of old backups happens after it has taken a new, so you need space for one more backup than you wish to keep, and as taking a new backup might be faster (it might also be slower, but that’s not an issue here) than taking the one you want to delete did, be aware that the old backup might not be n*86400 seconds (86400 is the number of seconds in a day).

The documentation on taking backups is quite good, and also covers a lot of what needs to be done alternatively, so you should read that.

1 Like

@satharkar Best way is just use something like this:

find $BACKUPDIR -type f -ctime +1 -exec rm -f {} \;

replace $BACKUPDIR with the location of your tar files, and change ctime +1 to for example +30 to delete over 30 days of backups.

In fact I use something like this (some of it depends on the distro I use Debian/Ubuntu hence dpkg command for getting version info this would have to be substituted with yum/rpm on a RHEL based distro):

#!/bin/bash
#
# Script to backup gitlab-ce

# Variables
PKGNAME=gitlab
PKGVER=`dpkg -l | grep -i gitlab | awk '{print $3}'`
GITLABCONFDIR=/etc/gitlab
GITLABBACKUPS=/var/opt/gitlab/backups
BACKUPDIR=/backups/gitlab
BACKUPDATE=`date '+%F'`

# Cleanup old backups and wait a minute
find $BACKUPDIR -type f -ctime +1 -exec rm -f {} \;
sleep 60

# Remove existing gitlab backups
rm -f $GITLABBACKUPS/*

# Backup Gitlab
sudo -u git -H gitlab-rake gitlab:backup:create
tar cvjpf $BACKUPDIR/$PKGNAME-$PKGVER-data-$BACKUPDATE.tar.bz2 $GITLABBACKUPS/*.tar

# Backup Gitlab config
tar cvjpf $BACKUPDIR/$PKGNAME-$PKGVER-config-$BACKUPDATE.tar.bz2 $GITLABCONFDIR

and you will get files backed up with a name of:

-rw-r--r-- 1 root root 637K Jan 26 00:16 gitlab-13.8.0-ce.0-config-2021-01-26.tar.bz2
-rw-r--r-- 1 root root 637K Jan 27 00:16 gitlab-13.8.0-ce.0-config-2021-01-27.tar.bz2
-rw-r--r-- 1 root root 2.9G Jan 26 00:16 gitlab-13.8.0-ce.0-data-2021-01-26.tar.bz2
-rw-r--r-- 1 root root 2.9G Jan 27 00:16 gitlab-13.8.0-ce.0-data-2021-01-27.tar.bz2
-rw-r--r-- 1 root root 657K Jan 28 00:24 gitlab-13.8.1-ce.0-config-2021-01-28.tar.bz2
-rw-r--r-- 1 root root 4.7G Jan 28 00:24 gitlab-13.8.1-ce.0-data-2021-01-28.tar.bz2

and you will have exactly the version of gitlab in the filename for you to know which needs to be installed to restore to.