Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 101 additions & 13 deletions github-backup.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,111 @@
#!/bin/sh
#!/bin/bash

#
# Simple shell script to backup all GitHub repos
# Usage: github-backup.sh <username>
# Usage: github-backup.sh -u <repo-user> | -o <repo-org> [--debug] -l <login-user>
# @author Petr Trofimov <[email protected]>
# @author Federico Mestrone <[email protected]>
#
# pointer for future development https://gist.github.com/rodw/3073987 to back up GitHub Wiki
#

function echo_usage
{
echo
echo "Usage:"
echo "github-backup.sh -u <repo-user>|-o <repo-org> -l <login-user> [-t all|public|private|forks|sources|owner|member] [--debug]"
echo "github-backup.sh --user <repo-user>|--org <repo-org> --login <login-user> [--type all|public|private|forks|sources|owner|member] [--debug]"
echo
}

echo

if [ "$#" -eq 0 ]; then
echo "No option specified"
echo_usage
exit 1
fi

GIT_TYPE="all"

while [[ $# -gt 0 ]]
do
case $1 in
-o|--org)
GIT_TARGET="orgs/$2"
shift
;;
-u|--user)
GIT_TARGET="users/$2"
shift
;;
-l|--login)
GIT_USER="$2"
shift
;;
-t|--type)
GIT_TYPE="$2"
shift
;;
--debug)
echo "Enabling debug mode"
echo
set -x
;;
*)
echo "Unknown option $1"
echo_usage
exit 1
;;
esac
shift
done

if [ "x$GIT_TARGET" = "x" ]; then
echo "No repo username or organization specified"
echo_usage
exit 1
fi

if [ "x$GIT_USER" = "x" ]; then
echo "No login username specified"
echo_usage
exit 1
fi

set -e

GIT_API_URL="https://api.github.com/${GIT_TARGET}/repos?type=${GIT_TYPE}"
GIT_DATE=$(date +"%Y%m%d")
GIT_TEMP_DIR="$(mktemp -q -d -t "github_${GIT_USER}_${GIT_DATE}")"
GIT_BACKUP_FILE="github_${GIT_USER}_${GIT_DATE}.tgz"

echo "Ready to back up $GIT_TYPE repos from ${GIT_API_URL}"
echo
echo "Temp target folder: $GIT_TEMP_DIR"
echo "Final target file: $GIT_BACKUP_FILE"
echo

echo "CDing into target folder $GIT_TEMP_DIR"
echo
pushd "$GIT_TEMP_DIR" > /dev/null

echo "Cloning $GIT_TYPE repos"
echo
curl -u "$GIT_USER" -s "$GIT_API_URL" | grep -Eo '"clone_url": "[^"]+"' | awk '{print $2}' | xargs -n 1 git clone

set -ex
echo "Returning to initial folder"
echo
popd > /dev/null

USER="$1"
API_URL="https://api.github.com/users/${USER}/repos?type=owner"
DATE=$(date +"%Y%m%d")
TEMP_DIR="github_${USER}_${DATE}"
BACKUP_FILE="${TEMP_DIR}.tgz"
echo "Creating target file $GIT_BACKUP_FILE"
echo

mkdir "$TEMP_DIR" && cd "$TEMP_DIR"
curl -s "$API_URL" | grep -Eo '"git_url": "[^"]+"' | awk '{print $2}' | xargs -n 1 git clone
cd -
tar zcf "$BACKUP_FILE" "$TEMP_DIR"
rm -rf "$TEMP_DIR"
tar czf "$GIT_BACKUP_FILE" -C "$GIT_TEMP_DIR/.." $(basename "$GIT_TEMP_DIR")
ls -l "$GIT_BACKUP_FILE"

echo "Deleting target folder $GIT_TEMP_DIR"
echo
rm -Rf "$GIT_TEMP_DIR"

exit 0
26 changes: 16 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# github-backup-sh

*Simple shell script to backup all GitHub repos for specified user*
*Simple shell script to backup all GitHub repos for the specified user or organization*

Well, there are many solutions, which will not only backup your repositories
but also prepare a pizza for you and clean after itself.
Here it is very simple Bash-written script to do this task easily
without any questions in one go. Nothing more.
Here is a very simple Bash script to perform this task easily
with few questions in one go. Nothing more.

Just run:

```sh
github-backup.sh <username>
```
./github-backup.sh -u <repo-user> -l <login-user>
```

and you will get tgz archive of all GitHub repos. It's simple!

### Use it without any installation
to get all repositories from user `repo-user` logging into GitHub as `login-user` (you will be prompted for your login password).

```sh
curl "https://raw.github.com/ptrofimov/github-backup-sh/master/github-backup.sh" | sh -s <username>
```
./github-backup.sh -o <repo-org> -l <login-user>
```

to get all repositories from organization `repo-org`.

You will get a `tgz` archive of all GitHub repos in your current folder. It's simple!

You can also use the `--debug` option to enable Bash debugging (`set -x`) and use the `-t` option to specify the type of
repositories you want to back up (for a user-based repository, one of `all`, `owner`, or `member`; for an organization,
one of `all`, `public`, `private`, `forks`, `sources`, or `member`). The default type is `all`.

Enjoy!