Skip to content

Commit

Permalink
Modularize backup providers and add restic as a backup provider (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prof-Bloodstone authored and itzg committed Aug 5, 2019
1 parent 4dbd30c commit ce2ea13
Show file tree
Hide file tree
Showing 12 changed files with 609 additions and 261 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto

*.sh text eol=lf
60 changes: 21 additions & 39 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@ jobs:
include:
- stage: Linting and sanity tests
name: Lint script with shellcheck
script:
- "readarray -t shell_scripts < <(find * -name '*.sh' -a ! -path '*/.git/*')"
- echo checking "${shell_scripts[@]}"
- shellcheck "${shell_scripts[@]}"
script: ./tests/lint.shellcheck.sh
- name: Lint dockerfile
# DL3006 Always tag the version of an image explicitly
# DL3018 Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`
script: >-
docker run --rm -i hadolint/hadolint hadolint
--ignore DL3018
--ignore DL3006
- < Dockerfile
- name: Test simple backup and exclusion scenario
script: bash ./tests/lint.hadolint.sh
- name: Test simple backup and exclusion scenario with tar
before_install:
- sudo apt-get update
- sudo apt-get install -y
tree
coreutils
env:
SRC_DIR: /tmp/source
DEST_DIR: /tmp/dest
Expand All @@ -31,30 +23,20 @@ jobs:
EXCLUDES: '*.jar,exclude_dir'
RCON_PATH: /usr/bin/rcon-cli
PRUNE_BACKUPS_DAYS: 3
script:
- mkdir -p "${SRC_DIR}/"{in,ex}clude_dir "${DEST_DIR}" "${EXTRACT_DIR}"
- touch "${SRC_DIR}/"{backup_me.{1,2}.json,exclude_me.jar}
- touch "${SRC_DIR}/include_dir/"{backup_me.{1,2}.json,exclude_me.jar}
- touch "${SRC_DIR}/exclude_dir/"exclude_me.{1,2}.{json,jar}
- tree "${SRC_DIR}"
- touch -d "$(( PRUNE_BACKUPS_DAYS + 2 )) days ago" "${DEST_DIR}/fake_backup_that_should_be_deleted.tgz"
- ls -al "${DEST_DIR}"
- docker build -t testimg .
- echo -e '#!/bin/bash\ntrue' > rcon-cli && chmod +x rcon-cli
- timeout 50 docker run --rm
--env SRC_DIR
--env DEST_DIR
--env BACKUP_INTERVAL
--env INITIAL_DELAY
--env EXCLUDES
--env PRUNE_BACKUPS_DAYS
--mount "type=bind,src=${SRC_DIR},dst=${SRC_DIR}"
--mount "type=bind,src=${DEST_DIR},dst=${DEST_DIR}"
--mount "type=bind,src=$(pwd)/rcon-cli,dst=${RCON_PATH}"
testimg
- tree "${DEST_DIR}"
- tar -xzf "${DEST_DIR}/"*.tgz -C "${EXTRACT_DIR}"
- tree "${EXTRACT_DIR}"
- '[ -z "$(find "${EXTRACT_DIR}" -name "exclude_*" -print -quit)" ]'
- '[ 4 -eq "$(find "${EXTRACT_DIR}" -name "backup_me*" -print | wc -l)" ]'

script: bash ./tests/test.simple.tar.sh
- name: Test restic handling
before_install:
- sudo apt-get update
- sudo apt-get install -y
tree
coreutils
env:
SRC_DIR: /tmp/source
DEST_DIR: /tmp/dest
RESTIC_REPOSITORY: /tmp/dest
BACKUP_INTERVAL: 0
INITIAL_DELAY: 0s
EXCLUDES: '*.jar,exclude_dir'
RCON_PATH: /usr/bin/rcon-cli
RESTIC_PASSWORD: 1234
script: bash ./tests/test.simple.restic.sh
85 changes: 56 additions & 29 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
FROM alpine AS builder

ARG RCON_CLI_VERSION=1.4.4

ADD https://github.com/itzg/rcon-cli/releases/download/${RCON_CLI_VERSION}/rcon-cli_${RCON_CLI_VERSION}_linux_amd64.tar.gz /tmp/rcon-cli.tgz

RUN mkdir -p /opt/rcon-cli && \
tar x -f /tmp/rcon-cli.tgz -C /opt/rcon-cli && \
rm /tmp/rcon-cli.tgz



FROM alpine

RUN apk -U --no-cache add \
bash \
coreutils \
unzip

COPY --from=builder /opt/rcon-cli/rcon-cli /opt/rcon-cli/rcon-cli

RUN ln -s /opt/rcon-cli/rcon-cli /usr/bin

ENTRYPOINT ["/opt/backup-loop.sh"]

VOLUME ["/data", "/backups"]

COPY backup-loop.sh /opt/

FROM alpine AS builder

ARG IMAGE_ARCH=amd64

ARG RCON_CLI_VERSION=1.4.4

ADD https://github.com/itzg/rcon-cli/releases/download/${RCON_CLI_VERSION}/rcon-cli_${RCON_CLI_VERSION}_linux_${IMAGE_ARCH}.tar.gz /tmp/rcon-cli.tar.gz

RUN mkdir -p /opt/rcon-cli && \
tar x -f /tmp/rcon-cli.tar.gz -C /opt/rcon-cli && \
rm /tmp/rcon-cli.tar.gz

ARG RESTIC_VERSION=0.9.5

ADD https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_${IMAGE_ARCH}.bz2 /tmp/restic.bz2

RUN bunzip2 /tmp/restic.bz2 && \
mv /tmp/restic /opt/restic

ARG DEMOTER_VERSION=0.1.0

ADD https://github.com/itzg/entrypoint-demoter/releases/download/${DEMOTER_VERSION}/entrypoint-demoter_${DEMOTER_VERSION}_linux_${IMAGE_ARCH}.tar.gz /tmp/entrypoint-demoter.tar.gz

RUN mkdir -p /opt/entrypoint-demoter && \
tar x -f /tmp/entrypoint-demoter.tar.gz -C /opt/entrypoint-demoter && \
rm /tmp/entrypoint-demoter.tar.gz



FROM alpine

RUN apk -U --no-cache add \
bash \
coreutils

COPY --from=builder /opt/rcon-cli/rcon-cli /opt/rcon-cli/rcon-cli

RUN ln -s /opt/rcon-cli/rcon-cli /usr/bin

COPY --from=builder /opt/restic /opt/restic

RUN chmod +x /opt/restic && \
ln -s /opt/restic /usr/bin

COPY --from=builder /opt/entrypoint-demoter/entrypoint-demoter /opt/entrypoint-demoter

RUN chmod +x /opt/entrypoint-demoter && \
ln -s /opt/entrypoint-demoter /usr/bin

COPY backup-loop.sh /opt/

VOLUME ["/data", "/backups"]

ENTRYPOINT ["/opt/entrypoint-demoter", "--match", "/backups"]

CMD ["/opt/backup-loop.sh"]
40 changes: 20 additions & 20 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License
Copyright (c) 2019 Geoff Bourne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
MIT License

Copyright (c) 2019 Geoff Bourne

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
162 changes: 90 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,90 @@
[![Docker Pulls](https://img.shields.io/docker/pulls/itzg/mc-backup.svg)](https://hub.docker.com/r/itzg/mc-backup)
[![Build Status](https://travis-ci.org/itzg/docker-mc-backup.svg?branch=master)](https://travis-ci.org/itzg/docker-mc-backup)

Provides a side-car container to backup itzg/minecraft-server world data.

## Environment variables

- `SRC_DIR`=/data
- `DEST_DIR`=/backups
- `BACKUP_NAME`=world
- `INITIAL_DELAY`=2m
- `BACKUP_INTERVAL`=24h
- `PRUNE_BACKUPS_DAYS`=7
- `RCON_PORT`=25575
- `RCON_PASSWORD`=minecraft
- `EXCLUDES`=\*.jar,cache,logs
- `LINK_LATEST`=false

If `PRUNE_BACKUP_DAYS` is set to a positive number, it'll delete old `.tgz` backup files from `DEST_DIR`. By default deletes backups older than a week.

If `BACKUP_INTERVAL` is set to 0 or smaller, script will run once and exit.

Both `INITIAL_DELAY` and `BACKUP_INTERVAL` accept times in `sleep` format: `NUMBER[SUFFIX] NUMBER[SUFFIX] ...`.
SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.

Examples:
- `BACKUP_INTERVAL`="1.5d" -> backup every one and a half days (36 hours)
- `BACKUP_INTERVAL`="2h 30m" -> backup every two and a half hours
- `INITIAL_DELAY`="120" -> wait 2 minutes before starting

`EXCLUDES` is a comma-separated list of glob(3) patterns to exclude from backups. By default excludes all jar files (plugins, server files), logs folder and cache (used by i.e. PaperMC server).

`LINK_LATEST` is a true/false flag that creates a symbolic link to the latest backup.

## Volumes

- `/data` :
Should be attached read-only to the same volume as the `/data` of the `itzg/minecraft-server` container
- `/backups` :
The volume where incremental tgz files will be created.

## Example

An example StatefulSet deployment is provided [in this repository](test-deploy.yaml).

The important part is the containers definition of the deployment:

```yaml
containers:
- name: mc
image: itzg/minecraft-server
env:
- name: EULA
value: "TRUE"
volumeMounts:
- mountPath: /data
name: data
- name: backup
image: mc-backup
imagePullPolicy: Never
securityContext:
runAsUser: 1000
env:
- name: BACKUP_INTERVAL
value: "2h 30m"
volumeMounts:
- mountPath: /data
name: data
readOnly: true
- mountPath: /backups
name: backups
```
[![Docker Pulls](https://img.shields.io/docker/pulls/itzg/mc-backup.svg)](https://hub.docker.com/r/itzg/mc-backup)
[![Build Status](https://travis-ci.org/itzg/docker-mc-backup.svg?branch=master)](https://travis-ci.org/itzg/docker-mc-backup)

Provides a side-car container to backup itzg/minecraft-server world data.

## Environment variables

##### Common variables:

- `SRC_DIR`=/data
- `BACKUP_NAME`=world
- `INITIAL_DELAY`=2m
- `BACKUP_INTERVAL`=24h
- `PRUNE_BACKUPS_DAYS`=7
- `RCON_PORT`=25575
- `RCON_PASSWORD`=minecraft
- `EXCLUDES`=\*.jar,cache,logs
- `BACKUP_METHOD`=tar

If `PRUNE_BACKUP_DAYS` is set to a positive number, it'll delete old `.tgz` backup files from `DEST_DIR`. By default deletes backups older than a week.

If `BACKUP_INTERVAL` is set to 0 or smaller, script will run once and exit.

Both `INITIAL_DELAY` and `BACKUP_INTERVAL` accept times in `sleep` format: `NUMBER[SUFFIX] NUMBER[SUFFIX] ...`.
SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.

Examples:
- `BACKUP_INTERVAL`="1.5d" -> backup every one and a half days (36 hours)
- `BACKUP_INTERVAL`="2h 30m" -> backup every two and a half hours
- `INITIAL_DELAY`="120" -> wait 2 minutes before starting

`EXCLUDES` is a comma-separated list of glob(3) patterns to exclude from backups. By default excludes all jar files (plugins, server files), logs folder and cache (used by i.e. PaperMC server).

##### `tar` backup method

- `DEST_DIR`=/backups
- `LINK_LATEST`=false

`LINK_LATEST` is a true/false flag that creates a symbolic link to the latest backup.

##### `restic` backup method

See [restic documentation](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html) on what variables are needed to be defined.
At least one of `RESTIC_PASSWORD*` variables need to be defined, along with `RESTIC_REPOSITORY`.


:warning: | When using restic as your backup method, make sure that you fix your container hostname to a constant value! Otherwise, each time a container restarts it'll use a different, random hostname which will cause it not to rotate your backups created by previous instances!
---|---

:warning: | When using restic, at least one of `HOSTNAME` or `BACKUP_NAME` must be unique, when sharing a repository. Otherwise other instances using the same repository might prune your backups prematurely.
---|---

## Volumes

- `/data` :
Should be attached read-only to the same volume as the `/data` of the `itzg/minecraft-server` container
- `/backups` :
The volume where incremental tgz files will be created, if using tar backup method.

## Example

An example StatefulSet deployment is provided [in this repository](test-deploy.yaml).

The important part is the containers definition of the deployment:

```yaml
containers:
- name: mc
image: itzg/minecraft-server
env:
- name: EULA
value: "TRUE"
volumeMounts:
- mountPath: /data
name: data
- name: backup
image: mc-backup
imagePullPolicy: Never
securityContext:
runAsUser: 1000
env:
- name: BACKUP_INTERVAL
value: "2h 30m"
volumeMounts:
- mountPath: /data
name: data
readOnly: true
- mountPath: /backups
name: backups
```
Loading

0 comments on commit ce2ea13

Please sign in to comment.