-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-migrator
executable file
·61 lines (58 loc) · 4 KB
/
github-migrator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# Script to create backup of github private/public repositories under organization and download as archives
# Requires curl command, github access token generated as Owner.
# Add organization, access token and username to below fields before proceeding.
# Run ./github-migrator --help for instructions.
# Avoid running --backup more than once to avoid multiple instances of backup on server side.
# v0.3
set -e
ORGANIZATION=""
GITHUB_USERNAME=""
GITHUB_ACCESS_TOKEN=""
backup_status()
{
> migration-status
for url in `curl -s -H "Authorization: token $GITHUB_ACCESS_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/orgs/$ORGANIZATION/migrations | grep 'migrations' | grep -v archive_url | awk '{print $2}' | cut -d'"' -f2`; do
echo $(curl -s -H "Authorization: token $GITHUB_ACCESS_TOKEN" -H "Accept: application/vnd.github.wyandotte-preview+json" $url | grep 'state\|full_name\|archive_url'| grep -v archive_format | awk '{print $2}' | cut -d'"' -f2) >> migration-status
done
cat migration-status
}
if [ "$1" = "--backup" ] || [ "$1" = "backup" ] && [ -n "$ORGANIZATION" ] && [ -n "$GITHUB_ACCESS_TOKEN" ] && [ -n "$GITHUB_USERNAME" ] ; then
echo "Initiating backup for all repositories in $ORGANIZATION, you can run github-migrator --status to check backup status. Avoid running --backup more than once to avoid duplicates."
for repo in `curl -s -H "Authorization: token $GITHUB_ACCESS_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/orgs/$ORGANIZATION/repos | grep '"full_name"' | awk '{print $2}' | cut -d'/' -f2 | cut -d'"' -f1`; do
if [ -z $repo ]; then
echo "Backup could not be initiated. Please try again after checking dependencies."
exit 3
else
curl -s -H "Authorization: token $GITHUB_ACCESS_TOKEN" -X POST -H "Accept: application/vnd.github.wyandotte-preview+json" -d'{"lock_repositories":false,"repositories":["'$ORGANIZATION'/'$repo'"]}' https://api.github.com/orgs/$ORGANIZATION/migrations -o /dev/null && echo "Backup initiated for $repo." || echo "Backup for $repo cannot be initiated."
fi
done
elif [ "$1" = "--download" ] || [ "$1" = "download" ] && [ -n "$ORGANIZATION" ] && [ -n "$GITHUB_ACCESS_TOKEN" ] && [ -n "$GITHUB_USERNAME" ]; then
backup_status >/dev/null 2>&1
for url in `grep exported ./migration-status | awk '{print $3}'`; do
for name in `grep $url ./migration-status | awk '{print $2}' | cut -d'/' -f2`; do
if [ ! -f ./migration_archive_$name.tar.gz ]; then
echo "Downloading $name .."
curl -s -H "Accept: application/vnd.github.wyandotte-preview+json" -u $GITHUB_USERNAME:$GITHUB_ACCESS_TOKEN -L -o ./migration_archive_$name.tar.gz $url
else [ -f ./migration_archive_$name.tar.gz ]; echo "$name already downloaded. Nothing to do."
fi
done
done
elif [ "$1" = "--status" ] || [ "$1" = "status" ] && [ -n "$ORGANIZATION" ] && [ -n "$GITHUB_ACCESS_TOKEN" ] && [ -n "$GITHUB_USERNAME" ]; then backup_status
elif [ "$1" = "--delete" ] || [ "$1" = "status" ] && [ -n "$ORGANIZATION" ] && [ -n "$GITHUB_ACCESS_TOKEN" ] && [ -n "$GITHUB_USERNAME" ]; then
echo "Deleting and cleaning up all backup archives from github.com. Repositories can still show exported but without archive urls while running --status."
backup_status >/dev/null 2>&1
for archive in `cat ./migration-status | awk '{print $3}'`; do
curl -s -H "Authorization: token $GITHUB_ACCESS_TOKEN" -X DELETE -H "Accept: application/vnd.github.v3+json" $archive
done
elif [ "$1" = "--help" ] || [ "$1" = "help" ]; then
echo "Instructions"
echo "------------"
echo "Requires curl command."
echo "Before proceeding, edit this script and add Organization, Access Token (as owner) and Username."
echo "Usage: ./github-migrator {backup, download, status, delete}"
echo "Eg. ./github-migrator --status"
echo "Avoid running --backup more than once to avoid duplicates."
else
echo "Please run with one of the arguments and by adding Organization, Access Token (as owner) and Username. Check --help for more details."
fi