diff --git a/kopia_backup/kopia_server_deploy_win_service/DEPLOY.cmd b/kopia_backup/kopia_server_deploy_win_service/DEPLOY.cmd new file mode 100644 index 0000000..71853f8 --- /dev/null +++ b/kopia_backup/kopia_server_deploy_win_service/DEPLOY.cmd @@ -0,0 +1,54 @@ +@echo off + +:: checking if the script is run as administrator +net session >nul 2>&1 +if %errorLevel% == 0 ( + echo - Success: Administrative permissions confirmed. +) else ( + echo - RUN AS ADMINISTRATOR + pause + exit /B +) + +echo - powershell ExecutionPolicy changing to Bypass +powershell.exe Set-ExecutionPolicy -ExecutionPolicy Bypass + +echo - checking if C:\Kopia folder exists, creating it if not +if not exist "C:\Kopia\" ( + mkdir C:\Kopia +) + +if exist "C:\Kopia\kopia_server_start.cmd" ( + echo - C:\Kopia\kopia_server_start.cmd exists, renaming it with random suffix + ren "C:\Kopia\kopia_server_start.cmd" "kopia_backup_scipt_%random%.ps1" +) + +echo - copying files to C:\Kopia +robocopy "%~dp0\" "C:\Kopia" "kopia.exe" /NDL /NJH /NJS +robocopy "%~dp0\" "C:\Kopia" "kopia_server_start.cmd" /NDL /NJH /NJS +robocopy "%~dp0\" "C:\Kopia" "win_vss_before.ps1" /NDL /NJH /NJS +robocopy "%~dp0\" "C:\Kopia" "win_vss_after.ps1" /NDL /NJH /NJS +robocopy "%~dp0\" "C:\Kopia" "shawl.exe" /NDL /NJH /NJS +echo. + +echo - creting Kopia service +C:\Kopia\shawl.exe add --log-dir C:\kopia\Kopia_service_logs --name Kopia -- C:\Kopia\kopia_server_start.cmd + +echo - setting Kopia service to start automaticly at boot +sc config Kopia start=auto + +echo - start Kopia service +sc start Kopia + + +echo - copying link to Desktop +robocopy "%~dp0\" "%USERPROFILE%\Desktop" "Kopia.url" /NDL /NJH /NJS + +echo. +echo -------------------------------------------------------------- +echo. +echo DEPLOYMENT DONE +echo KOPIA SERVER CAN NOW BE FIND AT WEB PAGE: localhost:51515 +echo A LINK SHOULD BE ON YOUR DESKTOP +echo. +pause diff --git a/kopia_backup/kopia_server_deploy_win_service/Kopia.url b/kopia_backup/kopia_server_deploy_win_service/Kopia.url new file mode 100644 index 0000000..fbd84cb --- /dev/null +++ b/kopia_backup/kopia_server_deploy_win_service/Kopia.url @@ -0,0 +1,6 @@ +[InternetShortcut] +URL=http://localhost:51515/ +IDList= +HotKey=0 +[{000214A0-0000-0000-C000-000000000046}] +Prop3=19,2 diff --git a/kopia_backup/kopia_server_deploy_win_service/kopia.exe b/kopia_backup/kopia_server_deploy_win_service/kopia.exe new file mode 100644 index 0000000..3878b58 Binary files /dev/null and b/kopia_backup/kopia_server_deploy_win_service/kopia.exe differ diff --git a/kopia_backup/kopia_server_deploy_win_service/kopia_server_start.cmd b/kopia_backup/kopia_server_deploy_win_service/kopia_server_start.cmd new file mode 100644 index 0000000..e178ab0 --- /dev/null +++ b/kopia_backup/kopia_server_deploy_win_service/kopia_server_start.cmd @@ -0,0 +1,2 @@ +chdir /d C:\Kopia +start /B 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 diff --git a/kopia_backup/kopia_server_deploy_win_service/shawl.exe b/kopia_backup/kopia_server_deploy_win_service/shawl.exe new file mode 100644 index 0000000..90b0b5a Binary files /dev/null and b/kopia_backup/kopia_server_deploy_win_service/shawl.exe differ diff --git a/kopia_backup/kopia_server_deploy_win_service/win_vss_after.ps1 b/kopia_backup/kopia_server_deploy_win_service/win_vss_after.ps1 new file mode 100644 index 0000000..01c9fb8 --- /dev/null +++ b/kopia_backup/kopia_server_deploy_win_service/win_vss_after.ps1 @@ -0,0 +1,18 @@ +if ($args.Length -eq 0) { + $kopiaSnapshotId = $env:KOPIA_SNAPSHOT_ID +} else { + $kopiaSnapshotId = $args[0] +} + +if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { + $mountPoint = Get-Item "${PSScriptRoot}\${kopiaSnapshotId}" + $mountedVolume = $mountPoint.Target + + cmd /c rmdir $mountPoint + Get-CimInstance -ClassName Win32_ShadowCopy | Where-Object { "$($_.DeviceObject)\" -eq "\\?\${mountedVolume}" } | Remove-CimInstance +} else { + Start-Process 'powershell' '-f', $MyInvocation.MyCommand.Path, $kopiaSnapshotId -Verb RunAs -WindowStyle Hidden -Wait + if ($proc.ExitCode) { + exit $proc.ExitCode + } +} diff --git a/kopia_backup/kopia_server_deploy_win_service/win_vss_before.ps1 b/kopia_backup/kopia_server_deploy_win_service/win_vss_before.ps1 new file mode 100644 index 0000000..e3692b1 --- /dev/null +++ b/kopia_backup/kopia_server_deploy_win_service/win_vss_before.ps1 @@ -0,0 +1,30 @@ +if ($args.Length -eq 0) { + $kopiaSnapshotId = $env:KOPIA_SNAPSHOT_ID + $kopiaSourcePath = $env:KOPIA_SOURCE_PATH +} else { + $kopiaSnapshotId = $args[0] + $kopiaSourcePath = $args[1] +} + +$sourceDrive = Split-Path -Qualifier $kopiaSourcePath +$sourcePath = Split-Path -NoQualifier $kopiaSourcePath +# use Kopia snapshot ID as mount point name for extra caution for duplication +$mountPoint = "${PSScriptRoot}\${kopiaSnapshotId}" + +if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { + $shadowId = (Invoke-CimMethod -ClassName Win32_ShadowCopy -MethodName Create -Arguments @{ Volume = "${sourceDrive}\" }).ShadowID + $shadowDevice = (Get-CimInstance -ClassName Win32_ShadowCopy | Where-Object { $_.ID -eq $shadowId }).DeviceObject + if (-not $shadowDevice) { + # fail the Kopia snapshot early if shadow copy was not created + exit 1 + } + + cmd /c mklink /d $mountPoint "${shadowDevice}\" +} else { + $proc = Start-Process 'powershell' '-f', $MyInvocation.MyCommand.Path, $kopiaSnapshotId, $kopiaSourcePath -PassThru -Verb RunAs -WindowStyle Hidden -Wait + if ($proc.ExitCode) { + exit $proc.ExitCode + } +} + +Write-Output "KOPIA_SNAPSHOT_PATH=${mountPoint}${sourcePath}" diff --git a/kopia_backup/readme.md b/kopia_backup/readme.md index 97a66d4..f8c55f7 100644 --- a/kopia_backup/readme.md +++ b/kopia_backup/readme.md @@ -267,28 +267,24 @@ WantedBy=multi-user.target ## KopiaUI in Windows While KopiaUI seems like the way to go because of the simple deployment and -use, it has a drawback. The way the schedule works, where the user must be -logged in for backups to take place is something to be very aware of.
+use, it has a drawback. The way the schedule works - that the user must be +logged in for backups to take place. -Othewise KopiaUI does not really need guidance here. -It just works for any normal use. +Othewise KopiaUI does not need guide. It just works for normal use. ## Kopia Server in Windows -Kopia binary is copied in to `C:\Kopia\` and a scheduled task is imported -that start kopia on boot in server mode. -In server mode kopia runs in the background, with its web server -answering at url: `localhost:51515` where it can be managed. +Kopia always running in the background, but also webgui to manage it in. * [Download this repo](https://github.com/DoTheEvo/selfhosted-apps-docker/archive/refs/heads/master.zip), - delete everything except `kopia_server_deploy_win` folder. -* Run `DEPLOY.cmd`, it will: + delete everything except `kopia_backup/kopia_server_deploy_win_service` folder. +* Run `DEPLOY.cmd` as admin, it will: * Removes powershell scripts restriction. - * Creates folder `C:\Kopia` and copies files there - * Imports a task schedule that will start `C:\Kopia\kopia_server_start.cmd`
- * Places `kopia.url` on the current user's desktop -* Check content of `C:\Kopia\kopia_server_start.cmd`
- note or change the credentials, the default: `admin // aaa` + * Creates folder `C:\Kopia` and copies files there. + * Uses [shawl](https://github.com/mtkennerly/shawl) to crate Kopia service. + * Places `kopia.url` on the current user's desktop. +* One should check content of `C:\Kopia\kopia_server_start.cmd`
+ note credentials set there: `admin // aaa` * Visit in browser `localhost:51515` * Setup new repo through webgui. * Setup what to backup and schedule. @@ -297,12 +293,16 @@ Kopia should now run on boot and be easy to manage through web GUI.
Be it creating backup jobs, mounting old snapshots to restore files, or just looking around if all works as it should. -It is also popular to use [nssm](https://nssm.cc/) to start up and manage -Kopia as a service. +All relevant files are in `C:\Kopia`, from binaries, `repository.config`, to logs. -While Kopia runs now in server mode, the fact that we pass `--insecure` flag -means it cant serve as a repository for other kopia instances. -For that look at docker deployment section or in to making changes to the cmd file. +Kopia server runs in insecure mode, so no https and no actual server for other +machines on network to use, just local deployment. + +Before shawl, task scheduler was used.
+This [matushorvath/Kopia as Windows Service](https://gist.github.com/matushorvath/dd7148c201ceae03ddebc1b4bbef4d20) +guide helped move beyond that. It contains more info if one would want to +actually run as server repository for other machines.
+Also use of [nssm](https://nssm.cc/) is popular. ## Kopia cli in Windows