Skip to content

Commit

Permalink
feat: update github backup script to do all orgs that the user has ac…
Browse files Browse the repository at this point in the history
…cess too (#72)

* feat: update github backup script to do all orgs that the user has access too

* fix: newlines

* fix: switch from echo to printf

* fix: removing literal newlines

* fix: echo statement for all orgs being backedup

* fix: cleanup after upload to s3

* Update github-backup.sh

* Update github-backup.sh

* Update github-backup.sh
  • Loading branch information
venkatamutyala authored Nov 4, 2024
1 parent 59332f7 commit 5c19aa7
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions github-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,78 @@

set -e

# Check if required variables are set
if [[ -z "${GITHUB_ORG_TO_BACKUP}" ]]; then
echo "Error: GITHUB_ORG_TO_BACKUP is not set."
exit 1
fi

if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "Error: GITHUB_TOKEN is not set."
exit 1
fi

repos=$(gh repo list $GITHUB_ORG_TO_BACKUP -L 100000 --json nameWithOwner -q '.[].nameWithOwner')

BACKUP_DATE=$(date '+%Y-%m-%d')
BACKUP_LOCATION="github.com/$BACKUP_DATE/$GITHUB_ORG_TO_BACKUP"
mkdir -p $BACKUP_LOCATION && cd $BACKUP_LOCATION
# Initialize page number
page=1
all_orgs=""

# Loop until we get an empty response
while true; do
# Fetch organizations for the current page
orgs=$(gh api "/user/orgs?page=$page" --jq '.[].login')

# Check if the response is empty, meaning no more orgs
if [ -z "$orgs" ]; then
break
fi

# Replace newlines with commas in the current page's orgs
orgs_comma=$(echo "$orgs" | paste -sd "," -)

for repo in $repos; do
git clone --mirror https://$GITHUB_TOKEN@github.com/$repo.git
repo_name="${repo##*/}"
tar -czf "${repo_name}.tar.gz" "${repo_name}.git" && rm -rf "${repo_name}.git"
# Append current orgs to all_orgs with a comma separator
if [ -z "$all_orgs" ]; then
all_orgs="$orgs_comma"
else
all_orgs="$all_orgs,$orgs_comma"
fi

# Increment the page number
page=$((page + 1))
done

echo "Uploading everything to S3...."
cd /app
aws s3 cp --recursive github.com/ s3://${S3_BUCKET_NAME}/github.com/

# Save the original IFS
OLD_IFS="$IFS"

# Set IFS to comma for splitting
IFS=','

# Read the all_orgs into positional parameters
set -- $all_orgs

# Restore the original IFS
IFS="$OLD_IFS"

echo "Full list to be backed up: $all_orgs"

for GITHUB_ORG_TO_BACKUP in "$@"; do
echo ""
echo ""

echo "STARTING BACKUP OF: https://github.com/${GITHUB_ORG_TO_BACKUP}"

repos=$(gh repo list $GITHUB_ORG_TO_BACKUP -L 100000 --json nameWithOwner -q '.[].nameWithOwner')

BACKUP_DATE=$(date '+%Y-%m-%d')
BACKUP_LOCATION="github.com/$BACKUP_DATE/$GITHUB_ORG_TO_BACKUP"
mkdir -p $BACKUP_LOCATION && cd $BACKUP_LOCATION

for repo in $repos; do
git clone --mirror https://$GITHUB_TOKEN@github.com/$repo.git
repo_name="${repo##*/}"
tar -czf "${repo_name}.tar.gz" "${repo_name}.git" && rm -rf "${repo_name}.git"
done

echo "Uploading everything to S3...."
cd /app
aws s3 cp --recursive github.com/ s3://${S3_BUCKET_NAME}/github.com/
rm -rf github.com/


echo "FINISHED BACKUP OF: https://github.com/${GITHUB_ORG_TO_BACKUP}"
done

0 comments on commit 5c19aa7

Please sign in to comment.