From b2cff6ad68f08f9c8606310be491a87429681f77 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 26 Jan 2023 14:26:03 +0000 Subject: [PATCH] first for win --- .gitignore | 4 ++++ backup.bat | 1 + backup.ps1 | 37 +++++++++++++++++++++++++++++++++++++ env.sample.ps1 | 22 ++++++++++++++++++++++ init.ps1 | 3 +++ readme.md | 2 ++ snapshots.ps1 | 3 +++ 7 files changed, 72 insertions(+) create mode 100644 .gitignore create mode 100644 backup.bat create mode 100644 backup.ps1 create mode 100644 env.sample.ps1 create mode 100644 init.ps1 create mode 100644 readme.md create mode 100644 snapshots.ps1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f184348 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +env.ps1 +exclusions.txt +pre +post \ No newline at end of file diff --git a/backup.bat b/backup.bat new file mode 100644 index 0000000..d145366 --- /dev/null +++ b/backup.bat @@ -0,0 +1 @@ +powershell -File %~dp0/backup.sh \ No newline at end of file diff --git a/backup.ps1 b/backup.ps1 new file mode 100644 index 0000000..45eb6d7 --- /dev/null +++ b/backup.ps1 @@ -0,0 +1,37 @@ + +$dir = Split-Path $script:MyInvocation.MyCommand.Path +. "$dir/env.ps1" +$DATE = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") +Write-Host "Backup $NAME at $DATE" +$BACKUP_CMD="restic backup $BACKUP_DIR" +if ( $BACKUP_PARAMS ){ + $BACKUP_CMD="$BACKUP_CMD $BACKUP_PARAMS" +} +if ( $BACKUP_EXCLUDE_FILE ) { + $BACKUP_CMD="$BACKUP_CMD --exclude-file $dir/$BACKUP_EXCLUDE_FILE" +} +if ( $USE_VSS -eq 1 ) { + $BACKUP_CMD = "$BACKUP_CMD --use-windows-vss" +} +#Write-Host $BACKUP_CMD +$running = get-process | ?{$_.path -eq "C:\Windows\system32\restic.exe"} +Write-Host $running +if ( $running ) { + Write-Host "Backup already running; $running. Exiting..." + curl https://hc-ping.com/$HEALTHCHECK_UUID/fail + exit +} +Write-Host "Not currently running. Continuing..." +curl https://hc-ping.com/$HEALTHCHECK_UUID/start +if ( $PRE_RUN_SCRIPT ) { + . $dir/$PRE_RUN_SCRIPT +} +Invoke-Expression $BACKUP_CMD +if ( $LastExitCode -eq 0 ) { + if ( $POST_RUN_SCRIPT ) { + . $dir/$POST_RUN_SCRIPT + } + curl https://hc-ping.com/$HEALTHCHECK_UUID +} else { + curl https://hc-ping.com/$HEALTHCHECK_UUID/fail +} diff --git a/env.sample.ps1 b/env.sample.ps1 new file mode 100644 index 0000000..bbf0c61 --- /dev/null +++ b/env.sample.ps1 @@ -0,0 +1,22 @@ +# Name your backup (only really for logging purposes) +$NAME="Backup Name" +# The directory to back up +$BACKUP_DIR="/dir/to/back/up" +# any additional params you want with restic +$BACKUP_PARAMS="" +# an exclude file, generally I use "exclusions.txt" that's ignored by git +$BACKUP_EXCLUDE_FILE="" +# https://healthchecks.io uuid +$HEALTHCHECK_UUID="" +# name of a script to run before (ideally in the "pre/" dir) +$PRE_RUN_SCRIPT="" +# name of a script to run after (ideally in the "post/" dir) +$POST_RUN_SCRIPT="" +# Use VSS +$USE_VSS = 1 + + +$Env:AWS_ACCESS_KEY_ID= +$Env:AWS_SECRET_ACCESS_KEY= +$Env:RESTIC_REPOSITORY="s3:https://repo.tld/bucket/sub-bucket" +$Env:RESTIC_PASSWORD=V3RYSecuRepaSsWord \ No newline at end of file diff --git a/init.ps1 b/init.ps1 new file mode 100644 index 0000000..539fe80 --- /dev/null +++ b/init.ps1 @@ -0,0 +1,3 @@ +$dir = Split-Path $script:MyInvocation.MyCommand.Path +. $dir/env.ps1 +Invoke-Expression "restic init" \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..efd3204 --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +A set of scripts to simplify running restic backup jobs via scheduler. +Also includes curl resquests to healthchecks.io service for monitoring purposes diff --git a/snapshots.ps1 b/snapshots.ps1 new file mode 100644 index 0000000..de3702e --- /dev/null +++ b/snapshots.ps1 @@ -0,0 +1,3 @@ +$dir = Split-Path $script:MyInvocation.MyCommand.Path +. $dir/env.ps1 +Invoke-Expression "restic snapshots" \ No newline at end of file