diff --git a/kopia_backup/kopia_server_deploy_service_win/add_NAS_shares.ps1 b/kopia_backup/kopia_server_deploy_service_win/add_NAS_shares.ps1 new file mode 100644 index 0000000..c858724 --- /dev/null +++ b/kopia_backup/kopia_server_deploy_service_win/add_NAS_shares.ps1 @@ -0,0 +1,54 @@ +$NasName = "NAS-2" +$Excluded = @("something1", "something2") # skip these shares + +$env:KOPIA_CONFIG_PATH = "C:\Kopia\repository.config" + +# Get list of shares from the NAS +$Shares = net view "\\$NasName" | ForEach-Object { + if ($_ -match '^(\S+)\s+Disk\s*$') { + $matches[1] + } +} | Where-Object { $_ -notin $Excluded } + +# Get list of Kopia policies and their UNC paths +# Filters out the global policy which has an empty path. +$KopiaPolicyPaths = kopia policy list --json | ConvertFrom-Json | ForEach-Object { $_.target.path } | Where-Object { $_ } + +Write-Host "====== checking for new shares to add ======" + +Write-Host "..." +foreach ($Share in $Shares) { + $UncPath = "\\$NasName\$Share" + + # Check whether share has a kopia policy defined + $HasPolicy = $KopiaPolicyPaths | Where-Object { $_ -ieq $UncPath } + + if (-not $HasPolicy) { + Write-Host "$UncPath - creating a new policy" + + # Temporarily relax EAP as Kopia writes information messages in to stderr + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = "Continue" + + # Kopia requires at least one flag explicitly defined + $output = kopia policy set $UncPath --keep-latest inherit 2>&1 + + $ErrorActionPreference = $prevEAP + + # Exit code is the real success/failure signal not the stderr + # non-zero here means Kopia genuinely failed. + if ($LASTEXITCODE -ne 0) { + Write-Warning "Failed to set policy for $UncPath (exit code $LASTEXITCODE): $output" + } + } +} +Write-Host "..." +Write-Host "============== check done ================" +Write-Host "" +Write-Host "============= current policies =============" + +kopia policy list --json | ConvertFrom-Json | ForEach-Object { $_.target.path } | Where-Object { $_ } + +Write-Host "" +Write-Host "============= ignored shares ===============" +$Excluded | ForEach-Object { "\\$NasName\$_" } diff --git a/kopia_backup/kopia_server_deploy_service_win/kopia_server_start.cmd b/kopia_backup/kopia_server_deploy_service_win/kopia_server_start.cmd index 919de8a..be6884c 100644 --- a/kopia_backup/kopia_server_deploy_service_win/kopia_server_start.cmd +++ b/kopia_backup/kopia_server_deploy_service_win/kopia_server_start.cmd @@ -1,4 +1,11 @@ -kopia server start --insecure --config-file=C:\Kopia\repository.config --log-dir=C:\Kopia\Kopia_Logs --address=127.0.0.1:51515 --server-username=admin --server-password=aaa --enable-actions +kopia server start ^ +--config-file=C:\Kopia\repository.config ^ +--insecure ^ +--address=127.0.0.1:51515 ^ +--server-username=admin --server-password=aaa ^ +--log-dir=C:\Kopia\Kopia_Logs ^ +--cache-directory=C:\Kopia\Kopia_Cache ^ +--enable-actions :: to have full functinality of a kopia server :: replace the above command with the one below