Skip to content

Instantly share code, notes, and snippets.

@davidegironi
Created January 27, 2021 19:39
Show Gist options
  • Save davidegironi/ff622406f1be118b8d3fe89db4f5a76a to your computer and use it in GitHub Desktop.
Save davidegironi/ff622406f1be118b8d3fe89db4f5a76a to your computer and use it in GitHub Desktop.
Script to download all repositories from a Bitbucket account
# Bitbucket Git Downloader
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
# Downloads all the repository from a Bitbucket account
#!/bin/bash
# Bitbucket Git Downloader
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
DATAFOLDER=gitdata
if [ -z "$GIT_USERNAME" ]; then
echo "No GIT_USERNAME supplied"
exit
fi
if [ -z "$GIT_PASSWORD" ]; then
echo "No GIT_PASSWORD supplied"
exit
fi
if [ ! -d "$DATAFOLDER" ]; then
echo "data folder does not exists"
exit
fi
cd $DATAFOLDER
user=$GIT_USERNAME:$GIT_PASSWORD
url='https://api.bitbucket.org/2.0/user/permissions/repositories'
while [ ! "$url" == "null" ]
do
curl -u $user $url > repositories.json
jq -r '.values[] | .repository.links.html.href' repositories.json > repositories.txt
for repo in `cat repositories.txt`
do
REPOSITORYNAME=`basename "$repo"`
echo "Cloning" $REPOSITORYNAME
if [ -d $REPOSITORYNAME ]
then
cd $REPOSITORYNAME
git pull
cd ..
else
git clone $repo
fi
done
url=`jq -r '.next' repositories.json`
done
rm repositories.json
rm repositories.txt
cd -
#!/bin/bash
# Bitbucket Git Downloader
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
#downloads all the repository from a Bitbucket account
#set you credential
#to store your password use git credential helper
# git config --global credential.helper store
export GIT_USERNAME=gitusername
export GIT_PASSWORD=gitpassword
./bitbucketgitdownloader.sh
@captrespect
Copy link

Thanks for this! Note that the username and password is your username found on your bitbucket profile page, not your email address. The password, must be setup through bitbucket 'App Passwords', not your normal password.

@nachiket
Copy link

Weirdly it still asks me for the username/password

@nachiket
Copy link

nachiket commented Apr 26, 2024

Minor fix -- do the following before git clone command and make sure your ssh key is in the account, then you can just clone the ssh repo URL.

  repo=`echo $repo | sed "s,https://,git@git\.," | sed "s/$/.git/" | sed "s,/,:,"`

@davidegironi
Copy link
Author

Thanks for sharing your mod!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment