Created
January 27, 2021 19:39
-
-
Save davidegironi/ff622406f1be118b8d3fe89db4f5a76a to your computer and use it in GitHub Desktop.
Script to download all repositories from a Bitbucket account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bitbucket Git Downloader | |
# Copyright (c) Davide Gironi, 2021 | |
# Released under GPLv3 | |
# Downloads all the repository from a Bitbucket account |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 - |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
Thanks for sharing your mod!
I made some changes to keep the teamname/reponame structure and also included username and app password inside the script and insert them in each repo url. It's important that the DATAFOLDER variable is absolute, to be able to get back to the original directory.
# Bitbucket Git Downloader
# Copyright (c) Davide Gironi, 2021
# Released under GPLv3
DATAFOLDER=/gitdata
if [ ! -d "$DATAFOLDER" ]; then
echo "data folder does not exist"
exit
fi
GIT_USERNAME='username'
GIT_PASSWORD='app_password'
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
AUTH_REPO=$(echo "$repo" | sed "s#https://#https://$GIT_USERNAME:$GIT_PASSWORD@#")
TEAMNAME=$(echo "$repo" | cut -d'/' -f4)
REPOSITORYNAME=$(echo "$repo" | cut -d'/' -f5 | sed 's/\.git$//')
echo "Cloning $TEAMNAME/$REPOSITORYNAME"
if [ ! -d "$TEAMNAME" ]; then
mkdir -p "$TEAMNAME"
fi
cd "$TEAMNAME"
if [ -d "$REPOSITORYNAME" ]; then
cd "$REPOSITORYNAME"
git pull
cd ..
else
git clone "$AUTH_REPO"
fi
cd $DATAFOLDER
done
url=$(jq -r '.next' repositories.json)
done
rm repositories.json
rm repositories.txt
cd -
Thanks for sharing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.