๐ Python GitLab API Backup Script Tutorial: Automated Daily Backup with Cron Job
Regular repository backups are essential for ensuring data safety in DevOps environments. In this tutorial, we’ll build an automated Python backup script using the GitLab REST API. The script will back up all repositories daily and run automatically via cron job, providing a lightweight and reliable backup solution for both individuals and teams.
๐ฆ 1. Environment Setup
# Install required packages
sudo apt update
sudo apt install -y python3 python3-pip
pip install python-gitlab requests
Generate a GitLab Personal Access Token
- Login to GitLab → Profile →
Access Tokens - Set scopes:
api,read_repository - Copy and securely store your token
๐ง 2. Python Backup Script
#!/usr/bin/env python3
import gitlab
import os
import datetime
# GitLab connection
GITLAB_URL = "https://gitlab.com"
PRIVATE_TOKEN = "YOUR_TOKEN_HERE"
BACKUP_DIR = "/home/admin/gitlab_backup"
# Initialize GitLab API
gl = gitlab.Gitlab(GITLAB_URL, private_token=PRIVATE_TOKEN)
gl.auth()
# Ensure backup directory exists
os.makedirs(BACKUP_DIR, exist_ok=True)
today = datetime.date.today().strftime("%Y-%m-%d")
# Iterate through projects
for project in gl.projects.list(owned=True, all=True):
name = project.path_with_namespace.replace("/", "_")
file_path = f"{BACKUP_DIR}/{name}_{today}.tar.gz"
print(f"⬇️ Downloading {project.name} ...")
try:
export = project.exports.create()
export.refresh()
export.download(streamed=True, action="download", filepath=file_path)
print(f"✅ Backup completed: {file_path}")
except Exception as e:
print(f"❌ Error: {e}")
Run it manually for testing:
python3 gitlab_backup.py
๐งน 3. Automatically Delete Old Backups
import time
RETENTION_DAYS = 7
for file in os.listdir(BACKUP_DIR):
fpath = os.path.join(BACKUP_DIR, file)
if os.path.isfile(fpath):
mtime = os.path.getmtime(fpath)
if (time.time() - mtime) > RETENTION_DAYS * 86400:
os.remove(fpath)
print(f"๐งน Removed old file: {file}")
๐ 4. Automate with Cron Job
crontab -e
# Run every day at 3AM
0 3 * * * /usr/bin/python3 /home/admin/gitlab_backup.py >> /var/log/gitlab_backup.log 2>&1
Check logs at /var/log/gitlab_backup.log for execution results.
๐ 5. Extended Use Cases
- Change
BACKUP_DIRto a mounted NAS or remote drive. - Integrate with
rcloneto upload backups to Google Drive or Dropbox. - Combine with Ansible + GitLab backup automation for multi-layer protection.
๐งญ Checklist
✅ Create GitLab Token with correct scopes
✅ Write and test the Python backup script
✅ Implement log retention and cleanup
✅ Add cron job for daily execution
✅ Verify backup logs regularly
๐ Conclusion
Automating GitLab backups via Python gives you full control without depending on CI/CD pipelines. It’s simple, flexible, and effective for small teams or personal projects. With scheduled cron jobs, you’ll never worry about lost repositories again.
๐ Related Articles
- ⚙️ Network Automation Backup with Ansible + GitLab
- ๐พ Linux rsync Backup Automation
- ๐ก Linux Security Hardening Guide
— WWFandy · DevOps Automation Notes
ๆฒๆ็่จ:
ๅผต่ฒผ็่จ