From 65d4819b9cdc7d64d499bfcdaae21197f3378728 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Mon, 30 Aug 2021 12:02:25 +0200 Subject: [PATCH 1/6] Using provided authentication data for git clone --- README.md | 2 +- github-backup.sh | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6fbebd9..d54c6f9 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You must [generate an oauth token](https://github.com/settings/applications#pers ```sh export GITHUB_AUTH_TOKEN=enter_oauth_token_here -github-backup.sh +github-backup.sh ``` Generates tgz archive of all GitHub repos. diff --git a/github-backup.sh b/github-backup.sh index 8839786..59031f6 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -8,6 +8,7 @@ set -ex ORG="$1" +USER="$2" AUTHENTICATION_API_TOKEN="${GITHUB_AUTH_TOKEN:?"must be set and non-empty"}:x-oauth-basic" REPOS_API_URL="https://api.github.com/orgs/${ORG}/repos?type=owner" DATE=$(date +"%Y%m%d") @@ -15,7 +16,8 @@ TEMP_DIR="github_${ORG}_${DATE}" BACKUP_FILE="${TEMP_DIR}.tgz" mkdir "$TEMP_DIR" && cd "$TEMP_DIR" -curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' | awk '{print $2}' | xargs -n 1 git clone --mirror +curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ + awk '{print $2}' | sed s"#\"https://#\"https://$USER:$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror cd - tar zcf "$BACKUP_FILE" "$TEMP_DIR" rm -rf "$TEMP_DIR" From d54efbbdbd222f0808307834fc834c13188677a8 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Mon, 30 Aug 2021 12:05:37 +0200 Subject: [PATCH 2/6] Using mktemp to create temporary directory --- github-backup.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/github-backup.sh b/github-backup.sh index 59031f6..b245ae1 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -12,10 +12,9 @@ USER="$2" AUTHENTICATION_API_TOKEN="${GITHUB_AUTH_TOKEN:?"must be set and non-empty"}:x-oauth-basic" REPOS_API_URL="https://api.github.com/orgs/${ORG}/repos?type=owner" DATE=$(date +"%Y%m%d") -TEMP_DIR="github_${ORG}_${DATE}" -BACKUP_FILE="${TEMP_DIR}.tgz" - -mkdir "$TEMP_DIR" && cd "$TEMP_DIR" +TEMP_DIR=`mktemp -d` +BACKUP_FILE="github_${ORG}_${DATE}.tgz" +cd "$TEMP_DIR" curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ awk '{print $2}' | sed s"#\"https://#\"https://$USER:$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror cd - From 813cf356a85d07e500a9fb3fce538470c66560d5 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Mon, 30 Aug 2021 12:14:16 +0200 Subject: [PATCH 3/6] Using trap to clean up temp directory --- github-backup.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/github-backup.sh b/github-backup.sh index b245ae1..3440212 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -7,6 +7,15 @@ set -ex +on_exit() +{ + if [ ! -z $TEMP_DIR ] && [ -d $TEMP_DIR ]; then + rm -rf $TEMP_DIR + fi +} + +trap on_exit EXIT + ORG="$1" USER="$2" AUTHENTICATION_API_TOKEN="${GITHUB_AUTH_TOKEN:?"must be set and non-empty"}:x-oauth-basic" @@ -18,6 +27,4 @@ cd "$TEMP_DIR" curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ awk '{print $2}' | sed s"#\"https://#\"https://$USER:$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror cd - -tar zcf "$BACKUP_FILE" "$TEMP_DIR" -rm -rf "$TEMP_DIR" - +tar zcf "$BACKUP_FILE" "$TEMP_DIR" \ No newline at end of file From 1b59eeeaea1d4df6b2ae5c9e8aef676aa798e880 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Mon, 30 Aug 2021 12:18:52 +0200 Subject: [PATCH 4/6] Adding missing newline at end of file --- github-backup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github-backup.sh b/github-backup.sh index 3440212..d398079 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -27,4 +27,4 @@ cd "$TEMP_DIR" curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ awk '{print $2}' | sed s"#\"https://#\"https://$USER:$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror cd - -tar zcf "$BACKUP_FILE" "$TEMP_DIR" \ No newline at end of file +tar zcf "$BACKUP_FILE" "$TEMP_DIR" From acfa1c9e99ce083b0255f4260a50fec522950fe3 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Fri, 17 Sep 2021 14:34:33 +0200 Subject: [PATCH 5/6] Removed obsolute user-argument --- README.md | 2 +- github-backup.sh | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d54c6f9..6fbebd9 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You must [generate an oauth token](https://github.com/settings/applications#pers ```sh export GITHUB_AUTH_TOKEN=enter_oauth_token_here -github-backup.sh +github-backup.sh ``` Generates tgz archive of all GitHub repos. diff --git a/github-backup.sh b/github-backup.sh index d398079..aa93df0 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -17,14 +17,13 @@ on_exit() trap on_exit EXIT ORG="$1" -USER="$2" AUTHENTICATION_API_TOKEN="${GITHUB_AUTH_TOKEN:?"must be set and non-empty"}:x-oauth-basic" REPOS_API_URL="https://api.github.com/orgs/${ORG}/repos?type=owner" DATE=$(date +"%Y%m%d") TEMP_DIR=`mktemp -d` -BACKUP_FILE="github_${ORG}_${DATE}.tgz" +BACKUP_FILE="`pwd`/github_${ORG}_${DATE}.tgz" cd "$TEMP_DIR" curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ - awk '{print $2}' | sed s"#\"https://#\"https://$USER:$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror -cd - -tar zcf "$BACKUP_FILE" "$TEMP_DIR" + awk '{print $2}' | sed s"#\"https://#\"https://$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror + +tar zcf "$BACKUP_FILE" "$TEMP_DIR" \ No newline at end of file From bafc16979063b1cc0c6c4f48d16820a696200ac0 Mon Sep 17 00:00:00 2001 From: Markus Lenger Date: Fri, 17 Sep 2021 14:35:27 +0200 Subject: [PATCH 6/6] Stripping leading path from archived files --- github-backup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github-backup.sh b/github-backup.sh index aa93df0..4f8e4ad 100755 --- a/github-backup.sh +++ b/github-backup.sh @@ -26,4 +26,4 @@ cd "$TEMP_DIR" curl -u $AUTHENTICATION_API_TOKEN -s "$REPOS_API_URL" | grep -Eo '"clone_url": "[^"]+"' |\ awk '{print $2}' | sed s"#\"https://#\"https://$GITHUB_AUTH_TOKEN@#" | xargs -n 1 git clone --mirror -tar zcf "$BACKUP_FILE" "$TEMP_DIR" \ No newline at end of file +tar zcf "$BACKUP_FILE" -C "$TEMP_DIR" . \ No newline at end of file