-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-github.ps1
124 lines (102 loc) · 3.73 KB
/
backup-github.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
using module './config.psm1'
using module AWSPowerShell.NetCore
param
(
[string] $configPath = 'config.json',
[switch] $dryRun = $false
)
if ($dryRun)
{
Write-Host 'RUNNING IN DRY-RUN MODE. NO ACTUAL CHANGES WILL BE DONE.'
}
$config = [Config] (Get-Content $configPath | ConvertFrom-JSON)
$base64Token = [System.Convert]::ToBase64String([char[]]$config.Token)
$headers =
@{
Authorization = 'Basic {0}' -f $base64Token
};
Set-Location -Path $config.BackupDirectory
$page = 1
$perPage = 30
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
do
{
Write-Host "Getting page: $page"
$response = Invoke-RestMethod -Headers $headers -Uri "https://api.github.com/user/repos?page=$page&per_page=$perPage"
foreach ($repo in $response)
{
$repoName = $repo.name
$repoPath = "$($config.BackupDirectory)/$repoName"
Write-Host "Processing repo at path: $repoPath"
if ($dryRun)
{
# Just to test SSH key. If it doesn't have access we should see permission errors in the output
Write-Host "Fetching with dry run parameter"
git fetch --dry-run
}
else
{
if ((Test-Path $repoPath) -eq 0)
{
# Repo doesn't exist, clone it
Write-Host "Repo doesn't exist, clone it"
git clone $repo.ssh_url
}
else
{
# Repo exists
Write-Host "Repo exists, update"
# Change to repo directory to fetch updates
Set-Location -Path $repoPath
git fetch --all
git reset --hard origin/master
# Change back to root backup directory
Set-Location -Path $config.BackupDirectory
}
}
}
$page = $page + 1
}
while ($response.Count -gt 0)
Write-Host "Backup completed"
# Send notifications
if ($config.Notification.Sns.Enabled)
{
Write-Host "Publishing to SNS topic $($config.Notification.Sns.TopicArn)"
if (-not $dryRun)
{
$result = @{}
$result.MessageId = [guid]::NewGuid()
$result.ResultCode = "OK"
$result.Message = "Success"
$result.Timestamp = [System.DateTime]::UtcNow
$messageJson = "{ ""MessageId"": ""$([guid]::NewGuid())"", ""ApplicationId"": ""$applicationId"", ""ResultCode"": ""OK"", ""Message"": ""Success"", ""Timestamp"": ""$([System.DateTime]::UtcNow)"" }"
Write-Host $messageJson
Publish-SNSMessage -TopicArn $config.Notification.Sns.TopicArn `
-Message $messageJson `
-Subject $config.Notification.Subject `
-AccessKey $config.Notification.Sns.AccessKey `
-SecretKey $config.Notification.Sns.secretKey `
-Region $config.Notification.Sns.Region
}
}
if ($config.Notification.Email.Enabled)
{
Write-Host "Sending email to $($config.Notification.Email.ToEmailAddress)"
if (-not $dryRun)
{
Send-MailMessage `
-From $config.Notification.Email.FromEmailAddress `
-To $config.Notification.Email.ToEmailAddress `
-Subject $config.Notification.Subject `
-Body $config.Notification.MessageBody `
-SmtpServer $config.Notification.Email.SmtpHost `
-UseSsl `
-Port $config.Notification.Email.SmtpPort `
-Credential $( `
New-Object System.Management.Automation.PSCredential `
-argumentlist $config.Notification.Email.SmtpUsername, `
$(ConvertTo-SecureString -AsPlainText -String $config.Notification.Email.SmtpPassword -Force) `
)
}
}