Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ruXlab/da1a84670cf9ec1044c5c1ac16f29518 to your computer and use it in GitHub Desktop.
Save ruXlab/da1a84670cf9ec1044c5c1ac16f29518 to your computer and use it in GitHub Desktop.
bitbucket-backup-all-repos.sh
#!/bin/bash
# Author: ruX
# Website: https://rux.vc/
# Blog post: https://rux.vc/2019.06/bitbucket-backup-script/
# Based on: https://gist.github.com/redhatjobin/8ed1fed08549b2cefdbf
# Always fail on error
set -e
# Configuration
BB_USER=ruX
BB_PASS=XXXXX__YOUR_APPLICATION_PASSWORD_HERE__XXXXXX
DESTINATION=./
SUFFIX=$(date +%F_%R)
BB_DIR=$(readlink -f $DESTINATION/bitbucket-$SUFFIX)
mkdir -p $BB_DIR
# Get repositories
repos=$(curl -s --user $BB_USER:$BB_PASS https://bitbucket.org/api/1.0/user/repositories \
| tr , '\n' \
| tr -d '"{}[]' \
| grep uri \
| cut -d":" -f2 \
| sed 's/.*\/1\.0\/repositories\///g' \
| uniq)
echo "Got $(wc -l <<< $repos) repositories to backup"
# Allocate temporary folder for git clones
tmp=$(mktemp -d -t bb-XXXXXXXXXX)
echo "Using tmp dir: $tmp"
echo "Creating archives in: $BB_DIR"
# Clone and compress each repository
for repo in $repos; do
echo -e "Processing $repo.."
id=$(echo "$repo" | sed 's/\//-/g')
archive="$BB_DIR/$id.tar.bz2"
url=$(echo "$repo" | sed "s/\(.*\)/https:\/\/$BB_USER:[email protected]\/\1.git/g")
path="$tmp/$id"
mkdir -p "$path"
git clone --quiet "$url" "$path"
tar -cpjf "$archive" -C "$tmp" "$id"
rm -rf "$path"
done
# Cleanup
rm -rf "$tmp"
echo "All done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment