This commit is contained in:
DoTheEvo 2024-04-21 20:18:29 +02:00
parent 939ad00b1a
commit f524b86606
6 changed files with 49 additions and 85 deletions

View File

@ -1,12 +1,14 @@
# v0.2
# Before using this script, create a repo, note the setting of a password
# kopia repo create filesystem --path C:\kopia_repo --password aaa --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
# kopia repo create filesystem --path C:\kopia_repo --password aaa
# for backblaza b2
# kopia repository create b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0 --password aaa
# useful commands:
# - kopia repo status
# - kopia repo connect filesystem --path C:\kopia_repo -p aaa
# - kopia snap list -all
# - kopia snap list --all
# - kopia mount all K:
# mounting should be used as non-admin user, weird windows thing
# or one does not see the drive, in that case:
@ -17,28 +19,31 @@
# if it was not manually set then it would be
# C:\Windows\System32\config\systemprofile\AppData
#
# to backup multiple targets/paths:
# - [array]$BACKUP_THIS = 'C:\Test','C:\users','C:\blabla'
$REPOSITORY_PATH = 'C:\kopia_repo'
$KOPIA_PASSWORD = 'aaa'
[array]$BACKUP_THIS = 'C:\Test'
$LOG_PATH = 'C:\Kopia\Kopia_Logs'
$USE_SHADOW_COPY = $false
# ----------------------------------------------------------------------------
kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PASSWORD --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
$Env:KOPIA_LOG_DIR = $LOG_PATH
kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PASSWORD
# kopia repository connect b2 --bucket=kopia-repo-rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0
kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=8 --keep-daily=14 --keep-hourly=0 --keep-latest=3 --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=0 --keep-daily=14 --keep-hourly=0 --keep-latest=3
foreach ($path in $BACKUP_THIS) {
if ($USE_SHADOW_COPY) {
kopia policy set $BACKUP_THIS --before-folder-action "powershell -WindowStyle Hidden C:\Kopia\win_vss_before.ps1" --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
kopia policy set $BACKUP_THIS --after-folder-action "powershell -WindowStyle Hidden C:\Kopia\win_vss_after.ps1" --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
}
kopia snapshot create $path --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
if ($USE_SHADOW_COPY) {
kopia policy set --global --enable-volume-shadow-copy=when-available
}
kopia repository disconnect --file-log-level=info --log-dir=C:\Kopia\Kopia_Logs
foreach ($path in $BACKUP_THIS) {
kopia snapshot create $path --file-log-level=info
}
kopia repository disconnect

View File

@ -1,18 +0,0 @@
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
}
}

View File

@ -1,30 +0,0 @@
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}"

View File

