-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackup_github_repositories.ps1
221 lines (169 loc) · 6.92 KB
/
backup_github_repositories.ps1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#Requires -Version 5.0
<#
.SYNOPSIS
Automatically backups all remote GitHub repositories.
.DESCRIPTION
This script automatically backups all remote GitHub repositories of a user or an organisation to a local directory.
.PARAMETER userName
Specifies the GitHub user name.
.PARAMETER userSecret
Specifies the personal access token of the GitHub user.
.PARAMETER organisationName
Specifies the optional GitHub organisation name.
.PARAMETER backupDirectory
Overrides the default backup directory.
.PARAMETER maxConcurrency
Overrides the default concurrency of 8.
.EXAMPLE
.\backup_github_repositories.ps1 -userName "user" -userSecret "token"
.EXAMPLE
.\backup_github_repositories.ps1 -userName "user" -userSecret "token" -organisationName "organisation"
.EXAMPLE
.\backup_github_repositories.ps1 -backupDirectory "C:\myBackupDirectory" -maxConcurrency 1
#>
[CmdletBinding(
DefaultParameterSetName = 'SecureSecret'
)]
Param (
[Parameter(
Mandatory=$True,
HelpMessage="The name of a GitHub user that has access to the GitHub API."
)]
[String]
$userName,
[Parameter(
Mandatory=$True,
HelpMessage="The personal access token of the GitHub user.",
ParameterSetName = 'SecureSecret'
)]
[Security.SecureString]${personal access token},
[Parameter(
Mandatory = $True,
ParameterSetName = 'PlainTextSecret'
)]
[String]
$userSecret,
[String]
$organisationName,
[String]
$backupDirectory,
[ValidateRange(1,256)]
[Int]
$maxConcurrency=8
)
# Consolidate the user secret, either from the argument or the prompt, in a secure string format.
if ($userSecret) {
$secureStringUserSecret = $userSecret | ConvertTo-SecureString -AsPlainText -Force
} else {
$secureStringUserSecret = ${personal access token}
}
# Convert the secure user secret string into a plain text representation.
$plainTextUserSecret = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureStringUserSecret)
)
# Default the backup directory to './YYYY-MM-DD'. This can
# not be done in the Param section because $PSScriptRoot
# will not be resolved if this script gets invoked from cmd.
if (!$backupDirectory) {
$backupDirectory = $(Join-Path -Path "$PSScriptRoot" -ChildPath $(Get-Date -UFormat "%Y-%m-%d"))
}
# Calculates the total repositories size in megabytes based on GitHubs 'size' property.
function Get-TotalRepositoriesSizeInMegabytes([Object] $repositories) {
$totalSizeInKilobytes = 0
ForEach ($repository in $repositories) {
$totalSizeInKilobytes += $repository.size
}
$([math]::Round($totalSizeInKilobytes/1024))
}
# Measure the execution time of the backup script.
$stopwatch = [System.Diagnostics.Stopwatch]::startNew()
# Use TLS v1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#
# Use different API endpoints for user and organisation repositories.
#
# @see https://developer.github.com/v3/repos/#list-organization-repositories
# @see https://developer.github.com/v3/repos/#list-your-repositories
#
if ($organisationName) {
$gitHubRepositoriesUrl = "https://api.github.com/orgs/${organisationName}/repos?type=all&per_page=50"
} else {
$gitHubRepositoriesUrl = "https://api.github.com/user/repos?affiliation=owner&per_page=50"
}
#
# Compose a Basic Authentication request header.
#
# @see https://developer.github.com/v3/auth/#basic-authentication
#
$basicAuthenticationCredentials = "${userName}:${plainTextUserSecret}"
$encodedBasicAuthenticationCredentials = [System.Convert]::ToBase64String(
[System.Text.Encoding]::ASCII.GetBytes($basicAuthenticationCredentials)
)
$requestHeaders = @{
Authorization = "Basic $encodedBasicAuthenticationCredentials"
}
# Request the paginated GitHub API to get all repositories of a user or an organisation.
$repositories = @()
$pageNumber = 0
Do {
$pageNumber++
$paginatedGitHubApiUri = "${gitHubRepositoriesUrl}&page=${pageNumber}"
Write-Host "Requesting '${paginatedGitHubApiUri}'..." -ForegroundColor "Yellow"
$paginatedRepositories = Invoke-WebRequest -Uri $paginatedGitHubApiUri -Headers $requestHeaders | `
Select-Object -ExpandProperty Content | `
ConvertFrom-Json
$repositories += $paginatedRepositories
} Until ($paginatedRepositories.Count -eq 0)
# Print a userfriendly message what will happen next.
$totalSizeInMegabytes = Get-TotalRepositoriesSizeInMegabytes -repositories $repositories
Write-Host "Cloning $($repositories.Count) repositories (~${totalSizeInMegabytes} MB) " -NoNewLine
Write-Host "into '${backupDirectory}' with a maximum concurrency of ${maxConcurrency}:"
# Clone each repository into the backup directory.
ForEach ($repository in $repositories) {
while ($true) {
# Handle completed jobs as soon as possible.
$completedJobs = $(Get-Job -State Completed)
ForEach ($job in $completedJobs) {
$job | Receive-Job
$job | Remove-Job
}
$concurrencyLimitIsReached = $($(Get-Job -State Running).Count -ge $maxConcurrency)
if ($concurrencyLimitIsReached) {
$pollingFrequencyInMilliseconds = 50
Start-Sleep -Milliseconds $pollingFrequencyInMilliseconds
continue
}
# Clone or fetch a remote GitHub repository into a local directory.
$scriptBlock = {
Param (
[Parameter(Mandatory=$true)]
[String]
$fullName,
[Parameter(Mandatory=$true)]
[String]
$directory
)
if (Test-Path "${directory}") {
git --git-dir="${directory}" fetch --quiet --all
git --git-dir="${directory}" fetch --quiet --tags
Write-Host "[${fullName}] Backup completed with git fetch strategy."
return
}
git clone --quiet --mirror "[email protected]:${fullName}.git" "${directory}"
Write-Host "[${fullName}] Backup completed with git clone strategy."
}
# Suffix the repository directory with a ".git" to indicate a bare repository.
$directory = $(Join-Path -Path $backupDirectory -ChildPath "$($repository.name).git")
Write-Host "[$($repository.full_name)] Starting backup to ${directory}..." -ForegroundColor "DarkYellow"
Start-Job $scriptBlock -ArgumentList $repository.full_name, $directory | Out-Null
# Give the job some time to start.
$warmUpTimeoutInMilliseconds = 50
Start-Sleep -Milliseconds $warmUpTimeoutInMilliseconds
break
}
}
# Wait for the last jobs to complete and output their results.
Get-Job | Receive-job -AutoRemoveJob -Wait
$stopwatch.Stop()
$durationInSeconds = [Math]::Floor([Decimal]($stopwatch.Elapsed.TotalSeconds))
Write-Host "Successfully finished the backup in ${durationInSeconds} seconds." -ForegroundColor "Yellow"