This commit is contained in:
DoTheEvo 2023-08-31 20:16:44 +02:00
parent b52f9b30ec
commit 33d4ac66d8
14 changed files with 289 additions and 69 deletions

View File

@ -1,11 +0,0 @@
$REPOSITORY_PATH = 'C:\Backup'
$BACKUP_THIS = 'C:\Users'
$KOPIA_PASSWORD='aaa'
kopia policy set $BACKUP_THIS --before-folder-action "powershell -WindowStyle Hidden C:\win_vss_before.ps1"
kopia policy set $BACKUP_THIS --after-folder-action "powershell -WindowStyle Hidden C:\win_vss_after.ps1"
kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PASSWORD
kopia snapshot create $BACKUP_THIS
kopia repository disconnect

View File

@ -0,0 +1,49 @@
@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 - copying kopia.exe in to system32
robocopy "%~dp0\" "C:\Windows\System32" "kopia.exe" /NDL /NJH /NJS
echo.
echo - checking if C:\Kopia folder exists, creating it if not
if not exist "C:\Kopia\" (
mkdir C:\Kopia
)
if exist "C:\Kopia\kopia_backup_scipt.ps1" (
echo - C:\Kopia\kopia_backup_scipt.ps1 exists, renaming it with random suffix
ren "C:\Kopia\kopia_backup_scipt.ps1" "kopia_backup_scipt_%random%.ps1"
)
echo - copying files to C:\Kopia
robocopy "%~dp0\" "C:\Kopia" "kopia_backup_scipt.ps1" /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
echo.
if exist C:\Windows\System32\Tasks\kopia_backup_schedule (
echo - scheduled task with that name already exists, skipping
echo - delete the task in taskschd.msc if you want fresh import
) else (
echo - importing scheduled task kopia_backup_schedule
schtasks.exe /Create /XML "%~dp0\kopia_backup_schedule.xml" /tn "kopia_backup_schedule"
)
echo.
echo --------------------------------------------------------------
echo.
echo DEPLOYMENT DONE, RUN THE FOLLOWING COMMAND TO INITIALIZE A REPO
echo kopia repo create filesystem --path C:\kopia_repo --password aaa
pause

View File

@ -0,0 +1,38 @@
# Before using this script, initiate the repo
# kopia repo create filesystem --path C:\kopia_repo --password aaa
# useful commands
# kopia repo connect filesystem --path C:\kopia_repo -p aaa
# 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
# net use - shows path that can be pasted to explorer or browser
# \\127.0.0.1@51295\DavWWWRoot
# logs location when run as task scheduler
# C:\Windows\System32\config\systemprofile\AppData
# config
# example of $BACKUP_THIS with multiple paths
# [array]$BACKUP_THIS = 'C:\Test','C:\Test2','C:\Test3'
$REPOSITORY_PATH = 'C:\kopia_repo'
$KOPIA_PASSWORD='aaa'
[array]$BACKUP_THIS = 'C:\Test'
$USE_SHADOW_COPY = $false
# ----------------------------------------------------------------------------
kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PASSWORD --enable-actions
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"
kopia policy set $BACKUP_THIS --after-folder-action "powershell -WindowStyle Hidden C:\Kopia\win_vss_after.ps1"
}
kopia snapshot create $path --file-log-level=info
}
kopia repository disconnect

View File

@ -0,0 +1,50 @@
@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
echo.
if exist C:\Windows\System32\Tasks\kopia_server_backup_start (
echo - scheduled task with that name already exists, skipping
echo - delete the task in taskschd.msc if you want fresh import
) else (
echo - importing scheduled task that starts Kopia Server on boot
schtasks /Create /XML "%~dp0\kopia_server_backup_start.xml" /tn "kopia_server_backup_start"
)
echo - starting Kopia Server
schtasks /run /tn kopia_server_backup_start
echo.
echo --------------------------------------------------------------
echo.
echo DEPLOYMENT DONE
echo KOPIA SERVER CAN NOW BE FIND AT WEB PAGE: localhost:51515
echo.
pause

Binary file not shown.

View File

@ -0,0 +1,2 @@
chdir /d C:\Kopia
start /B kopia server start --insecure --address=127.0.0.1:51515 --server-username=admin --server-password=aaa

View File

@ -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
}
}

View File

@ -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}"

View File

