Skip to content

Instantly share code, notes, and snippets.

@thorian93
Last active February 26, 2024 09:08
Show Gist options
  • Save thorian93/eafe52f844a502940eda1d3589a40429 to your computer and use it in GitHub Desktop.
Save thorian93/eafe52f844a502940eda1d3589a40429 to your computer and use it in GitHub Desktop.
Gitlab Backup Script
#!/bin/bash
#
# Written by: Robin Gierse - info@thorian93.de - on 20191024
#
# Purpose:
# This script backs up gitlab
#
# Version: 0.1 on 20191024
# Version: 0.1 on 20191031 - Fix cleanup routine
#
# Usage:
# ./backup-gitlab.sh
# Variables:
gitlab_logfile="/var/log/gitlab/backup/$(date +%Y%m%d_%H%M%S)_gitlab_backup.log"
gitlab_backup_root="/var/opt/gitlab/backups"
gitlab_backup_dest="${gitlab_backup_root}"
gitlab_backup_retention="7" # Delete backups older than number of days.
#See /etc/gitlab/gitlab.rb for this line: gitlab_rails['backup_keep_time'] = 604800 # This is the number of seconds backups will be kept (equals 7 days)
# Functions:
_initialize() {
echo "$(date): Starting Gitlab Backup."
touch "${gitlab_logfile}"
}
_gitlab_backup() {
echo "$(date) Backing up Gitlab Database:"
gitlab-rake gitlab:backup:create
echo "$(date) Gitlab Database done."
echo "$(date) Backing up Gitlab Configuration:"
tar -caf "${gitlab_backup_dest}/$(date +%Y%m%d)_etc-gitlab.tar.bz2" "/etc/gitlab"
echo "$(date) Gitlab Configuration done."
}
_gitlab_cleanup() {
echo 'Deleting old backups:'
rm -rvf $(find ${gitlab_backup_root} -mtime +${gitlab_backup_retention})
echo "$(date) Cleanup done."
}
_finalize() {
echo "$(date): Finished Gitlab Backup."
exit 0
}
# Main
_initialize >> "${gitlab_logfile}" 2>&1
_gitlab_backup >> "${gitlab_logfile}" 2>&1
_gitlab_cleanup >> "${gitlab_logfile}" 2>&1
_finalize >> "${gitlab_logfile}" 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment