forked from no-realm/Bitwarden-Attachment-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitwarden-backup.ps1
More file actions
104 lines (87 loc) · 3.78 KB
/
Bitwarden-backup.ps1
File metadata and controls
104 lines (87 loc) · 3.78 KB
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
# Bitwarden-Attachment-Exporter
# Marviins, edited by justincswong
# Initialization Step
$username = "REPLACE USERNAME HERE" # keep the quotes, your username
$extension = "csv" # csv or json, keep the quotes, your output file format
$gpg = $false # $true or $false, true = gpg encrypt false = skip gpg encrypt
$keyname = "keyName" # gpg recipient, only required if gpg encrypting
$securedlt = $false # $true or $false, true = secure delete false = skip secure delete
$key = $null # don't change this
# Master Password Prompt
$masterPass = Read-Host -assecurestring "Please enter your master password"
$masterPass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($masterPass))
# Attempt Login
while ($key -eq $null) {
try {
$key = bw login $username $masterPass --raw
if ($key -eq $null) {
throw "InvalidPasswordException"
}
}
catch {
$masterPass = Read-Host -assecurestring "`nPlease re-enter your master password"
$masterPass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($masterPass))
}
}
Write-Host "You have logged in." -ForegroundColor Green
$env:BW_SESSION="$key"
# Specify directory and filenames
$backupFolder = 'Backup'
$backupFile = (get-date -Format "yyyyMMdd_hhmmss") + "_Bitwarden_backup.$extension"
$attachmentFolder = (get-date -Format "yyyyMMdd_hhmmss") + '_Attachments'
# Backup Vault
Write-Host "`nExporting Bitwarden Vault"
bw sync
bw export --output "$backupFolder\$backupFile" --format $extension $masterPass
write-host "`n"
# Backup Attachments
$vault = bw list items | ConvertFrom-Json
foreach ($item in $vault){
if($item.PSobject.Properties.Name -contains "Attachments"){
foreach ($attachment in $item.attachments){
$exportName = '[' + $item.name + '] - ' + $attachment.fileName
bw get attachment $attachment.id --itemid $item.id --output "$backupFolder\$attachmentFolder\$exportName"
write-host "`n"
}
}
}
# Logging Out/Termination Prep
Write-Host "The Vault has been successfully backed up."
bw logout
"`n"
# Terminate if not GPG encrypting
if (!$gpg) {
pause
exit
} else {
# GPG Encryption Prep
$cdir = (Get-Item -Path '.\' -Verbose).FullName + "\$backupFolder"
Set-Location $cdir
# GPG Encryption Step
Write-Host "Your backup file is now being encrypted with key: " -NoNewline
Write-Host $keyname -ForegroundColor Yellow -NoNewline
Write-Host "."
try {
gpg --output "$backupFile.gpg" --encrypt --recipient $keyname $backupFile
if (!(Test-Path -path "$backupFile.gpg")) {
throw "InvalidRecipientException"
}
Write-Host "Your backup file has been successfully encrypted!" -ForegroundColor Green
}
catch {
Write-Host "ERROR: Please open the script and review your recipient.`n" -ForegroundColor DarkRed
}
finally {
# File Cleanup
Write-Host "`nCleaning up the Backup folder. " -NoNewline
Remove-Item -Path "$cdir\$backupFile" -Force
# Secure File Cleanup
if ($securedlt) {
Write-Host "This will take some time."
cipher /w:$cdir
}
}
}
Write-Host "`nFile cleanup completed." -ForegroundColor Green
pause
exit