-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backup.ps1
163 lines (130 loc) · 5.62 KB
/
Backup.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
[CmdletBinding(DefaultParameterSetName='CoreOptions')]
Param(
# Access token for DropBox.
# https://www.dropbox.com/developers/apps
[Parameter(Mandatory=$true)]
[string]$AccessToken,
# Directory with files to backup.
[Parameter(Mandatory=$true)]
[string]$BackupPath,
# Archive prefix to use on DropBox.
[Parameter(Mandatory=$true)]
[string]$BackupPrefix,
# Optional HTTP proxy to use.
[Parameter(Mandatory=$false)]
[string]$ProxyAddress,
# Optional username.
[Parameter(ParameterSetName='Authentication', Mandatory=$true)]
[string]$ProxyCredentialUsername,
# Optional password supplied as encrypted standard string (output from ConvertFrom-SecureString)
# For example: "password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
[Parameter(ParameterSetName='Authentication', Mandatory=$true)]
[string]$ProxyCredentialPassword
)
function Upload-FileToDropbox {
<#
.Description
Uploads file to DropBox.
https://markb.uk/powershell-dropbox-rolling-database-backup.html
#>
param(
# File on the disk to upload.
[Parameter(Mandatory=$true)]
[string]$SourcePath,
# Location on DropBox to place file in.
[Parameter(Mandatory=$true)]
[string]$TargetPath,
# DropBox OAuth Token
[Parameter(Mandatory=$true)]
[string]$AccessToken,
# Optional proxy address.
[Parameter(Mandatory=$false)]
[string]$Proxy,
# Optional proxy credentials
[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]$ProxyCredential
)
$body = '{ "path": "' + $TargetPath + '", "mode": "overwrite" }'
$authorization = "Bearer $AccessToken"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $body)
$headers.Add("Content-Type", 'application/octet-stream')
$DropBoxUploadAPI = "https://content.dropboxapi.com/2/files/upload"
# Check whether we have the proxy paramter set
if ($PSBoundParameters.ContainsKey('Proxy')) {
# Check whether we have the proxy credential paramter set
if ($PSBoundParameters.ContainsKey('ProxyCredential')) {
Invoke-RestMethod -Uri $DropBoxUploadAPI `
-Method Post `
-InFile $SourcePath `
-Headers $headers `
-Proxy $Proxy `
-ProxyCredential $ProxyCredential
} else {
Invoke-RestMethod -Uri $DropBoxUploadAPI `
-Method Post `
-InFile $SourcePath `
-Headers $headers `
-Proxy $Proxy
}
} else {
Invoke-RestMethod -Uri $DropBoxUploadAPI `
-Method Post `
-InFile $SourcePath `
-Headers $headers
}
}
function Convert-FolderToArchive {
<#
.Description
Converts a folder and all of its contents into a ZIP archive with the specfied prefix.
Function will return path to ZIP archive.
#>
param (
# Folder with files to archive..
[Parameter(Mandatory=$true)]
[string]$FolderPath,
# Prefix to create archive with.
[Parameter(Mandatory=$true)]
[string]$FilePrefix
)
$DateStamp = (Get-Date).ToString("yyyy-MM-dd-HHmm")
$TempFolderLocation = [System.IO.Path]::GetTempPath()
$ArchiveName = $TempFolderLocation + $FilePrefix + '-' + $DateStamp + '.zip'
Compress-Archive -Path $FolderPath -DestinationPath $ArchiveName
return $ArchiveName
}
# Check if ProxyAddress parameter is set when credentails are entered, if not then prompt with error.
if ($PSBoundParameters.ContainsKey('ProxyCredentialUsername') -and (-not $PSBoundParameters.ContainsKey('ProxyAddress'))) {
Write-Output -InputObject "You are missing the ProxyAddress parameter."
exit
}
# Convert folder with files into a ZIP archive in TMP directory
$FileToUpload = Convert-FolderToArchive -FolderPath $BackupPath -FilePrefix $BackupPrefix
# Get the file name of the ZIP archive
$CloudFileLocation = '/' + (Split-Path -Path $FileToUpload -Leaf)
# Upload file based on parameters set.
if ($PSBoundParameters.ContainsKey('ProxyAddress')) {
if ($PSBoundParameters.ContainsKey('ProxyCredentialUsername')) {
# Upload through proxy with credentials
Upload-FileToDropbox -SourcePath $FileToUpload `
-TargetPath $CloudFileLocation `
-AccessToken $AccessToken `
-Proxy $ProxyAddress `
-ProxyCredential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ProxyCredentialUsername, ($ProxyCredentialPassword | ConvertTo-SecureString))
} else {
# Upload through proxy
Upload-FileToDropbox -SourcePath $FileToUpload `
-TargetPath $CloudFileLocation `
-AccessToken $AccessToken `
-Proxy $ProxyAddress
}
} else {
# Upload directly
Upload-FileToDropbox -SourcePath $FileToUpload `
-TargetPath $CloudFileLocation `
-AccessToken $AccessToken
}
# Remove created archive
Remove-Item -Path $FileToUpload