Migrate bitbucket workspace

Mohammed Tayeh
3 min readApr 19, 2023

How to migrate bitbucket workspace to a new workspace

Photo by Yancy Min on Unsplash

in the last days i have a task to move more than 150 repositories to a new workspace with all branches, the first impression was we need a lot of time to do the migration.

Clone all repositories

let’s clone all repositories from the old workspace to our local environment or to a remote server. not one by one 🤓.

i will use bitbucket API to retrieve all repositories names, then clone all using while loop.

#!/bin/bash

BITBUCKET_USER='<your_bitbucket_username>'
BITBUCKET_PASS='<your_bitbucket_password>'
OLD_WORKSPACE='<old_workspace_ID>'
OUTPUT_FILE='repositories.txt'

# Initialize variables
next_url="https://api.bitbucket.org/2.0/repositories/$OLD_WORKSPACE?pagelen=100"

# Empty the output file
> $OUTPUT_FILE

# Get all repositories in the old workspace
while [ ! -z "$next_url" ]; do
response=$(curl -s -u $BITBUCKET_USER:$BITBUCKET_PASS "$next_url")

# Append repo URLs to output file
echo "$response" | jq -r '.values[].links.clone[1].href' >> $OUTPUT_FILE

# Check for the next page
next_url=$(echo "$response" | jq -r '.next // empty')
done

# Clone all repositories
while read repo_url; do
echo "Cloning $repo_url"
git clone $repo_url
done < $OUTPUT_FILE

--

--

Mohammed Tayeh

Seasoned DevOps Engineer with diverse extensive background in cloud platforms. Highly motivated.