Backs up all repositories from a Gitlab instance via API as git gzip bundles
- Shell 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .env.example | ||
| .gitignore | ||
| gitlab-backup.sh | ||
| README.md | ||
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
-
Clone or download this repository
-
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 repositoriesbackup_dir: Directory where backups will be stored
Environment Variables
Alternatively, you can set these environment variables:
GITLAB_URL: GitLab instance URLGITLAB_TOKEN: GitLab API tokenBACKUP_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
- Log in to your GitLab instance
- Go to User Settings > Access Tokens
- Create a token with
read_apiandread_repositoryscopes - 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