changeset 0:507ba2547208 default tip

Initial commit
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 30 Jun 2020 15:52:36 +0200
parents
children
files README.md download_from_list.sh download_team_repos.sh download_user_repos.sh
diffstat 4 files changed, 156 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Jun 30 15:52:36 2020 +0200
@@ -0,0 +1,34 @@
+# Bitbucket backup
+
+These repo contains a few scripts to help with cloning repositories from bitbucket.org. They were created in a hurry mostly by copy paste so please make sure that they actually download everything you need.
+
+## Using the API
+ATTENTION! The user script will not download all folders visible when visiting the Bitbucket site, as that list is not availably through the API. If you want to download all repositories that you have access to please read below.
+
+`./download_team_repos user team` will download all repos for a team
+`./download_user_repos user` will download all repos owned by a user
+
+The scripts use your password to access the API and clones the repositories through SSH. Therefore you will need to have your ssh-keys set up with your Bitbucket account.
+
+## Downloading all repos
+There is no API endpoint for listing all repositories a user has access to. Therefore you will need to create a list of the repositories manually. There are two steps to this method, creating the list of repos, and using a script to clone all the repos in the list.s
+
+### Creating the list
+
+The file should look something like
+```
+/sbpteam/flexural_applications
+/sbpteam/flexural_package
+/werpers/pade_time
+/sbpteam/laplace1d
+/fwerpers/rankings
+```
+
+I created my list by doing "Inspect element" in the browser for each of the pages under https://bitbucket.org/dashboard/repositories and copying the HTML into a text file. Then looking for links of the type `<a href="/sbpteam/pade_time">` and copying them to their own text file. If you have very few repos it might be faster to just right click the links on the page and doing "Copy link".
+
+### Running the script
+When you have the list of repos you can run
+```bash
+./download_from_list.sh repolist.txt targetdir
+```
+where `repolist.txt` is your file with repo links, and `targetdir` is the name of the directory to which all repos will be cloned. `targetdir` should NOT exist before running the script.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/download_from_list.sh	Tue Jun 30 15:52:36 2020 +0200
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+# Usage: downloader.sh username teamname
+REPO_FILE=${1}
+DIR=${2}
+
+if [ -d "$DIR" ]
+then
+    echo "Directory already exists"
+    exit 1
+fi
+
+mkdir "${DIR}"
+cd "${DIR}"
+
+for repo in `cat "../${REPO_FILE}"`
+do
+    local_dir=.`dirname $repo`
+    mkdir -p "$local_dir"
+    cd "$local_dir"
+
+    echo "[Cloning $repo]"
+
+    hg clone ssh://hg@bitbucket.org"$repo"
+
+    if [ $? != 0 ]
+    then
+        echo "[hg failed, trying git instead]"
+        git clone git@bitbucket.org:"$repo".git
+    fi
+
+    echo ""
+    echo ""
+
+    cd ..
+
+    # echo "Cloning" $repo
+    # if echo "$repo" | grep -q ".git"; then
+    #     command="git"
+    # else
+    #     command="hg"
+    # fi
+    # $command clone $repo
+done
+
+cd ..
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/download_team_repos.sh	Tue Jun 30 15:52:36 2020 +0200
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Usage: downloader.sh username teamname
+USER=${1}
+TEAM=${2}
+
+if [ -d "$TEAM" ]
+then
+    echo "Team folder already exists"
+    exit 1
+fi
+
+mkdir "$TEAM"
+cd $TEAM
+
+i=1
+NEXT_URL="https://api.bitbucket.org/2.0/repositories/${TEAM}?pagelen=100"
+
+while [ $NEXT_URL != null ]
+do
+    curl -u $USER $NEXT_URL > repoinfo"$i".json
+    jq -r '.values[] | .links.clone[1].href' repoinfo"$i".json >> repos.txt
+    i=$((i++))
+    NEXT_URL=`jq -r '.next' repoinfo"$i".json`
+done
+
+for repo in `cat repos.txt`
+do
+    echo "Cloning" $repo
+    if echo "$repo" | grep -q ".git"; then
+        command="git"
+    else
+        command="hg"
+    fi
+    $command clone $repo
+done
+
+cd ..
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/download_user_repos.sh	Tue Jun 30 15:52:36 2020 +0200
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Usage: downloader.sh username teamname
+USER=${1}
+
+if [ -d "$USER" ]
+then
+    echo "User folder already exists"
+    exit 1
+fi
+
+mkdir "$USER"
+cd $USER
+
+i=1
+NEXT_URL="https://api.bitbucket.org/2.0/repositories/${USER}?pagelen=100&role=member"
+while [ $NEXT_URL != null  ]
+do
+    curl -u $USER $NEXT_URL > repoinfo"$i".json
+    jq -r '.values[] | .links.clone[1].href' repoinfo"$i".json >> repos.txt
+    i=$((i++))
+    NEXT_URL=`jq -r '.next' repoinfo"$i".json`
+done
+
+exit 0
+
+for repo in `cat repos.txt`
+do
+    echo "Cloning" $repo
+    if echo "$repo" | grep -q ".git"; then
+        command="git"
+    else
+        command="hg"
+    fi
+    $command clone $repo
+done
+
+cd ..