Skip to content

Instantly share code, notes, and snippets.

@fdcastel
Created August 24, 2023 18:02
Show Gist options
  • Save fdcastel/9c160e6f250947c842e7fd8c3901a6c8 to your computer and use it in GitHub Desktop.
Save fdcastel/9c160e6f250947c842e7fd8c3901a6c8 to your computer and use it in GitHub Desktop.
Demo for issue: Kopia not running actions when snapshot source is a single file.
#
# Demo for issue: Kopia not running actions when snapshot source is a single file.
#
# Tested with
# Kopia 0.13.0
# Windows Server 2022
#
# Demo settings
$DEMO_ROOT_FOLDER = 'C:\kopia-test\' # Any temp folder on your machine (without spaces!)
$USE_FILE_AS_SOURCE = $false # Set this to $true to reproduce the bug
# Kopia settings
$env:KOPIA_CONFIG_PATH = Join-Path $DEMO_ROOT_FOLDER 'repository.config'
$env:KOPIA_PASSWORD='dummy-passw0rd'
$env:KOPIA_CHECK_FOR_UPDATES='false' # Disables check for Kopia updates on GitHub (--no-check-for-updates)
#
# Main
#
$MY_REPOSITORY = Join-Path $DEMO_ROOT_FOLDER 'Repository\'
$LOGFILE = Join-Path $DEMO_ROOT_FOLDER 'log.txt'
$MY_SOURCE = if ($USE_FILE_AS_SOURCE) {
Join-Path $DEMO_ROOT_FOLDER 'Source\EFI\memtest.efi'
} else {
Join-Path $DEMO_ROOT_FOLDER 'Source\'
}
# Create source folder with some files to backup
if ($USE_FILE_AS_SOURCE) {
$sourceFolder = Split-Path -Path $MY_SOURCE -Parent
mkdir $sourceFolder -Force -ErrorAction SilentlyContinue | Out-Null
Copy-Item -Path 'C:\Windows\Boot\EFI\memtest.efi' -Destination $sourceFolder
} else {
mkdir $MY_SOURCE -Force -ErrorAction SilentlyContinue | Out-Null
ROBOCOPY /S 'C:\Windows\Boot' $MY_SOURCE | Out-Null
}
# Create local filesystem repository (if does not exists).
if (-not (Test-Path $MY_REPOSITORY)) {
kopia repository create filesystem --path $MY_REPOSITORY --enable-actions
}
# Connect
kopia repository connect filesystem --path $MY_REPOSITORY --enable-actions
# Configure policies (before & after snapshot)
$beforeScriptFile = Join-Path $DEMO_ROOT_FOLDER 'before.ps1'
@"
echo "$(Get-Date -Format 's') BEFORE script was called." | Add-Content -Path $LOGFILE
exit 0 # Force exit code = 0
"@ | Out-File $beforeScriptFile
kopia policy set $MY_SOURCE --before-snapshot-root-action "powershell.exe -File $beforeScriptFile"
$afterScriptFile = Join-Path $DEMO_ROOT_FOLDER 'after.ps1'
@"
Start-Sleep -Seconds 5
echo "$(Get-Date -Format 's') AFTER script was called." | Add-Content -Path $LOGFILE
exit 0 # Force exit code = 0
"@ | Out-File $afterScriptFile
kopia policy set $MY_SOURCE --after-snapshot-root-action "powershell.exe -File $afterScriptFile"
# Create snapshot -- This should run BEFORE and AFTER scripts.
kopia snapshot create $MY_SOURCE
# List snapshots
# kopia snapshot list
# Disconnect
# kopia repository disconnect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment