diff --git a/github-backup.sh b/github-backup.sh old mode 100644 new mode 100755 index 13641e7..4731a87 --- a/github-backup.sh +++ b/github-backup.sh @@ -1,23 +1,111 @@ -#!/bin/sh +#!/bin/bash # # Simple shell script to backup all GitHub repos -# Usage: github-backup.sh +# Usage: github-backup.sh -u | -o [--debug] -l # @author Petr Trofimov +# @author Federico Mestrone # +# 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 |-o -l [-t all|public|private|forks|sources|owner|member] [--debug]" + echo "github-backup.sh --user |--org --login [--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 diff --git a/readme.md b/readme.md index 2b0b3ae..b8c1805 100644 --- a/readme.md +++ b/readme.md @@ -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 +``` +./github-backup.sh -u -l ``` -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 ``` +./github-backup.sh -o -l +``` + +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! \ No newline at end of file