Skip to content

Commit

Permalink
feat(docker): ✨ Allow mounting an authorized_keys file directly inste…
Browse files Browse the repository at this point in the history
…ad of using the environment variable.

As an alternative to the ENV-Var the Docker image now supports the mounting of an authorized_key file. Note that setting the environment variable WILL overwrite any mounted file.
  • Loading branch information
AnotherStranger committed May 5, 2023
1 parent 542d33c commit b4a446d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ LABEL org.opencontainers.image.title="borg-server"
ENV BORG_SERVE_ADDITIONAL_ARGS=""
ENV BORG_UID=""
ENV BORG_GID=""
ENV BORG_AUTHORIZED_KEYS=""

RUN set -x && \
apk add --no-cache \
Expand Down Expand Up @@ -112,7 +111,7 @@ RUN set -x \
&& mkdir -p /var/lib/docker-borg/ssh \
&& mkdir -p /home/borg/backups

VOLUME ["/home/borg/backups/", "/var/lib/docker-borg"]
VOLUME ["/home/borg/backups/", "/var/lib/docker-borg", "/home/borg/.ssh/authorized_keys"]

COPY ./entrypoint.sh /

Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ services:
volumes:
- backup:/home/borg/backups # You can find your backups inside this volume
- server_keys:/var/lib/docker-borg # This volume is used to persist the hosts ssh-keys across updates
# - <path to authorized_keys file>:/home/borg/.ssh/authorized_keys <- Alternative to BORG_AUTHORIZED_KEYS
ports:
- "8022:22"

Expand All @@ -47,15 +48,24 @@ volumes:
```
<!-- markdownlint-enable -->
### Volumes
<!-- markdownlint-disable -->
| Path | Description |
| --------------------------------- | ---------------------------------------------------------------------------------------- |
| `/home/borg/backups` | All backups will be in this volume |
| `/var/lib/docker-borg` | This volume persists the hosts ssh-keys across updates |
| `/home/borg/.ssh/authorized_keys` | As an alternative to the variable `BORG_AUTHORIZED_KEYS` you can mount the file directly |
<!-- markdownlint-enable -->
### Environment variables

<!-- markdownlint-disable -->
| Variable | Description | Example |
|-----------------------------|----------------------------------------|------------------------|
| `BORG_AUTHORIZED_KEYS` | Public ssh keys for backups. Required. | `<key-one>\n<key-two>` |
| `BORG_UID` | UID for the backup user. | `1000` |
| `BORG_GID` | GID for the backup user. | `1000` |
| `BORG_SERVE_ADDITIONAL_ARGS`| Additional CMD args to borg serve | `--append-only` |
| Variable | Description | Example |
| ---------------------------- | -------------------------------------- | ---------------------- |
| `BORG_AUTHORIZED_KEYS` | Public ssh keys for backups. Required. | `<key-one>\n<key-two>` |
| `BORG_UID` | UID for the backup user. | `1000` |
| `BORG_GID` | GID for the backup user. | `1000` |
| `BORG_SERVE_ADDITIONAL_ARGS` | Additional CMD args to borg serve | `--append-only` |
<!-- markdownlint-enable -->

### Important Notes
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
volumes:
- backup:/home/borg/backups
- server_keys:/var/lib/docker-borg
# - <path to authorized_keys file>:/home/borg/.ssh/authorized_keys <- Alternative to BORG_AUTHORIZED_KEYS
ports:
- "8022:22"

Expand Down
19 changes: 10 additions & 9 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ if [ -n "${BORG_GID}" ]; then
usermod -g "${BORG_GID}" borg > /dev/null
fi

# if BORG_AUTHORIZED_KEYS is set substitute authorized_keys file
if [ -n "${BORG_AUTHORIZED_KEYS+x}" ]; then
echo -e "${BORG_AUTHORIZED_KEYS}" | sed -e "s/^/command=\"borg serve ${BORG_SERVE_ADDITIONAL_ARGS} --restrict-to-path \/home\/borg\/backups\" /" >/home/borg/.ssh/authorized_keys
chown borg:borg /home/borg/.ssh/authorized_keys
chmod og-rwx /home/borg/.ssh/authorized_keys

echo "################################################################################"
echo "# PRINTING THE CONTENTS OF /HOME/BORG/.SSH/AUTHORIZED_KEYS: #"
echo "################################################################################"
cat /home/borg/.ssh/authorized_keys
echo "end of /home/borg/.ssh/authorized_keys"
echo ""
fi
chown borg:borg /home/borg/.ssh/authorized_keys
chmod og-rwx /home/borg/.ssh/authorized_keys

echo "################################################################################"
echo "# PRINTING THE CONTENTS OF /HOME/BORG/.SSH/AUTHORIZED_KEYS: #"
echo "################################################################################"
cat /home/borg/.ssh/authorized_keys
echo "end of /home/borg/.ssh/authorized_keys"
echo ""

chown -R borg:borg /home/borg
chown -R borg:borg /home/borg/.ssh
Expand Down

0 comments on commit b4a446d

Please sign in to comment.