Skip to content

Run rustic periodically from a PowerShell script

License

Notifications You must be signed in to change notification settings

Chouffy/restic_backup_powershell_script

Repository files navigation

Restic Backup PowerShell Script

This set of scripts allow the user to launch periodically restic using PowerShell from a Windows or Linux environment.

Goals:

  • Only one script to maintain different systems and OSes (Windows and Linux)
  • Do a basic restic housekeeping: backup, forget, check
    • prune is absent as this should be a supervised activity
    • unlock is also absent for the same reason - the backup WILL fail if there's a stall lock
  • Avoid backup on metered connection (Windows only)
  • One dashboard overview via HealthChecks.io
  • Simple enough, so I can understand what's going on

Beware that I'm not a PowerShell expert, I've learned along the way.
I've tested this set of script with PowerShell 7.3 on Windows 10/11, Raspberry Pi OS and Ubuntu Server.

Overview of user scrips

_main_script.ps1

This script can be called periodically to execute restic backup, restic forget and restic check and must have two parameters:

  • $BackupConfigPath, which is the folder that contains common_*.ps1 and config_*.ps1 parameters files
  • $BackupConfigName, which is the file name of the config_*.ps1 file in the $BackupConfigPath which has specific parameters for the backup

Example: ./_main_script.ps1 "/home/restic/config" "config_template.ps1"

_environment_initialize_CLI_call.ps1

Can be used when the user needs a restic environment to do other actions.
Parameters are the same than _main_script.ps1.

Example: . ./environment_initialize_CLI_call.ps1 ./config/ config_template.ps1; restic stats

_function_update_CLI_call.ps1

Can be used to start self-update of restic and rclone.
Parameters are /path/to/restic/executable and /path/to/rclone/executable

Example: sudo pwsh ./function_update_CLI_call.ps1 "/opt/restic/restic" "/opt/rclone/rclone"

Setup

  1. Have PowerShell installed: built-in on Windows, see here on Linux
  2. git clone this repository
  3. Create log/ folder which MUST be in the same folder than the main script
  4. Rename and/or move the config_templates/ to build your own config/ folder.
    1. The config folder MUST contain common.ps1
    2. The config folder MUST contain common_windows.ps1 or common_linux.ps1
    3. The config folder MUST contain the script that will be called in $BackupConfigName
  5. If you use rclone, set-up common.ps1 with either rclone.conf or environment variables for your own configuration
  6. Set-up common_windows.ps1 or common_unix.ps1 with the path to restic and rclone.
    1. You can download restic and rclone here, and put the binaries in a fixed folder like /opt/
  7. Set-up config_template.ps1 for your own configuration
    1. Set up backup source, target, password, …
    2. Set up HealthChecks.io UUID to monitor backup state and receive logs
      1. 3 Checks are required, one for each restic phase
      2. Suggested naming convention for each Check: restic ClientName BackupSource 1-backup, … 2-forget, … 3-check
  8. Call _main_script.ps1 </path/to/config/folder> <config_name.ps1>
  9. Set up cron and be happy!