Skip to content

Instantly share code, notes, and snippets.

@alastairmccormack
Created July 18, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alastairmccormack/470a556fbfde7ca81555b4da09dd4c71 to your computer and use it in GitHub Desktop.
Save alastairmccormack/470a556fbfde7ca81555b4da09dd4c71 to your computer and use it in GitHub Desktop.
Bitbucket Backup - Clones all repos for given username or team
# Requires Python 3.6 (for fstrings)
import requests
from git import Repo
import jmespath
import os.path
# Create an app password. Username can be found using: https://stackoverflow.com/a/44932928/1554386
username = ""
password = ""
dest_dir = 'bitbucket-backup'
url = f"https://api.bitbucket.org/2.0/repositories/{username}"
while True:
repo_resp = requests.get(url=url, auth=(username, password))
repo_resp_json = repo_resp.json()
print(f"Page {repo_resp_json['page']}")
for repo in repo_resp_json['values']:
proj_name = repo['name']
dir_name = os.path.join(dest_dir, proj_name)
ssh_clone_url = jmespath.search("links.clone[?name == 'ssh'].href", repo)[0]
if os.path.exists(dir_name):
print(f'Repo {proj_name} already exists. Pulling')
repo = Repo(dir_name)
repo.remotes.origin.pull()
else:
print(f'cloning {ssh_clone_url}')
clone_resp = Repo.clone_from(url=ssh_clone_url, to_path=dir_name)
if repo_resp_json.get('next'):
url = repo_resp_json['next']
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment