Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amritanshu-pandey/a0f77ba8a7b9c366edb48355c90d3df7 to your computer and use it in GitHub Desktop.
Save amritanshu-pandey/a0f77ba8a7b9c366edb48355c90d3df7 to your computer and use it in GitHub Desktop.
Use Restic to perform backups to Backblaze B2.
#requires -version 4
<#
.SYNOPSIS
Restic backup script.
.DESCRIPTION
Use Restic to perform backups to Backblaze B2.
.PARAMETER Repository
The path to the Restic repository, e.g. 'b2:my-named-repo'.
.PARAMETER Folder
The folder to be backed up, e.g. 'E:\richard\Documents'
.PARAMETER ExcludesFile
A file of excludes, e.g. 'E:\richard\Source\scripts\excludes.txt'.
.INPUTS
None.
.OUTPUTS
None.
.NOTES
Version: 1.0
Author: Richard Bogle
Creation Date: 2019-11-14
Purpose/Change: Initial script development
Restic was installed via Scoop.
Set up [Task Scheduler](https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler).
.EXAMPLE
PS C:\> . .\Backup-Folders.ps1 -Folder 'E:\richard\Documents' -Repository 'b2:bogle-restic' -ExcludesFile 'E:\richard\Source\scripts\excludes.txt'
#>
Param (
[Parameter(Mandatory=$True)]
[string]$Repository,
[Parameter(Mandatory=$True)]
[string]$Folder,
[Parameter()]
[string]$ExcludesFile
)
# Set Error Action to Silently Continue
#$ErrorActionPreference = 'SilentlyContinue'
Function SetupEnvironment {
Param ()
Begin {
Import-Module CredentialManager
}
Process {
$env:RESTIC_REPOSITORY=$Repository
$credential = Get-StoredCredential -Target bogle-restic -AsCredentialObject
$env:B2_ACCOUNT_ID = $credential.UserName
$env:B2_ACCOUNT_KEY = $credential.Password
$credential = Get-StoredCredential -Target restic -AsCredentialObject
$env:RESTIC_PASSWORD = $credential.Password
}
End {
}
}
Function BackupFolders {
Param ()
Begin {
SetupEnvironment
$logFile = $PSCommandPath.replace('.ps1', '.log')
Start-Transcript -Path $logFile
Write-Host 'Backup folders to B2 using Restic.'
}
Process {
Try {
if (-not (restic snapshots)) {
throw 'Cannot find repository.'
}
if (-not [string]::IsNullOrWhiteSpace($ExcludesFile) -and (Test-Path -Path $ExcludesFile)) {
restic backup $Folder --exclude-file=$ExcludesFile
}
else {
restic backup $Folder
}
}
Catch {
Write-Error $_.Exception.Message
Break
}
}
End {
If ($?) {
Write-Host 'Completed Successfully.'
Write-Host ' '
}
Stop-Transcript
}
}
BackupFolders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment