็†ฑ้–€ๅˆ†้กž
 ่ผ‰ๅ…ฅไธญ…
็›ฎ้Œ„

๐Ÿ Python GitLab API Backup Script Tutorial: Automated Daily Backup with Cron Job

    ๐Ÿ 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

    1. Login to GitLab → Profile → Access Tokens
    2. Set scopes: api, read_repository
    3. 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_DIR to a mounted NAS or remote drive.
    • Integrate with rclone to 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

    — WWFandy · DevOps Automation Notes

    ๐Ÿ”— ๅˆ†ไบซ้€™็ฏ‡ LINE Facebook X

    ๆฒ’ๆœ‰็•™่จ€:

    ๅผต่ฒผ็•™่จ€

    ๅญ—็ดš