1
0
Fork 0
mirror of https://github.com/yavook/kiwi-backup.git synced 2024-11-25 08:03:01 +00:00
kiwi-backup/README.md

276 lines
9 KiB
Markdown
Raw Normal View History

2020-08-27 14:53:19 +00:00
# kiwi-backup
2022-02-27 15:42:51 +00:00
[![Build Status](https://github.drone.yavook.de/api/badges/yavook/kiwi-backup/status.svg)](https://github.drone.yavook.de/yavook/kiwi-backup)
2021-09-21 16:02:56 +00:00
2020-08-27 14:53:19 +00:00
> `kiwi` - simple, consistent, powerful
2022-03-03 00:05:33 +00:00
The backup solution for [`kiwi-scp`](https://github.com/yavook/kiwi-scp). Also [on Docker Hub](https://hub.docker.com/r/yavook/kiwi-backup).
2020-08-27 14:53:19 +00:00
## Quick start
2022-03-03 00:05:33 +00:00
kiwi-backup is an image with [duplicity](https://duplicity.gitlab.io/duplicity-web/), tailored to backup service data of `kiwi-scp` instances.
2020-08-28 01:22:05 +00:00
2022-02-27 15:42:51 +00:00
If you want backups in the host directory `/var/local/kiwi.backup`, just add this to one of your projects' `docker-compose.yml` to use the default configuration.
2020-08-27 14:53:19 +00:00
```yaml
backup:
2022-03-03 00:05:33 +00:00
image: yavook/kiwi-backup:0.10
2020-08-27 14:53:19 +00:00
volumes:
2022-03-03 00:05:33 +00:00
- "${KIWI_INSTANCE}:/kiwi-backup/source:ro"
- "/var/local/kiwi.backup:/kiwi-backup/target"
2020-08-27 14:53:19 +00:00
```
- backups the entire service data directory
- stores all backup data on the host file system
2020-08-28 01:22:05 +00:00
- daily incremental backups at night
- a new full backup once every 3 months
- keeps backups up to 6 months old
- keeps daily backups for two recent sets (3-6 months)
2022-03-03 00:05:33 +00:00
- backup jobs run at a random minute past 2 am
2020-08-28 01:22:05 +00:00
Be aware though -- backups will use a fair bit of storage space!
2020-08-27 14:53:19 +00:00
## Customization
The kiwi-backup image allows for extensive customization even without creating a local image variant.
2022-03-03 00:05:33 +00:00
Schedules in environment variables are to be provided [in cron notation](https://crontab.guru/). Additionally, the special value "R" is supported and will be replaced by a random value.
### Time Zones
Being based on [`kiwi-cron`](https://github.com/yavook/kiwi-cron), `kiwi-backup` makes changing time zones easy. Just change the container environment variable `TZ` to your liking, e.g. "Europe/Berlin".
2020-08-27 14:53:19 +00:00
### Backup Scope
2022-03-03 00:05:33 +00:00
kiwi-backup will backup everything in its `/kiwi-backup/source` directory -- change the backup scope by adjusting what's mounted into that container directory.
2020-08-27 14:53:19 +00:00
```yaml
backup:
# ...
volumes:
2020-08-28 01:22:05 +00:00
# change scope here!
2022-03-03 00:05:33 +00:00
- "${KIWI_INSTANCE}:/kiwi-backup/source:ro"
2020-08-27 14:53:19 +00:00
```
2022-03-03 00:05:33 +00:00
You may of course create additional sources below the `/kiwi-backup/source` directory to limit the backup to specific projects or services. For added safety, mount your backup source(s) read-only by appending `:ro`.
You may also change the container environment variable `BACKUP_SOURCE`, though this is discouraged.
2020-08-27 14:53:19 +00:00
### Backup policy
These are the environment variables to change the basic backup policy.
```yaml
backup:
# ...
environment:
# ...
2020-08-27 16:06:27 +00:00
2020-08-27 14:53:19 +00:00
# when to run backups
2022-03-03 00:05:33 +00:00
# default: daily at a random minute past 2 am
SCHEDULE_BACKUP: "R 2 * * *"
2020-08-27 14:53:19 +00:00
2020-08-28 01:22:05 +00:00
# when to remove leftovers from failed transactions
2022-03-03 00:05:33 +00:00
# default: daily at a random minute past 4 am
SCHEDULE_CLEANUP: "R 4 * * *"
2020-08-27 14:53:19 +00:00
# how often to opt for a full backup
2020-08-28 01:22:05 +00:00
# default: every 3 months
FULL_BACKUP_FREQUENCY: "3M"
2020-08-27 14:53:19 +00:00
# how long to keep backups at all
2020-08-28 01:22:05 +00:00
# default: 6 months
BACKUP_RETENTION_TIME: "6M"
2020-08-27 14:53:19 +00:00
# how many full backup chains with incrementals to keep
2020-08-28 01:22:05 +00:00
# default: 2
KEEP_NUM_FULL_CHAINS: "2"
2020-08-27 14:53:19 +00:00
```
### Handling Secrets
`duplicity` usually handles secrets by [reading its environment](http://duplicity.nongnu.org/vers7/duplicity.1.html#sect6). Some of its backends also accept secrets via environment, [notably the AWS S3 backend](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html).
There are three major ways to for inject secrets into `kiwi-backup` environments:
#### Container environment
2022-03-03 00:05:33 +00:00
Just fire up your container using `docker run -e "FTP_PASSWORD=my_secret_here" yavook/kiwi-backup:0.10`
#### Image environment
Create a simple `Dockerfile` from following template.
```Dockerfile
2022-03-03 00:05:33 +00:00
FROM yavook/kiwi-backup:0.10
ENV FTP_PASSWORD="my_secret_here"
```
#### "Secrets" file in container
Create a shell script:
```sh
#!/bin/sh
export FTP_PASSWORD="my_secret_here"
```
Then, include that file as `/root/duplicity_secrets` into your container by building a custom `Dockerfile` or by mounting it as a (read-only) volume.
2020-08-27 14:53:19 +00:00
### Additional options
There's more environment variables for further customization. You'll likely know if you need to change these.
```yaml
backup:
# ...
environment:
# ...
# when to remove old full backup chains
2022-03-03 00:05:33 +00:00
# default: every Saturday at a random minute past 5 am
SCHEDULE_RMFULL: "R 5 * * SAT"
2020-08-27 14:53:19 +00:00
# when to remove old incremental backups
2022-03-03 00:05:33 +00:00
# default: every Sunday at a random minute past 5 am
SCHEDULE_RMINCR: "R 5 * * SUN"
2020-08-27 14:53:19 +00:00
# size of individual duplicity data volumes
2020-08-28 01:22:05 +00:00
# default: 1GiB
2020-08-27 14:53:19 +00:00
BACKUP_VOLSIZE: "1024"
2022-03-03 00:05:33 +00:00
# what to base backups on
# default: container directory "/kiwi-backup/source", usually mounted volume(s)
BACKUP_SOURCE: "/kiwi-backup/source"
2020-08-28 01:22:05 +00:00
# where to put backups
2022-03-03 00:05:33 +00:00
# default: container directory "/kiwi-backup/target", usually a mounted volume
BACKUP_TARGET: "file:///kiwi-backup/target"
2020-08-28 01:22:05 +00:00
# Additional options for all "duplicity" commands
OPTIONS_ALL: ""
2020-08-27 14:53:19 +00:00
# Additional options for "duplicity --full-if-older-than" command
OPTIONS_BACKUP: ""
# Additional options for "duplicity cleanup" command
OPTIONS_CLEANUP: ""
# Additional options for "duplicity remove-older-than" command
OPTIONS_RMFULL: ""
# Additional options for "duplicity remove-all-inc-of-but-n-full" command
OPTIONS_RMINCR: ""
2022-10-12 09:13:21 +00:00
# Webhook to be pinged on action (use "%%MSG%%" as a placeholder for a message)
WEBHOOK_URL: ""
2022-10-20 15:12:29 +00:00
# Webhook to be pinged on failed action (use "%%MSG%%" as a placeholder for a message)
WEBHOOK_FAIL_URL: ""
2022-10-12 09:13:21 +00:00
# Allow self-signed certificates on webhook target
WEBHOOK_INSECURE: "0"
2020-08-27 14:53:19 +00:00
```
## Encryption
2020-08-27 18:23:33 +00:00
For effective use of GnuPG encryption, you will need a GnuPG key and a custom image.
2020-08-27 16:06:27 +00:00
2020-08-27 18:23:33 +00:00
For simplicity, this guide assumes you have a `kiwi-scp` instance with some project where you want to put your backup service. You should have a shell opened in that project's directory.
2020-08-27 16:06:27 +00:00
2020-08-27 18:23:33 +00:00
### GnuPG Key Generation
2020-08-27 16:06:27 +00:00
2020-08-28 01:22:05 +00:00
> You'll usually want to generate a new key for each `kiwi-scp` instance.
> If you have reasons not to, skip this section.
2020-08-27 16:06:27 +00:00
2020-08-27 18:23:33 +00:00
Reasonable defaults for a backup encryption key are:
2020-08-27 16:06:27 +00:00
2020-08-27 18:23:33 +00:00
* User ID: `Administrator <root@my-hostname.com>`
* 4096 bit RSA
* Doesn't expire
* Secure passphrase (Don't bother memorizing it, you will save it in your `kiwi-scp` instance!)
2020-08-27 16:06:27 +00:00
2020-08-28 01:22:05 +00:00
To quickly generate a key, use the following command, then enter a passphrase:
2020-08-27 16:06:27 +00:00
2020-08-27 18:23:33 +00:00
```sh
2022-03-03 00:16:58 +00:00
docker run --rm -it -v "kiwi-backup.gnupg.tmp:/root/.gnupg" yavook/kiwi-backup:0.10 gpg --quick-gen-key --yes "Administrator <root@my-hostname.com>" rsa4096 encr never
2020-08-27 18:23:33 +00:00
```
2020-08-28 01:22:05 +00:00
To get a more in-depth generation wizard instead, use `gpg --full-gen-key` command without any more args and follow through.
2020-08-27 18:23:33 +00:00
2020-08-28 01:22:05 +00:00
### Export the generated key
2020-08-27 16:06:27 +00:00
2022-03-03 00:16:58 +00:00
This one-liner exports your generated key into a new subdirectory "kiwi-backup.gnupg":
2020-08-27 16:06:27 +00:00
```sh
2022-03-03 00:16:58 +00:00
docker run --rm -it -v "kiwi-backup.gnupg.tmp:/root/.gnupg" -v "$(pwd)/kiwi-backup.gnupg:/root/kiwi-backup.gnupg" -e "CURRENT_USER=$(id -u):$(id -g)" yavook/kiwi-backup:0.10 sh -c 'cd /root/kiwi-backup.gnupg && gpg --export-secret-keys --armor > secret.asc && gpg --export-ownertrust > ownertrust.txt && chown -R "${CURRENT_USER}" .'
2020-08-27 16:06:27 +00:00
```
2022-03-03 00:16:58 +00:00
You'll now find the "kiwi-backup.gnupg" subdirectory with files "secret.asc" and "ownertrust.txt" in it. Check your exported files:
2020-08-27 16:06:27 +00:00
```sh
2022-03-03 00:16:58 +00:00
docker run --rm -v "$(pwd)/kiwi-backup.gnupg:/root/kiwi-backup.gnupg:ro" yavook/kiwi-backup:0.10 sh -c 'cd /root/kiwi-backup.gnupg && gpg --import --batch secret.asc 2>/dev/null && gpg --import-ownertrust ownertrust.txt 2>/dev/null && gpg -k 2>/dev/null | grep -A1 "^pub" | xargs | tail -c17'
2020-08-27 16:06:27 +00:00
```
2022-03-03 00:16:58 +00:00
This should output your 16-digit Key-ID, so take note of it if you haven't already! Afterwards, run `docker volume rm kiwi-backup.gnupg.tmp` to get rid of the key generation volume.
2020-08-27 16:06:27 +00:00
2020-08-28 01:22:05 +00:00
### Using a pre-generated key
2020-08-27 18:23:33 +00:00
2020-08-28 01:22:05 +00:00
To use a pre-generated key, you'll need to export it manually instead. These are the commands:
2020-08-27 16:06:27 +00:00
```sh
2020-08-28 01:22:05 +00:00
gpg --export-secret-keys --armor [Key-ID] > backup/secret.asc
gpg --export-ownertrust > backup/ownertrust.txt
2020-08-27 16:06:27 +00:00
```
2020-08-28 01:22:05 +00:00
You can still check your exported files :)
2020-08-27 16:06:27 +00:00
```sh
2022-03-03 00:16:58 +00:00
docker run --rm -v "$(pwd)/kiwi-backup.gnupg:/root/kiwi-backup.gnupg:ro" yavook/kiwi-backup:0.10 sh -c 'cd /root/kiwi-backup.gnupg && gpg --import --batch secret.asc && gpg --import-ownertrust ownertrust.txt && gpg -k'
2020-08-27 16:06:27 +00:00
```
2020-08-27 18:23:33 +00:00
### Describe local kiwi-backup image
Now create a simple `Dockerfile` inside the "backup" directory from following template.
```Dockerfile
2022-03-03 00:05:33 +00:00
FROM yavook/kiwi-backup:0.10
2020-08-27 18:23:33 +00:00
COPY secret.asc ownertrust.txt /root/
2020-08-28 01:22:05 +00:00
RUN gpg --import --batch /root/secret.asc; \
2020-08-27 18:23:33 +00:00
gpg --import-ownertrust /root/ownertrust.txt; \
rm /root/secret.asc /root/ownertrust.txt
2020-08-28 01:22:05 +00:00
# fill in these values to match your data
ENV GPG_KEY_ID="changeme" \
2020-08-27 18:23:33 +00:00
GPG_PASSPHRASE="changeme"
```
2020-08-28 01:22:05 +00:00
If applicable, commit the "backup" directory into the `kiwi-scp` instance repository.
2020-08-27 18:23:33 +00:00
### Use local image
2020-08-28 01:22:05 +00:00
All that's left is to come back to your project's `docker-compose.yml`, where you shorten one line. Change:
2020-08-27 18:23:33 +00:00
```yaml
backup:
2022-03-03 00:05:33 +00:00
image: yavook/kiwi-backup:0.10
2020-08-28 01:22:05 +00:00
# ...
2020-08-27 18:23:33 +00:00
```
2020-08-28 01:22:05 +00:00
Into:
2020-08-27 18:23:33 +00:00
```yaml
backup:
build: ./backup
2020-08-28 01:22:05 +00:00
# ...
2020-08-27 16:06:27 +00:00
```
2020-08-28 01:22:05 +00:00
That's it -- from now on, all new backups will be encrypted!
2020-08-27 14:53:19 +00:00
## Offsite Backups
2022-03-03 00:05:33 +00:00
TODO