@ -17,51 +17,47 @@ Backups.
Kopia is a new open source backup utility with basicly **all** modern features.</br>
Cross-platform, deduplication, encryption, compression, multithreaded speed,
native cloud storage support, GUI versions, repository replication, snapshots mounting,...
native cloud storage support, repository replication, snapshots mounting,
GUI versions, server version,...
Written in golang.
In this setup kopia cli is used to backup docker containers and the host,
but general use and concepts are universal.</br>
# Some aspects of Kopia
* Kopia configuraiton uses term policies to apply to various
- global policy, from which repos inherit settings
- repos policy created on repo creation
* Backup configuration is stored in a repository where backups are stored.<br>
* You connect to a repository before using it, and disconnect afterwards.<br>
Only one repository can be connected at the time(at least for cli version).
* Currently to ignore a folder - `CACHEDIR.TAG` file can be placed inside,
with specific [content](https://bford.info/cachedir/)
and set policy: `--ignore-cache-dirs true`
There are 3 ways to go about running kopia
* **cli** - Command line.<br>
You call the binary passing some commands, it executes stuff, done.<br>
Requires extra work - scripts with configs, scheduling.
* **KopiaUI** - GUI version of kopia.<br>
Easier managment, takes uppon itself scheduling.<br>
Drawback is that it runs under one user and only when logged in.
* **Kopia Server** - running kopia and its webserver in the background<br>
Managment through web browser at an url, can run as a docker container.
[Official Getting Started Guide](https://kopia.io/docs/getting-started/)<br>
[Official Features](https://kopia.io/docs/features/)
* Kopia is a single ~35MB binary file.
* Backups are stored in a **repository** that needs to be created first,
and is always encrypted - requires password.
* Before any action, Kopia needs to connecto to a repo.
* **Snapshot**, appart from typical meaning, kopia also uses it to for
targets(paths) to be backed up.
* **Policy** is a term used to define behaviour of the backup/repo,
like backups retention, what to ignore, logging, scheduling(server/UI),
actions before and after backup,...
* **Policies** are stored inside a repo and can apply at various levels and
can inherit from each other
- global policy, the default that comes predefined during repo creation
- per user policy and per machine policy
- snapshot level policy, only applying for that one path
* Maintence is automatic
* ..
# Files and directory structure
# Kopia on a linux machine
```
/home/
│ └── ~/
│ └── docker/
│ ├── container-setup #2
│ ├── container-setup #1
│ ├── ...
/mnt/
│ └── mirror/
│ └── KOPIA/
│ └── arch_docker_host/
/opt/
└── kopia-backup-home-etc.sh
```
only the script `kopia-backup-home-etc.sh` in /opt is created<br>
uf, systemd unit files too, but I am not "drawing" /etc/systemd/system/ up there...
even this will probably get deleted
# The setup
cli version of kopia will be used, with a script and systemd-timers for scheduling.
### install kopia
@ -69,8 +65,14 @@ for arch linux, kopia is on AUR `yay kopia-bin`
### the initial steps
General use of sudo so that kopia has access everywhere.
use of sudo so that kopia has access everywhere<br>
* **repo creation**
`mkdir -p /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repo create filesystem --path /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repo connect filesystem --path /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repo status`<br>
* **the policy info and change**
@ -78,13 +80,6 @@ use of sudo so that kopia has access everywhere<br>
`sudo kopia policy list`<br>
`sudo kopia policy set --global --ignore-cache-dirs true --keep-annual 1 --keep-monthly 6 --keep-weekly 4 --keep-daily 14 --keep-hourly 0 --keep-latest 14`<br>
* **repo creation**
`mkdir -p /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repository create filesystem --path /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repository connect filesystem --path /mnt/mirror/KOPIA/docker_host_kopia`<br>
`sudo kopia repository status`<br>
* **manual run**
`sudo kopia snapshot create /home/spravca/docker /etc`<br>
@ -92,17 +87,18 @@ use of sudo so that kopia has access everywhere<br>
* **mounting a backup**
`sudo kopia snapshot list`<br>
`sudo kopia mount all /mnt/tmp &` - mounts all snapshots<br>
`sudo kopia snapshot list`<br>
`sudo kopia mount k7e2b0a503edd7604ff61c68655cd5ad7 /mnt/tmp &`<br>
`sudo umount /mnt/tmp`<br>
### the backup script
### The backup script
`/opt/kopia-backup-home-etc.sh`
```bash
#!/bin/bash
#sudo kopia policy set --global --ignore-cache-dirs true --keep-annual 1 --keep-monthly 6 --keep-weekly 4 --keep-daily 14 --keep-hourly 0 --keep-latest 14
#sudo kopia policy set --global --keep-annual 1 --keep-monthly 6 --keep-weekly 4 --keep-daily 14 --keep-hourly 0 --keep-latest 14
REPOSITORY_PATH='/mnt/mirror/KOPIA/docker_host_kopia'
BACKUP_THIS='/home /etc'
@ -112,6 +108,8 @@ kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PA
kopia snapshot create $BACKUP_THIS
kopia repository disconnect
```
make the script executable<br>
`sudo chmod +x /opt/kopia-backup-home-etc.sh`
### Scheduled backups using systemd
@ -119,9 +117,11 @@ Usually cron is used, but systemd provides better logging and control,
so better get used to using it.<br>
[Heres](https://github.com/kopia/kopia/issues/2685#issuecomment-1384524828)
some discussion on unit files.<br>
[ntfy](https://github.com/binwiederhier/ntfy) is used for notifications,
[ntfy](https://github.com/binwiederhier/ntfy) can be used for notifications,
more info [here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/gotify-ntfy-signal#linux-systemd-unit-file-service)
* `sudo micro /etc/systemd/system/kopia-home-etc.service`
```kopia-home-etc.service```
```ini
[Unit]
@ -147,6 +147,7 @@ Environment="HOME=/root"
ExecStart=/opt/kopia-backup-home-etc.sh
```
* `sudo micro /etc/systemd/system/kopia-home-etc.timer`
```kopia-home-etc.timer```
```ini
@ -162,6 +163,10 @@ Persistent=true
WantedBy=timers.target
```
* `sudo systemctl enable --now kopia-home-etc.timer`
* `systemctl status kopia-home-etc.timer`
* `journalctl -u kopia-home-etc.timer` - see history
# Mounting network storage using systemd
* files are placed in `/etc/systemd/system`
@ -206,18 +211,53 @@ WantedBy=multi-user.target
# Kopia in Windows
While GUI version seems like a way to go.. its not there yet.
The way the schedule is running - it uses is running only under a user, theres no certainty it will run.
## Kopia Server on Windows
So here goes cli version
* download this repo, delete shit, keep `kopia_server_deploy` folder
* run `DEPLOY.cmd`, it will
* Removes powershell scripts restriction.
* Creates folder `C:\Kopia` and kopies files there
* imports a task schedule that will start Kopia Server at boot
* visit in browser `localhost:51515`
* setup repo
* setup what to backup and schedule
* edit the `kopia_backup_scipt.ps1`, set what to backup and where
* for the same repo location execute<br>
`sudo kopia repository create filesystem --path C:\Backup`
* run the script
* [download](https://github.com/kopia/kopia/releases/) latest named kopia-X.XX.X-windows-x64.zip
, \~11MB
* extract, move to `C:\kopia`
* download `win_vss_before.ps1` and `win_vss_after.ps1` from this repo,
or crete them from
[here](https://kopia.io/docs/advanced/actions/#windows-shadow-copy)
* kopia-backup-home-etc.sh
## Kopia cli on Windows
While GUI version seems like a way to go.. IMO its not there yet.
The schedule seems to be dependant on user logging in...
and general weird feeling of poor GUI quality.
So here goes cli version. As after some hands-on experience with cli version
the GUI version might click in better too, later.
* download this repo, delete shit, keep `kopia-deploy` folder
* run `DEPLOY.cmd`, it will
* Removes powershell scripts restriction.
* Install scoop, sudo, kopia.
* Creates folder `C:\Kopia` and kopies there<br>
`kopia_backup_scipt.ps1` and the VSS ps1 before and after files.
* imports a task schedule
* edit the `kopia_backup_scipt.ps1`, set what to backup and where
* for the same repo location execute<br>
`sudo kopia repository create filesystem --path C:\Backup`
* run the script
To do the above things manualy:
* install kopia using scoop
* open terminal as admin
* `Set-ExecutionPolicy RemoteSigned`
* `iex "& {$(irm get.scoop.sh)} -RunAsAdmin"`
* `scoop install sudo --global`
* `sudo scoop install kopia --global`
* download this repo, extract to `c:\kopia`
* edit kopia-backup-home-etc.sh as see fit
* powershell as as administrator
* --enable-actions
@ -227,3 +267,7 @@ So here goes cli version
kopia policy set <target_dir> --before-folder-action "powershell -WindowStyle Hidden <path_to_script>\before.ps1"
kopia policy set <target_dir> --after-folder-action "powershell -WindowStyle Hidden <path_to_script>\after.ps1"