@ -56,7 +56,7 @@ Embedded webGUI for server mode is done in React. KopiaUI comes packaged with el
# Some aspects of Kopia
[Official Getting Started Guide](https://kopia.io/docs/getting-started/)<br>
[Official Features](https://kopia.io/docs/features/)<br>
[Features](https://kopia.io/docs/features/)<br>
[Advanced Topics](https://kopia.io/docs/advanced/)
The above linked documentation is well written and worth a look
@ -84,17 +84,20 @@ if planning serious use.
Web GUI versions have button for it, cli version can do `sudo kopia mount all /mnt/temp &`
* **Tasks** section in gui gets wiped when Kopia closes, info on snapshots run
history and duration then has to be find in logs
* **Logs** rotate with max age 30 days or max 1000 log files, 5000 content log files<br>
Useful to search in cli-logs are the terms `kopia/server snapshotting` and
`kopia/server finished`
* **Logs** are creted on every execution of kopia binary.<br>
They rotate by default with max age 30 days, but still can grow hundreds of MB.
* [Compression](https://kopia.io/docs/advanced/compression/) is good and
should be set before backup starts. My go-to is `zstd-fastest`. If backups
feel slow `s2-default` is less cpu heavy but with worse compression.
Useful command: `kopia content stats`
* During snapshots Kopia uses local **cache**, location varies depending on the OS.
Default max size is 5GB, but it gets swept periodically every few minutes.<br>
Useful commands are `kopia cache info` and `kopia cache clear`
* ..
Default max size is 5GB. Cache gets swept periodically every few minutes.<br>
Useful commands are `kopia cache info` and `kopia cache clear`.
* Increase [considerably the max cache size](https://github.com/kopia/kopia/issues/3059#issuecomment-1663479603)
if planning to use cloud storage as the maintenance could eat into egress cost
when kopia redownloads files.
* ...
# Kopia in Linux
@ -125,7 +128,7 @@ After creation the repo is connected, so connnect command is just demonstration.
`sudo kopia policy list`<br>
`sudo kopia policy show --global`<br>
`sudo kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=8 --keep-daily=14 --keep-hourly=0 --keep-latest=3`<br>
`sudo kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=0 --keep-daily=14 --keep-hourly=0 --keep-latest=3`<br>
* **manual backup run**
@ -151,13 +154,14 @@ So both `/home` and `/etc` are set to be backed up.
```bash
#!/bin/bash
# v0.2
# initialize repository
# sudo kopia repo create filesystem --path /mnt/mirror/KOPIA/docker_host_kopia
# for cloud like backblaze
# sudo kopia repository create b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0
# sudo kopia repository connect b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0
# adjust global policy
# sudo kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=8 --keep-daily=14 --keep-hourly=0 --keep-latest=3
# sudo kopia policy set --global --compression=zstd-fastest --keep-annual=0 --keep-monthly=12 --keep-weekly=0 --keep-daily=14 --keep-hourly=0 --keep-latest=3
REPOSITORY_PATH='/mnt/mirror/KOPIA/docker_host_kopia'
BACKUP_THIS='/home /etc'
@ -167,10 +171,11 @@ kopia repository connect filesystem --path $REPOSITORY_PATH
kopia snapshot create $BACKUP_THIS
kopia repository disconnect
# -------------- ERROR EXIT CODES --------------
# kopia does not interupts its run with an error if target or repository are missing
# this makes systemd OnSuccess OnFailure not behaving as they should
# below are checks for paths, that results in immediate error exit code if they do not exist
# they are at the end because some backup might get done even another is missing something
# this makes systemd OnSuccess OnFailure not behaving as one might expect
# below are checks for paths, that result in immediate error exit code if they do not exist
# they are at the end because some backup might get done even if another is missing something
# we just want the error exit code
IFS=' ' read -ra paths <<< "$BACKUP_THIS"
@ -368,6 +373,8 @@ Also use of [nssm](https://nssm.cc/) is popular.
![windows_scoop_install_kopia](https://i.imgur.com/UPZFImh.png)
At the moment **cli is the only way to use VSS snapshots**.
All relevant files are in `C:\Kopia`, from binaries, `repository.config`, to logs.
A scheduled task is imported that executes a powershell script
`C:\Kopia\kopia_backup_scipt.ps1` at 21:19.
@ -375,28 +382,26 @@ The script connects to a set repo and backup set targets.
This approach is bit more hands on than having a gui, but for daily use one
can easily get by with the commands: `kopia snap list -all` and `kopia mount all K:`<br>
Note that mount command should be executed in non admin terminal. Weird
windows thing.
Also at the moment cli is the only way I know how to make kopia actions work,
so that VSS snapshots can be used.
Note that if mount command is not working, try executing it in non admin terminal. Weird
windows thing. Or you need to enable/install `WebClient` service.
* [Download this repo](https://github.com/DoTheEvo/selfhosted-apps-docker/archive/refs/heads/master.zip),
delete everything except `kopia_cli_deploy_win` folder.
* Run `DEPLOY.cmd`
* Removes powershell scripts restriction.
* Creates folder `C:\Kopia` and kopies there<br>
`kopia.exe`, `kopia_backup_scipt.ps1` and the VSS ps1 before and after files.
`kopia.exe`, `kopia_backup_scipt.ps1`.
* Adds `C:\Kopia` to the system env variable PATH.
* imports a task schedule
* imports a scheduled task.
* Read `kopia_backup_scipt.ps1` and follow the instructions there.<br>
Which should be to just to create repo before running the script.
* edit the scheduled task to the prefered time, default is daily at 21:19
* run scheduled task manually
* check if it worked
* `kopia repo status`
* `kopia snap list --all`
The script is set to save logs in to `C:\Kopia`.
The script is set to save logs in to `C:\Kopia\Kopia_Logs\`.
### VSS snapshots
@ -408,11 +413,13 @@ This is what allows backup of open files that are in use.<br>
To make use of this feature edit `kopia_backup_scipt.ps1` changing
`$USE_SHADOW_COPY = $false` to `$USE_SHADOW_COPY = $true`
Note the use of `--enable-actions` which is required for before/after actions
to work.
To check if it's set: `kopia policy show --global`,
should see there: *OS-level snapshot support: Volume Shadow Copy: when-available*
To test if its working, one can execute command `vssadmin list shadows`
to see current VSS snapshots and then execute it again during the backup.
Can also check log files, any named snapshot-creat in cli folder, and see
entries about *volume shadow copy*. Or also one might execute command
`vssadmin list shadows` to see current VSS snapshots and then execute
it again during the backup.
### Kopia install using scoop, machine-wide
@ -632,7 +639,7 @@ For cli just follow [the official documentation.](https://kopia.io/docs/reposito
The example of commands:<br>
* `kopia repository create b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0 --password aaa`
* `kopia repository connect b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0 --password aaa --enable-actions`
* `kopia repository connect b2 --bucket=rakanishu --key-id=001496285081a7e0000000003 --key=K0016L8FAMRp/F+6ckbXIYpP0UgTky0 --password aaa`
The backup script contains example commands, just commented out.