GitHub Gists Backup Tool
May 4, 2025 · View on GitHub
Last Updated: May 4th, 2025
A bash script for programmatically backing up your GitHub Gists to a local directory.
Overview
This tool allows you to create and maintain a complete backup of all your GitHub Gists (both public and private). It works incrementally, meaning you can run it periodically to keep your backups up-to-date without duplicating content.
Features
- Separate Storage: Public and private Gists are stored in separate directories for better organization
- Smart Naming: Uses the first filename of each Gist to create meaningful directory names
- Incremental Backups: Only updates Gists that have changed since the last backup
- Markdown Extraction: Automatically extracts all markdown files into a flat directory structure
- Pagination Safety: Includes limits to prevent infinite loops when fetching large collections
- Detailed Logging: Provides comprehensive logs of the backup process
- Error Handling: Robust error detection and reporting
How It Works
The script:
- Fetches all your Gists using the GitHub API with proper pagination
- Separates them into public and private categories
- Uses the first filename of each Gist to create a meaningful directory name
- Clones new Gists or updates existing ones
- Extracts all markdown files into a flat directory structure for easy access
- Handles filename conflicts by adding numeric suffixes
- Maintains a marker file to track which Gists have been backed up
GitHub Gists Structure
GitHub Gists are essentially mini-repositories. While many users (including the author) primarily use them for single markdown files, each Gist is stored as a Git repository with potentially multiple files. This script preserves this structure by cloning each Gist as a complete Git repository.
Usage
-
Edit the configuration section at the top of the script:
GITHUB_USERNAME="your_username" BACKUP_PATH="/path/to/backup/directory" GITHUB_TOKEN="your_github_token" -
Make the script executable:
chmod +x backup.sh -
Run the script:
./backup.sh
Security Recommendations
For better security, consider using environment variables instead of hardcoding your GitHub token:
-
Create a
.envfile:GITHUB_USERNAME=your_username GITHUB_TOKEN=your_github_token BACKUP_PATH=/path/to/backup -
Modify the script to use these environment variables:
# Load environment variables source .env # Use the loaded variables # GITHUB_USERNAME, GITHUB_TOKEN, and BACKUP_PATH should now be available -
Add
.envto your.gitignorefile to prevent accidentally committing your credentials.
Requirements
curl: For API requestsjq: For JSON parsinggit: For cloning Gists
Directory Structure
After running the script, you'll have the following directory structure:
backup_path/
├── public/ # Public Gists, each in its own directory
├── private/ # Private Gists, each in its own directory
└── markdown_files/ # All markdown files extracted from Gists
The markdown_files directory provides a convenient way to access all your markdown content without navigating through individual Gist directories.
License
This script is provided as-is under the MIT License.