Last active
January 12, 2017 22:03
-
-
Save thierryx96/e7d4b25e975a978c913b81d0d05152ae to your computer and use it in GitHub Desktop.
GitHub Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$token = "" | |
$org = "acme" | |
$gitRepoUri = "https://api.github.com/orgs/$org/repos?access_token=$token" | |
$workdir = "C:/backup/git" | |
$git = "C:\Program Files\Git\cmd\git.exe" | |
$branches = "master","develop" # branches to zip | |
#$filter = "Team"; # leave empty if all repo | |
#restore : | |
# 1) go to the folder (.git) | |
# 2) create the repo on github | |
# 3) git remote set-url origin [email protected]:<username>/<repo>.git (git remote set-url origin https://github.com/<username>/<repo>.git) | |
#import-module $awsS3Module | |
mkdir $workdir -Force | |
$data = Invoke-RestMethod -Method Get -Uri $gitRepoUri | |
New-Item -path $profile -type file –force | |
foreach ($repo in $data) | |
{ | |
Write-Output($repo) | |
if(($filter.Length -gt 0) -And !($repo.name.Contains($filter))){ | |
Write-Output("skip : " + $repo.name) | |
continue; | |
} | |
Write-Output("Starting backup for github repository: " + $repo.name) | |
## create the repo backup dir (or remove) | |
$codeDir = "$workdir/" + $repo.name | |
If (Test-Path $codeDir){ | |
Remove-Item $codeDir -Recurse -Force | |
} | |
mkdir $codeDir -Force | |
cd $codeDir | |
## backup source code (zip /develop and /master from github) | |
$codeUrl = $repo.clone_url | |
$gitCodeCommand = "clone --bare $codeUrl" | |
foreach ($branch in $branches) | |
{ | |
Write-Output("Download zip for branch: " + $branch + " to " + $codeDir + "/" + $branch + ".zip") | |
$zipMasterCodeUri = $repo.url + "/zipball/" + $branch | |
try { | |
Invoke-RestMethod -Uri $zipMasterCodeUri -Headers @{"Authorization" = "token $token"} -OutFile ($codeDir + "/" + $branch + ".zip") | |
} catch { | |
# brnach not found, | |
if($_.Exception.Response.StatusCode.value__ = 404){ | |
Write-Output ("branch " + $branch + " not found (" + $_.Exception.Response.StatusCode.value__ + ")") | |
} | |
else{ | |
throw $_.Exception; | |
} | |
} | |
} | |
# Backup the whole git repo (metadata, code, branches, commits ... ) | |
Write-Output "Backup $codeUrl code into $codeDir" | |
Write-Output $gitCodeCommand | |
Start-Process -FilePath $git -ArgumentList $gitCodeCommand -Wait -NoNewWindow; | |
## backup the git repo wiki | |
$wikiDir = "$workdir/" + $repo.name | |
$wikiUrl = $repo.clone_url.Replace(".git", ".wiki.git") | |
$gitWikiCommand = "clone --bare $wikiUrl" | |
Write-Output "Backup $wikiUrl wiki into $wikiDir" | |
cd $wikiDir | |
Start-Process -FilePath $git -ArgumentList $gitWikiCommand -Wait -NoNewWindow; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment