Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Nov 4, 2024
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://[email protected]/$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://[email protected]/$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
Loading