Source: Dev.to | Jan 21, 2026 (UTC)
TL;DR: This guide provides an automated, cost-effective solution to mitigate data loss risks by backing up all GitHub repositories. It leverages a Python script to clone repositories, archive them, and upload them to an AWS S3 bucket, orchestrated by GitHub Actions for scheduled execution.
- The solution uses a Python script with
requestsfor GitHub API interaction,subprocessfor Git operations,shutilfor archiving, andboto3for S3 uploads. - Authentication requires a GitHub Personal Access Token (PAT) with
reposcope and AWS IAM user credentials (Access Key ID, Secret Access Key) withs3:PutObject,s3:ListBucket, ands3:GetObjectpermissions on the target S3 bucket. - Automation is achieved via GitHub Actions, configured with a
cronschedule for daily backups andworkflow\_dispatchfor manual triggers, securely passing sensitive credentials as repository secrets.
Backup All GitHub Repositories to S3 Bucket Automatically
As a Senior DevOps Engineer and Technical Writer for TechResolve, I understand the critical importance of data resilience. In today’s cloud-native landscape, while platforms like GitHub offer high availability, relying solely on a single vendor for your invaluable source code can be a significant risk. Disasters, accidental deletions, or even account compromises can lead to irreparable data loss if not properly safeguarded.
Manual backups are tedious, error-prone, and often overlooked, especially in fast-paced development environments. The cost of specialized third-party backup solutions can also be prohibitive for many teams. This tutorial addresses these challenges by providing a robust, automated, and cost-effective solution to back up all your GitHub repositories directly to an Amazon S3 bucket.
By the end of this guide, you will have a fully automated system that regularly pulls all your GitHub repositories and archives them securely in S3, giving you peace of mind, improved disaster recovery capabilities, and full control over your code’s backups.
Before we dive into the automation, ensure you have the following in place:
- GitHub Account: Access to the repositories you wish to back up.
- GitHub Personal Access Token (PAT): With appropriate scopes. For private repositories, the
reposcope (all sub-options) is required. For public repositories only,public_repois sufficient. We recommend generating a token specifically for this backup process. - AWS Account: With permissions to create S3 buckets and IAM users/roles.
- AWS S3 Bucket: A bucket configured in your AWS account to store the backups.
- AWS IAM User or Role: With programmatic access (Access Key ID and Secret Access Key) and permissions to perform
s3:PutObjectands3:ListBucketactions on the designated S3 bucket. - Python 3.x: Installed on the system where the script will run (or implicitly available in a GitHub Actions runner).
- pip: Python’s package installer.
- Git: Command-line Git installed (also implicitly available in GitHub Actions runners).
Step-by-Step Guide: Automating Your GitHub Backups
Step 1: Create a GitHub Personal Access Token (PAT)
This token will allow our script to authenticate with GitHub’s API and clone your repositories. Treat it like a password.
- Go to your GitHub profile settings.
- Navigate to “Developer settings” > “Personal access tokens” > “Tokens (classic)”.
- Click “Generate new token” > “Generate new token (classic)”.
- Give it a descriptive name (e.g., “S3-Backup-Script”).
- Set an appropriate expiration (e.g., 90 days, 1 year, or no expiration if managed securely). Remember to rotate it regularly.
- Under “Select scopes”, check
repo(all sub-options) to ensure it can access both public and private repositories. If you only have public repos to back up,public_repowill suffice. - Click “Generate token”.
- IMPORTANT: Copy the token immediately. You will not be able to see it again. Store it securely.
Step 2: Configure AWS S3 Bucket and IAM Permissions
We need an S3 bucket to store the archives and an IAM entity with the necessary permissions.
- Create an S3 Bucket: If you don’t have one, navigate to the S3 service in your AWS Console and create a new bucket. Choose a unique name and region. For security, consider enabling server-side encryption and versioning on the bucket.
-
Create an IAM User (or Role):
- Go to the IAM service in your AWS Console.
- Navigate to “Users” > “Add user”.
- Give the user a name (e.g., “github-backup-user”) and select “Programmatic access”.
- On the “Permissions” page, choose “Attach existing policies directly” and then “Create policy”.
- Use the JSON tab to define a policy like this, replacing
your-s3-bucket-namewith your actual bucket name:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:ListBucket", "s3:GetObject" ], "Resource": [ "arn:aws:s3:::your-s3-bucket-name/*", "arn:aws:s3:::your-s3-bucket-name" ] } ] }This policy grants permissions to upload objects, list the bucket content, and retrieve objects (for verification if needed).
- Save the policy, then attach it to your newly created IAM user.
- Complete the user creation process. Copy the Access Key ID and Secret Access Key. Store them securely.
Step 3: Develop the Backup Script (Python)
This Python script will fetch your repositories, clone them, create archives, and upload them to S3. Create a file named backup_github.py.
First, install the necessary Python libraries:
pip install requests boto3
Enter fullscreen mode
Exit fullscreen mode



Залишити відповідь