From ab62fb416bd52c66f6e0385deb881ef2c43fed6a Mon Sep 17 00:00:00 2001 From: Federico Mestrone Date: Wed, 22 Feb 2017 17:20:49 +0000 Subject: [PATCH 1/5] Improved functionality and usability of script --- github-backup.sh | 83 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 13 deletions(-) mode change 100644 => 100755 github-backup.sh diff --git a/github-backup.sh b/github-backup.sh old mode 100644 new mode 100755 index 13641e7..19effa4 --- a/github-backup.sh +++ b/github-backup.sh @@ -1,23 +1,80 @@ -#!/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 # -set -ex +function echo_usage +{ + echo "Usage: github-backup.sh -u |-o -l [--debug]" +} -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" +if [ "$#" -eq 0 ]; then + echo "No option specified" + echo_usage + exit 1 +fi -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" +while [[ $# -gt 1 ]] +do +case $1 in + -o|--org) + GIT_TARGET="orgs/$2" + shift + ;; + -u|--user) + GIT_TARGET="users/$2" + shift + ;; + -l|--login-user) + GIT_USER="$2" + shift + ;; + --debug) + 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=all" +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 all repos from ${GIT_API_URL}" +echo "Temp target folder: $GIT_TEMP_DIR" +echo "Final target file: $GIT_BACKUP_FILE" + +pushd "$GIT_TEMP_DIR" + +curl -u "$GIT_USER" -s "$GIT_API_URL" | grep -Eo '"clone_url": "[^"]+"' | awk '{print $2}' | xargs -n 1 git clone + +popd + +tar zcf "$GIT_BACKUP_FILE" "$GIT_TEMP_DIR" +rm -Rf "$GIT_TEMP_DIR" + +exit 0 From 0f38fcee2154bd5967770be84c7be26dc27dc55b Mon Sep 17 00:00:00 2001 From: Federico Mestrone Date: Wed, 22 Feb 2017 18:45:25 +0000 Subject: [PATCH 2/5] Added type option and small improvements --- github-backup.sh | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/github-backup.sh b/github-backup.sh index 19effa4..124a2d5 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -9,16 +9,24 @@ function echo_usage { - echo "Usage: github-backup.sh -u |-o -l [--debug]" + 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 -while [[ $# -gt 1 ]] +GIT_TYPE="all" + +while [[ $# -gt 0 ]] do case $1 in -o|--org) @@ -29,11 +37,17 @@ case $1 in GIT_TARGET="users/$2" shift ;; - -l|--login-user) + -l|--login) GIT_USER="$2" shift ;; + -t|--type) + GIT_TYPE="$2" + shift + ;; --debug) + echo "Enabling debug mode" + echo set -x ;; *) @@ -59,22 +73,37 @@ fi set -e -GIT_API_URL="https://api.github.com/${GIT_TARGET}/repos?type=all" +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 all repos from ${GIT_API_URL}" +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 -pushd "$GIT_TEMP_DIR" +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 -popd +echo "Returning to initial folder" +echo +popd > /dev/null + +echo "Creating target file $GIT_BACKUP_FILE" +echo + +tar czf "$GIT_BACKUP_FILE" -C "$GIT_TEMP_DIR/.." $(basename "$GIT_TEMP_DIR") +ls -l "$GIT_BACKUP_FILE" -tar zcf "$GIT_BACKUP_FILE" "$GIT_TEMP_DIR" +echo "Deleting target folder $GIT_TEMP_DIR" +echo rm -Rf "$GIT_TEMP_DIR" exit 0 From 2b7d2d489c74896e83634b5fa3e8856c70f550a9 Mon Sep 17 00:00:00 2001 From: Federico Mestrone Date: Wed, 22 Feb 2017 18:55:17 +0000 Subject: [PATCH 3/5] Updated ReadMe file --- readme.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 2b0b3ae..4447b01 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 logging into GitHub as (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 . + +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`, `member`). Enjoy! \ No newline at end of file From c8ffe906fa2bd7a98dea707c070144f5bfd2685a Mon Sep 17 00:00:00 2001 From: Federico Mestrone Date: Wed, 22 Feb 2017 18:57:40 +0000 Subject: [PATCH 4/5] Fixes to ReadMe file --- readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 4447b01..b8c1805 100644 --- a/readme.md +++ b/readme.md @@ -10,21 +10,21 @@ with few questions in one go. Nothing more. Just run: ``` -./github-backup.sh -u -l " +./github-backup.sh -u -l ``` -to get all repositories from user logging into GitHub as (you will be prompted for your login password). +to get all repositories from user `repo-user` logging into GitHub as `login-user` (you will be prompted for your login password). ``` -./github-backup.sh -o -l " +./github-backup.sh -o -l ``` -to get all repositories from organization . +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`, `member`). +one of `all`, `public`, `private`, `forks`, `sources`, or `member`). The default type is `all`. Enjoy! \ No newline at end of file From f5697c7450943a117e545cddf5ae2be0cb97495a Mon Sep 17 00:00:00 2001 From: Federico Mestrone Date: Thu, 23 Feb 2017 07:49:51 +0000 Subject: [PATCH 5/5] Added link to help with future development (e.g. for backing up github wikis) --- github-backup.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/github-backup.sh b/github-backup.sh index 124a2d5..4731a87 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -6,6 +6,8 @@ # @author Petr Trofimov # @author Federico Mestrone # +# pointer for future development https://gist.github.com/rodw/3073987 to back up GitHub Wiki +# function echo_usage {