Created
August 7, 2016 00:56
-
-
Save skydiver/198506e0737e16f076c4f7892664f9c5 to your computer and use it in GitHub Desktop.
Backup Bitbucket
This file contains 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
#!/usr/bin/env bash | |
# Bitbucket backup script | |
# backups all repos (including wiki and issues) of a user | |
# | |
# requires (in PATH): | |
# - hg (http://mercurial.selenic.com) | |
# - git (http://git-scm.com) | |
# - jq (http://stedolan.github.io/jq/) | |
# - curl (http://curl.haxx.se) | |
# | |
# variables that must be set | |
# - USER_NAME: the user name | |
# - API_KEY: api key of the user | |
# - BACKUP_LOCATION: the base path to store the backup | |
################################################################################################# | |
# SCRIPT VARIABLES | |
################################################################################################# | |
USER_NAME=$1 | |
API_KEY=$2 | |
BACKUP_LOCATION=$3 | |
################################################################################################# | |
# BACKUP HG REPO | |
################################################################################################# | |
function backup_hg { | |
if [ -d "$1" ]; then | |
cd $1 | |
hg pull | |
else | |
hg clone -U ssh://hg@bitbucket.org/$2 $1 | |
fi | |
} | |
################################################################################################# | |
# BACKUP GIT REPO | |
################################################################################################# | |
function backup_git { | |
if [ -d "$1" ]; then | |
cd $1 | |
git pull | |
else | |
#git clone -n git@bitbucket.org:$2 $1 | |
git clone -n https://$USER_NAME:$API_KEY@bitbucket.org/$2 $1 | |
fi | |
} | |
################################################################################################# | |
# BACKUP REPO | |
################################################################################################# | |
function backup_scm { | |
TYPE=$3 | |
REPO=$2 | |
REPO_BACKUP_LOCATION=$BACKUP_LOCATION/$REPO | |
REPO_REMOTE_PATH=$REPO | |
if [ "git" = "$1" ]; then | |
REPO_REMOTE_PATH=$REPO_REMOTE_PATH.git | |
fi | |
if [ "repo" = "$TYPE" ]; then | |
REPO_BACKUP_LOCATION=$REPO_BACKUP_LOCATION/repo | |
elif [ "wiki" = "$TYPE" ]; then | |
REPO_BACKUP_LOCATION=$REPO_BACKUP_LOCATION/wiki | |
REPO_REMOTE_PATH=$REPO_REMOTE_PATH/wiki | |
fi | |
if [ "hg" = "$1" ]; then | |
backup_hg $REPO_BACKUP_LOCATION $REPO_REMOTE_PATH | |
elif [ "git" = "$1" ]; then | |
backup_git $REPO_BACKUP_LOCATION $REPO_REMOTE_PATH | |
fi | |
} | |
################################################################################################# | |
# PRINT HEADER LINE | |
################################################################################################# | |
function line { | |
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' = | |
} | |
################################################################################################# | |
# CREATE BACKUP LOCATION | |
################################################################################################# | |
mkdir -p $BACKUP_LOCATION | |
################################################################################################# | |
# GET REPOS LIST | |
################################################################################################# | |
repositories=`curl -s -S --user $USER_NAME:$API_KEY https://api.bitbucket.org/2.0/repositories\?role\=admin\&pagelen\=100 | jq -r '.values[] | "\(.full_name) \(.scm) \(.has_issues) \(.has_wiki)"'` | |
################################################################################################# | |
# PERFORM BACKUPS | |
################################################################################################# | |
OIFS="$IFS" | |
IFS=$'\n' | |
for repository in $repositories | |
do | |
repository_name=`echo $repository | cut -d ' ' -f1` | |
has_issues=`echo $repository | cut -d ' ' -f3` | |
has_wiki=`echo $repository | cut -d ' ' -f4` | |
scm=`echo $repository | cut -d ' ' -f2` | |
# PRINT BACKUP NAME | |
line | |
echo "BACKUP > $repository_name" | |
# BACKUP REPO | |
echo "BACKUP > $repository_name > REPO" | |
backup_scm $scm $repository_name repo | |
# BACKUP WIKI | |
if [ "true" = "$has_wiki" ]; then | |
echo "BACKUP > $repository_name > WIKI" | |
backup_scm $scm $repository_name wiki | |
fi | |
# BACKUP ISSUES | |
if [ "true" = "$has_issues" ]; then | |
echo "BACKUP > $repository_name > ISSUES" | |
ISSUES_BACKUP_LOCATION=$BACKUP_LOCATION/$repository_name/issues | |
mkdir -p $ISSUES_BACKUP_LOCATION | |
curl -s -S --user $USER_NAME:$API_KEY https://api.bitbucket.org/1.0/repositories/$repository_name/issues > $ISSUES_BACKUP_LOCATION/issues.json | |
fi | |
# PRINT END LINES | |
line | |
printf "\n\n" | |
done | |
IFS="$OIFS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage