You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
17 KiB
Markdown

1 year ago
# Kopia
###### guide-by-example
![logo](https://i.imgur.com/A2mosM6.png)
10 months ago
1 year ago
WORK IN PROGRESS<br>
WORK IN PROGRESS<br>
WORK IN PROGRESS<br>
10 months ago
# Content
10 months ago
* [Kopia in Linux](#Kopia-in-Linux)
* [Kopia in Windows](#Kopia-in-Windows)
10 months ago
* [Kopia in Docker](#Kopia-in-Docker)
1 year ago
# Purpose & Overview
Backups.
* [Official site](https://kopia.io/)
10 months ago
* [Official Forum](https://kopia.discourse.group/)
1 year ago
* [Github](https://github.com/kopia/kopia)
10 months ago
Kopia is a new open source backup utility with basically **all** modern features.</br>
1 year ago
Cross-platform, deduplication, encryption, compression, multithreaded speed,
10 months ago
native cloud storage support, repository replication, snapshots mounting,
10 months ago
GUI version, server version,...
1 year ago
10 months ago
Written in golang.<br>
10 months ago
Embedded webGUI for server mode is done in React. KopiaUI comes packaged with electron.
1 year ago
10 months ago
### Ways to use Kopia
10 months ago
* **cli** - Command line.<br>
10 months ago
You call the kopia binary passing some commands, it executes stuff, done.<br>
Deployment requires extra work - scripts with configs, scheduling.
10 months ago
* **Kopia Server** - kopia binary runs in server mode.<br>
10 months ago
Runs in the background, with its web server answering at url: `localhost:51515`<br>
10 months ago
Web GUI makes the management easier than using cli. Additionally in server mode
kopia can serve as a centralized repository for other machines that run kopia instances.<br>
10 months ago
Deployment requires extra work similar to cli, but actual use is through web GUI.
* **KopiaUI** - GUI version.<br>
10 months ago
Kopia that comes packaged with electron to provide the feel of a standalone desktop app.<br>
10 months ago
Good for simple deployment where average user just wants to backup stuff.<br>
Benefits over cli or server is easier setup and management.<br>
Drawback is that it runs under one user and only when that user is logged in.
* **Kopia in Docker** - Kopia Server running in docker<br>
Can fulfill two needs:
* Backup docker-host stuff to a cloud or a mounted network storage.
Managed through webgui instead of cli.
* A centralized kopia repository where other machines on the network,
that also use kopia, backup their data.
![repo_first](https://i.imgur.com/rbqhmzZ.png)
# Some aspects of Kopia
10 months ago
[Official Getting Started Guide](https://kopia.io/docs/getting-started/)<br>
10 months ago
[Kopia Build Architecture](https://github.com/kopia/kopia/blob/master/BUILD.md)<br>
10 months ago
[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,
10 months ago
and is always encrypted.<br>
Before any action, Kopia needs to connect to a repo.
10 months ago
* **Snapshots**, apart from the typical meaning, kopia also uses the term for
10 months ago
targets(paths) that are being backed up.
* **Policy** is a term used to define behavior of the backup/repo,
10 months ago
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
10 months ago
can **inherit** from each other
- **Global** policy, the default that comes predefined during repo creation,
can be edited like any other.
- Per user policy, per machine policy.
- Snapshot level policy, only applying for that one path.
* **Maintenance** is automatic
* 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`
10 months ago
* **Retention** of backups - [here's](https://kopia.discourse.group/t/trying-to-understand-retention-policies/164/4)
how it works under the hood.<br>
10 months ago
* **Restore** from backups is most easily done by mounting a snapshot.<br>
Web GUI versions have button for it, cli version can do `sudo kopia mount all /mnt/temp &`
10 months ago
* **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
10 months ago
* [Compression](https://kopia.io/docs/advanced/compression/) is good,
`s2-default` seems decent.
1 year ago
* ..
1 year ago
10 months ago
# Kopia in Linux
1 year ago
10 months ago
![list_snapshots_cli](https://i.imgur.com/lQ8W5yh.png)
1 year ago
10 months ago
cli version of kopia will be used to periodically backup to a mounted network storage.<br>
The backup script will be executed using systemd-timers for scheduling.
1 year ago
10 months ago
### Install Kopia
1 year ago
10 months ago
For arch linux, kopia is on AUR `yay kopia-bin`
1 year ago
10 months ago
### The initial steps and general use commands
1 year ago
10 months ago
* **repo creation**
10 months ago
10 months ago
`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>
10 months ago
`sudo kopia repo status`
If the path used during creation does not exists, kopia will create it in full.<br>
After creation the repo is connected, so connnect command is just demonstration.
1 year ago
1 year ago
* **the policy info and change**
`sudo kopia policy get --global`<br>
`sudo kopia policy list`<br>
10 months ago
`sudo kopia policy set --global --keep-annual 2 --keep-monthly 6 --keep-weekly 4 --keep-daily 14 --keep-hourly 0 --keep-latest 3`<br>
1 year ago
10 months ago
* **manual backup run**
1 year ago
1 year ago
`sudo kopia snapshot create /home/spravca/docker /etc`<br>
`sudo kopia snapshot list`<br>
1 year ago
10 months ago
Since the connection exists with a repo,
all that is needed is target that should be backed up.
* **mounting backups**
1 year ago
10 months ago
`sudo kopia mount all /mnt/tmp &` - mounts all snapshots<br>
`sudo kopia snapshot list`<br>
1 year ago
`sudo kopia mount k7e2b0a503edd7604ff61c68655cd5ad7 /mnt/tmp &`<br>
`sudo umount /mnt/tmp`<br>
1 year ago
10 months ago
### The backup script
1 year ago
10 months ago
In linux, passing multiple paths separated by space seems to work fine.<br>
So both `/home` and `/etc` are set to be backed up.
1 year ago
`/opt/kopia-backup-home-etc.sh`
1 year ago
```bash
1 year ago
#!/bin/bash
1 year ago
10 months ago
# initialize repository
# sudo kopia repo create filesystem --path /mnt/mirror/KOPIA/docker_host_kopia
# adjust global policy
# sudo kopia policy set --global --keep-annual 2 --keep-monthly 6 --keep-weekly 4 --keep-daily 14 --keep-hourly 0 --keep-latest 3
1 year ago
1 year ago
REPOSITORY_PATH='/mnt/mirror/KOPIA/docker_host_kopia'
BACKUP_THIS='/home /etc'
1 year ago
KOPIA_PASSWORD='aaa'
1 year ago
1 year ago
kopia repository connect filesystem --path $REPOSITORY_PATH --password $KOPIA_PASSWORD
1 year ago
kopia snapshot create $BACKUP_THIS
kopia repository disconnect
```
10 months ago
10 months ago
make the script executable<br>
`sudo chmod +x /opt/kopia-backup-home-etc.sh`
1 year ago
1 year ago
### Scheduled backups using systemd
1 year ago
1 year ago
Usually cron is used, but systemd provides better logging and control,
1 year ago
so better get used to using it.<br>
[Heres](https://github.com/kopia/kopia/issues/2685#issuecomment-1384524828)
1 year ago
some discussion on unit files.<br>
10 months ago
[ntfy](https://github.com/binwiederhier/ntfy) can be used for notifications,
1 year ago
more info [here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/gotify-ntfy-signal#linux-systemd-unit-file-service)
10 months ago
* `sudo micro /etc/systemd/system/kopia-home-etc.service`
1 year ago
```kopia-home-etc.service```
```ini
[Unit]
Description=kopia backup
Wants=network-online.target
After=network-online.target
ConditionACPower=true
1 year ago
# OnFailure=ntfy@failure-%p.service
1 year ago
# OnSuccess=ntfy@success-%p.service
1 year ago
[Service]
Type=oneshot
# Lower CPU and I/O priority.
Nice=19
CPUSchedulingPolicy=batch
IOSchedulingPriority=7
IPAccounting=true
PrivateTmp=true
Environment="HOME=/root"
1 year ago
ExecStart=/opt/kopia-backup-home-etc.sh
1 year ago
```
10 months ago
* `sudo micro /etc/systemd/system/kopia-home-etc.timer`
1 year ago
```kopia-home-etc.timer```
1 year ago
```ini
1 year ago
[Unit]
Description=Run kopia backup
[Timer]
1 year ago
OnCalendar=*-*-* 02:00:00
RandomizedDelaySec=10min
1 year ago
Persistent=true
[Install]
WantedBy=timers.target
```
1 year ago
10 months ago
* `sudo systemctl enable --now kopia-home-etc.timer`
* `systemctl status kopia-home-etc.timer`
* `journalctl -u kopia-home-etc.timer` - see history
10 months ago
<details>
<summary><h3>Mounting network storage using systemd</h3></summary>
1 year ago
1 year ago
* files are placed in `/etc/systemd/system`
1 year ago
* the name of mount and automount files MUST correspond with the path<br>
1 year ago
replacing `/` with a `-`,
but otherwise it must be the mounting path in the name
* for mounting that does not fail on boot if there are network issues,
1 year ago
and mounts the target only on request - enable `automount` file,
not `mount` file, so:<br>
1 year ago
`sudo systemctl enable mnt-mirror.automount`
1 year ago
1 year ago
`mnt-mirror.mount`
```ini
[Unit]
Description=3TB truenas mirror mount
1 year ago
1 year ago
[Mount]
What=//10.0.19.11/Mirror
Where=/mnt/mirror
Type=cifs
Options=rw,username=kopia,password=aaa,file_mode=0644,dir_mode=0755,uid=1000,gid=1000
1 year ago
1 year ago
[Install]
WantedBy=multi-user.target
```
`mnt-mirror.automount`
```ini
[Unit]
Description=3TB truenas mirror mount
[Automount]
Where=/mnt/mirror
[Install]
WantedBy=multi-user.target
```
1 year ago
10 months ago
</details>
---
---
1 year ago
10 months ago
# Kopia in Windows
1 year ago
10 months ago
![windows_snapshot_history_gui](https://i.imgur.com/fI6uhdo.png)
1 year ago
10 months ago
## KopiaUI in Windows
10 months ago
While KopiaUI seems like the way to go because of the simple deployment and
10 months ago
use, it has a drawback. The way the schedule works - that the user must be
logged in for backups to take place.
10 months ago
10 months ago
Othewise KopiaUI does not need guide. It just works for normal use.
10 months ago
## Kopia Server in Windows
10 months ago
Kopia always running in the background, but also webgui to manage it in.
10 months ago
10 months ago
* [Download this repo](https://github.com/DoTheEvo/selfhosted-apps-docker/archive/refs/heads/master.zip),
10 months ago
delete everything except `kopia_backup/kopia_server_deploy_service_win` folder.
10 months ago
* Run `DEPLOY.cmd` as admin, it will:
10 months ago
* Removes powershell scripts restriction.
10 months ago
* Creates folder `C:\Kopia` and copies files there.
10 months ago
* Uses [shawl](https://github.com/mtkennerly/shawl) to create Kopia service.
10 months ago
* Places `kopia.url` on the current user's desktop.
* One should check content of `C:\Kopia\kopia_server_start.cmd`<br>
10 months ago
that's where credentials are set, default: `admin // aaa`
10 months ago
* Visit in browser `localhost:51515`
* Setup new repo through webgui.
10 months ago
* Setup what to backup, compression and schedule.
10 months ago
10 months ago
Kopia should now run on boot and be easy to manage through web GUI.<br>
10 months ago
Be it creating backup jobs, mounting old snapshots to restore files,
or just looking around if all works as it should.
10 months ago
10 months ago
All relevant files are in `C:\Kopia`, from binaries, `repository.config`, to logs.
10 months ago
10 months ago
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.<br>
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.<br>
Also use of [nssm](https://nssm.cc/) is popular.
10 months ago
10 months ago
## Kopia cli in Windows
10 months ago
10 months ago
![windows_scoop_install_kopia](https://i.imgur.com/UPZFImh.png)
10 months ago
10 months ago
Kopia binary is copied in to `C:\Windows\System32\`
and a scheduled task is imported that executes a powershell script
`C:\Kopia\kopia_backup_scipt.ps1` at 21:19.
The script executes few kopia commands - connects to a repo, backs up stuff,
and disconnects.
Bit more hands on than having a gui, but once setup one can easily get by with
two 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.
10 months ago
10 months ago
* [Download this repo](https://github.com/DoTheEvo/selfhosted-apps-docker/archive/refs/heads/master.zip),
10 months ago
delete everything except `kopia_cli_deploy_win` folder.
10 months ago
* Run `DEPLOY.cmd`, it will:
10 months ago
* Removes powershell scripts restriction.
10 months ago
* kopies kopia.exe in to `C:\Windows\System32`
10 months ago
* Creates folder `C:\Kopia` and kopies there<br>
`kopia_backup_scipt.ps1` and the VSS ps1 before and after files.
* imports a task schedule
10 months ago
* Read `kopia_backup_scipt.ps1` and follow the instructions there.<br>
Which should be to just to create repo before running the script.<br>
10 months ago
`kopia repo create filesystem --path C:\kopia_repo --password aaa`
* edit the scheduled task to the prefered time, default is daily at 21:19
* run scheduled task manually
* check if it worked
* `kopia snap list --all`
### VSS snapshots
10 months ago
Volume Shadow Copy Service freezes the state of the disk in time and makes
10 months ago
this snapshot available to use.
This is what allows backup of open files that are in use.<br>
10 months ago
[Here's some youtube video on VSS.](https://youtu.be/RUwocwP2ilI?t=85)
10 months ago
10 months ago
To make use of this feature edit `kopia_backup_scipt.ps1` changing
`$USE_SHADOW_COPY = $false` to `$USE_SHADOW_COPY = $true`
10 months ago
Note the use of `--enable-actions` in the backup script `kopia_backup_scipt.ps1`,
10 months ago
which is required for before/after actions to work.
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.
10 months ago
### Kopia install using scoop, machine-wide
Just something to have note of, if decided to switch to heavy scoop use.
* open terminal as admin
* `Set-ExecutionPolicy Bypass`
* `iex "& {$(irm get.scoop.sh)} -RunAsAdmin"`
* `scoop install sudo --global`
* `sudo scoop install kopia --global`
---
---
10 months ago
10 months ago
# Kopia in Docker
1 year ago
10 months ago
![kopia_docker_logs](https://i.imgur.com/w57KHvp.png)
1 year ago
10 months ago
### Files and directory structure
10 months ago
10 months ago
```
/mnt/
└── mirror/
/home/
└── ~/
└── docker/
└── kopia/
├── kopia_config/
├── kopia_cache/
├── kopia_logs/
├── some_data/
├── kopia_repository/
├── kopia_tmp/
├── .env
└── docker-compose.yml
```
* `/mnt/mirror/...` - a mounted network storage share
* `kopia_config/` - repository.config and ui-preferences.json
* `kopia_cache/` - cache
* `kopia_logs/` - logs
* `some_data/` - some data to be backed up
* `kopia_repository/` - repository location
* `kopia_tmp/` - temp used for snapshots
* `.env` - a file containing environment variables for docker compose
* `docker-compose.yml` - a docker compose file, telling docker how to run the containers
### docker-compose
The data to be backed up are mounted in read only mode.<br>
10 months ago
To be able to mount snapshots, extra privileges are required and fuse access.
10 months ago
```
services:
kopia:
image: kopia/kopia:latest
container_name: kopia
hostname: kopia
restart: unless-stopped
env_file: .env
privileged: true
cap_add:
- SYS_ADMIN
security_opt:
- apparmor:unconfined
devices:
- /dev/fuse:/dev/fuse:rwm
ports:
- "51515:51515"
command:
- server
- start
- --tls-generate-cert
- --disable-csrf-token-checks
- --address=0.0.0.0:51515
- --server-username=$USERNAME
- --server-password=$KOPIA_PASSWORD
volumes:
# Mount local folders needed by kopia
- ./kopia_config:/app/config
- ./kopia_cache:/app/cache
- ./kopia_logs:/app/logs
# Mount local folders to snapshot
- ./some_data:/data:ro
# Mount repository location
10 months ago
- /mnt/mirror/kopia_repository:/repository
10 months ago
# Mount path for browsing mounted snaphots
- ./kopia_tmp:/tmp:shared
networks:
default:
name: $DOCKER_MY_NETWORK
external: true
```
`.env`
```bash
DOCKER_MY_NETWORK=caddy_net
TZ=Europe/Bratislava
# KOPIA
USERNAME=admin
KOPIA_PASSWORD=aaa
```
**All containers must be on the same network**.</br>
Which is named in the `.env` file.</br>
If one does not exist yet: `docker network create caddy_net`
### Reverse proxy
10 months ago
10 months ago
Caddy is used, details
[here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2).</br>
To function as a repository server, Kopia must be setup with https,
which is achieved by using `--tls-generate-cert` flag and removal
of `--insecure` flag.
So now Kopia sits behind Caddy, but caddy needs to be told the traffic is
https and to ignore that the certificate is not valid.
`Caddyfile`
```
kopia.{$MY_DOMAIN} {
reverse_proxy kopia:51515 {
transport http {
tls
tls_insecure_skip_verify
}
}
}
```
10 months ago
10 months ago
### First run
![kopia_repo_setup_first_run](https://i.imgur.com/mnn66Hj.png)
* visit kopia.example.com
* create new repository as `Local Directory or NAS`, set path to `/repository`,
set password
Now this container can do backups of mounted stuff in to other mounted places
10 months ago
or cloud, while managed through webgui.
10 months ago
To also make it function as a repository server a user account needs to be added.
The users are stored in the repo.
* exec in to the container<br>
`docker container exec -it kopia /bin/bash`
* add user@machine and set the password<br>
`kopia server user add user1@machine1`
10 months ago
* on another machine test with koppiaUI, on the first run:<br>
10 months ago
* Pick `Kopia Repository Server`
* Server address: `https://kopia.example.com:443`
* *Trusted server certificate fingerprint (SHA256)*<br>
can be left empty, or if you put something there, it gives you error
where it tells you fingerprints of the server to pick from.
* In advanced option one can override user@machine with the one set
when exec-ed in to the docker container.
Or exec again there and add another one.
### Troubleshooting
10 months ago
* check kopia docker container logs, I like using [ctop](https://github.com/bcicen/ctop)
* `nslookup kopia.example.com` check if you are getting to you server from client
10 months ago
* Make sure you use port 443 in server address.
10 months ago