Skip to content

Backup

Bitbucket

Validate Access

Validate User and Password

your_app_password="bitbucket-app-password"
your_username="bitbucket-username"
curl -u $your_username:$your_app_password "https://api.bitbucket.org/2.0/user" | jq

Backup Repositories

Backup All Repositories

#!/bin/bash

# Replace with your Bitbucket username, app password (or access token), and workspace
BITBUCKET_USER=""
BITBUCKET_APP_PASSWORD=""
BITBUCKET_WORKSPACE=""

# API endpoint to get list of repositories from your workspace
API_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE"

# Directory where the repositories will be cloned
CLONE_DIR="$(pwd)/$BITBUCKET_USER"

# Create the directory if it doesn't exist
mkdir -p "$CLONE_DIR"
cd "$CLONE_DIR"

# Fetch repositories from Bitbucket API (paginated results)
while [ ! -z "$API_URL" ]; do
echo "Fetching repositories from: $API_URL"
response=$(curl -s -u "$BITBUCKET_USER:$BITBUCKET_APP_PASSWORD" "$API_URL")

# Parse repository clone URLs
repo_urls=$(echo "$response" | jq -r '.values[].links.clone[1].href')

# Clone each repository
for repo_url in $repo_urls; do
    repo_name=$(basename "$repo_url" .git)
    if [ ! -d "$repo_name" ]; then
    echo "Cloning repository: $repo_name"
    git clone "$repo_url"
    else
    echo "Repository $repo_name already exists, skipping."
    fi
done

# Check for next page (pagination)
API_URL=$(echo "$response" | jq -r '.next')
done

echo "All repositories from workspace '$BITBUCKET_WORKSPACE' have been cloned."