Backs up all repositories from a Gitlab instance via API as git gzip bundles
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-11-05 15:06:48 -05:00
.env.example Initial commit 2025-11-05 15:06:48 -05:00
.gitignore Initial commit 2025-11-05 15:06:48 -05:00
gitlab-backup.sh Initial commit 2025-11-05 15:06:48 -05:00
README.md Initial commit 2025-11-05 15:06:48 -05:00

GitLab API Backup Script

A bash script that backs up all repositories from a self-hosted GitLab instance via the GitLab API as compressed git bundles.

Features

  • Backs up all repositories (including archived ones)
  • Uses bare clones for efficient disk usage
  • Creates verified git bundles
  • Compresses bundles with gzip
  • Organizes backups by group and repository name
  • Handles nested groups (flattened with underscores)
  • Summary statistics

Requirements

  • bash
  • curl
  • jq
  • git
  • gzip

Installation

  1. Clone or download this repository

  2. Make the script executable:

    chmod +x gitlab-backup.sh
    

Usage

The script accepts configuration via command line arguments or environment variables.

Command Line Arguments

./gitlab-backup.sh [gitlab_url] [api_token] [backup_dir]
  • gitlab_url: Your GitLab instance URL (e.g., https://gitlab.example.com)
  • api_token: GitLab API token with read access to all repositories
  • backup_dir: Directory where backups will be stored

Environment Variables

Alternatively, you can set these environment variables:

  • GITLAB_URL: GitLab instance URL
  • GITLAB_TOKEN: GitLab API token
  • BACKUP_DIR: Backup directory path

Note: Command line arguments take precedence over environment variables.

Examples

Using command line arguments:

./gitlab-backup.sh https://gitlab.example.com glpat-xxxxxxxxxxxx /backups/gitlab

Using environment variables:

export GITLAB_URL="https://gitlab.example.com"
export GITLAB_TOKEN="glpat-xxxxxxxxxxxx"
export BACKUP_DIR="/backups/gitlab"
./gitlab-backup.sh

Creating GitLab API Token

  1. Log in to your GitLab instance
  2. Go to User Settings > Access Tokens
  3. Create a token with read_api and read_repository scopes
  4. Copy the token (it won't be shown again)

Restoring from Backup

To restore a repository from a backup:

# Decompress
gunzip group_name/repo_name.gz

# Clone from bundle
git clone group_name/repo_name.bundle restored_repo

# Or verify bundle contents
git bundle verify group_name/repo_name.bundle