A comprehensive bash script to automatically backup all repositories and branches from a Bitbucket workspace with advanced features like parallel processing, error recovery, and progress tracking.
- π Automatic repository discovery - Fetches all repositories from your Bitbucket workspace
- πΏ All branches backup - Backs up every branch in each repository
- π Secure authentication - Uses Atlassian API tokens for authentication
- π Progress tracking - Real-time progress bar and detailed logging
- π‘οΈ Error recovery - Automatic retry with exponential backoff
- β‘ Parallel processing - Configurable parallel job execution
- π Backup verification - Verify backups after completion
- π― Dry-run mode - Preview what would be backed up
- π Comprehensive logging - Colored, timestamped log messages
- π Summary reports - Detailed backup statistics and results
- π₯οΈ Cross-platform support - Works on macOS, Linux, and Windows
-
Clone this repository:
git clone <your-repo-url> cd bitbucketbackup
-
Create your configuration file:
cp config.env.example config.env
-
Edit
config.env
with your Bitbucket credentials:# Bitbucket Configuration ATLASSIAN_EMAIL="your-email@example.com" API_TOKEN="your-api-token-here" ORGNAME="your-workspace-name" BACKUP_DIR="/path/to/your/backup/directory"
-
Run the backup:
On macOS/Linux:
chmod +x main.sh ./main.sh
On Windows:
.\main.ps1
Platform | Script | Requirements |
---|---|---|
macOS | main.sh |
Bash 4.0+, Git, curl |
Linux | main.sh |
Bash 4.0+, Git, curl |
Windows (WSL) | main.sh |
WSL, Bash 4.0+, Git, curl |
Windows (PowerShell) | main.ps1 |
PowerShell 5.0+, Git |
- Bash shell (version 4.0 or higher)
- Git (version 2.0 or higher)
- curl (for API communication)
- jq (optional, for better JSON parsing)
- PowerShell 5.0 or higher
- Git for Windows
- Execution policy allowing script execution
-
Enable PowerShell script execution:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Install Git for Windows (if not already installed):
- Download from: https://git-scm.com/download/win
- Ensure Git is added to PATH
-
Run the PowerShell script:
.\main.ps1
- Go to Atlassian Account Settings > Security > API tokens
- Click "Create API token with scopes"
- Give the API token a name and set an expiry date, then click "Next"
- Select "Bitbucket" as the app and click "Next"
- Select the following scopes (permissions):
- Repositories: Read
- Workspace membership: Read
- Click "Next" to review your token
- Click "Create token"
- Copy the generated API token and paste it in your
config.env
file
Note: API tokens are created through your Atlassian account settings, not through Bitbucket directly. They provide better security and are the recommended authentication method for Bitbucket APIs.
Usage: main.sh [OPTIONS]
OPTIONS:
-h, --help Show this help message
-c, --config FILE Specify configuration file (default: config.env)
-d, --dry-run Show what would be done without executing
-v, --verbose Enable verbose output
-s, --skip-existing Skip repositories that already exist
-j, --jobs N Number of parallel jobs (default: 4)
--verify Verify backups after completion
EXAMPLES:
./main.sh # Run with default settings
./main.sh --dry-run # See what would be backed up
./main.sh --verbose --jobs 8 # Verbose output with 8 parallel jobs
./main.sh --config my.env # Use custom config file
Usage: main.ps1 [OPTIONS]
OPTIONS:
-ConfigFile FILE Specify configuration file (default: config.env)
-DryRun Show what would be done without executing
-Verbose Enable verbose output
-SkipExisting Skip repositories that already exist
-Jobs N Number of parallel jobs (default: 4)
-Verify Verify backups after completion
-Help Show this help message
EXAMPLES:
.\main.ps1 # Run with default settings
.\main.ps1 -DryRun # See what would be backed up
.\main.ps1 -Verbose -Jobs 8 # Verbose output with 8 parallel jobs
.\main.ps1 -ConfigFile my.env # Use custom config file
Preview what the script would do without making any changes:
macOS/Linux:
./main.sh --dry-run
Windows:
.\main.ps1 -DryRun
Get detailed information about each step:
macOS/Linux:
./main.sh --verbose
Windows:
.\main.ps1 -Verbose
Speed up backups by running multiple repositories in parallel:
macOS/Linux:
./main.sh --jobs 8 # Use 8 parallel jobs
Windows:
.\main.ps1 -Jobs 8 # Use 8 parallel jobs
Only backup new repositories, skip existing ones:
macOS/Linux:
./main.sh --skip-existing
Windows:
.\main.ps1 -SkipExisting
Verify that all backups are valid after completion:
macOS/Linux:
./main.sh --verify
Windows:
.\main.ps1 -Verify
Use a different configuration file:
macOS/Linux:
./main.sh --config production.env
Windows:
.\main.ps1 -ConfigFile production.env
- β All repositories in your specified workspace
- β All branches in each repository (main, master, develop, feature branches, etc.)
- β Complete Git history and commits
- β All tags and refs
- β Repository metadata and structure
- The
config.env
file is excluded from Git via.gitignore
- Never commit your actual API token to version control
- The example file (
config.env.example
) is safe to commit as it contains no real credentials - All sensitive data is validated and sanitized before use
- Parallel Processing: Configurable number of concurrent repository backups
- Retry Logic: Automatic retry with exponential backoff for network operations
- Progress Tracking: Real-time progress bar and detailed statistics
- Error Recovery: Graceful handling of failures with detailed reporting
- Memory Efficient: Processes repositories one at a time to minimize memory usage
The script includes comprehensive error handling:
- Network Failures: Automatic retry with exponential backoff
- Authentication Errors: Clear error messages for invalid credentials
- Permission Issues: Validation of backup directory permissions
- Repository Access: Handling of private repositories and access restrictions
- Disk Space: Validation of available disk space before backup
- Bash shell (version 4.0 or higher)
- Git (version 2.0 or higher)
- curl (for API communication)
- jq (optional, for better JSON parsing)
- PowerShell (version 5.0 or higher)
- Git for Windows (version 2.0 or higher)
The script provides comprehensive logging with different levels:
- ERROR: Critical errors that prevent operation
- WARN: Non-critical issues that don't stop execution
- INFO: General information about progress
- SUCCESS: Successful operations
All logs include timestamps and are color-coded for easy reading.
[2024-07-21 10:30:15] [INFO] Found 38 repositories to backup
[2024-07-21 10:30:16] [INFO] Created backup directory: /Users/macbook/Downloads/Backup
[##################################################] 100% (38/38)
[2024-07-21 10:35:22] [SUCCESS] Completed backup of my-repo (5/5 branches updated)
==========================================
BACKUP SUMMARY
==========================================
Total repositories: 38
Successful backups: 38
Failed backups: 0
Backup location: /Users/macbook/Downloads/Backup
Backup size: 2.1G
Backup time: Sun Jul 21 10:35:22 2024
==========================================
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
Copyright (c) 2024 Bitbucket Backup Script
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.