Skip to content

Commit 2bfc9c0

Browse files
committed
Add GH action to publish a dockerfile of the forums
1 parent 01f4e04 commit 2bfc9c0

9 files changed

Lines changed: 474 additions & 218 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Prompt: Ansible Role - MongoDB Backup via rsync
2+
3+
## Task
4+
5+
Create an Ansible role called `forums_backup` that sets up a daily MongoDB backup job on a
6+
Linux server running MongoDB inside Docker Compose. The backup is compressed and rsync'd to
7+
a separate server over SSH.
8+
9+
---
10+
11+
## Context
12+
13+
- MongoDB runs as a Docker Compose service named `mongodb`.
14+
- The compose file is at `/srv/triplea-forums/docker-compose.yml`.
15+
- The database name is `nodebb`.
16+
- The dumped output is NOT pre-compressed; the script must compress it with gzip.
17+
- The backup is transferred to a remote server via rsync over SSH.
18+
- A dedicated SSH key (no passphrase) is used for the rsync connection.
19+
The key is placed on the host manually - Ansible must NOT manage the private key file.
20+
- Old backups on the remote must be pruned to avoid unbounded disk growth.
21+
22+
---
23+
24+
## Role Structure to Create
25+
26+
```
27+
roles/forums_backup/
28+
defaults/main.yml # all tuneable variables with sensible defaults
29+
tasks/main.yml # deploys the script and installs the cron job
30+
templates/backup.sh.j2 # the backup shell script (Jinja2 template)
31+
```
32+
33+
---
34+
35+
## Variables (defaults/main.yml)
36+
37+
| Variable | Default | Description |
38+
|---|---|---|
39+
| `backup_remote_user` | `"backups"` | SSH user on the remote server |
40+
| `backup_remote_host` | `""` | Hostname/IP of the remote server (required - no default) |
41+
| `backup_remote_path` | `"/backups/forums"` | Directory on the remote server to store backups |
42+
| `backup_ssh_key_path` | `"/root/.ssh/id_backup"` | Path to the SSH private key on the forums server |
43+
| `backup_retention_days` | `14` | Days of backup files to keep on the remote |
44+
| `backup_tmp_dir` | `"/tmp"` | Local temp directory used during dump + transfer |
45+
| `backup_cron_hour` | `"3"` | Hour for the cron job (24h) |
46+
| `backup_cron_minute` | `"0"` | Minute for the cron job |
47+
48+
---
49+
50+
## Backup Script Logic (templates/backup.sh.j2)
51+
52+
The script must:
53+
54+
1. Use `set -euo pipefail` so any failure stops the script immediately.
55+
2. Build a timestamped filename like `nodebb-YYYYMMDD-HHMMSS.gz`.
56+
3. Run mongodump via `docker compose exec -T` piped through `gzip` into a temp file:
57+
```bash
58+
docker compose -f /srv/triplea-forums/docker-compose.yml exec -T mongodb \
59+
mongodump --db nodebb --archive | gzip > "$BACKUP_FILE"
60+
```
61+
4. rsync the file to the remote using the dedicated SSH key:
62+
```bash
63+
rsync -az -e "ssh -i {{ backup_ssh_key_path }} -o StrictHostKeyChecking=no" \
64+
"$BACKUP_FILE" \
65+
{{ backup_remote_user }}@{{ backup_remote_host }}:{{ backup_remote_path }}/
66+
```
67+
5. Delete the local temp file after successful rsync.
68+
6. Prune files on the remote older than `backup_retention_days` days:
69+
```bash
70+
ssh -i {{ backup_ssh_key_path }} -o StrictHostKeyChecking=no \
71+
{{ backup_remote_user }}@{{ backup_remote_host }} \
72+
"find {{ backup_remote_path }}/ -name 'nodebb-*.gz' -mtime +{{ backup_retention_days }} -delete"
73+
```
74+
7. Log each major step to stdout with a timestamp prefix (the cron daemon captures this).
75+
76+
---
77+
78+
## Ansible Tasks (tasks/main.yml)
79+
80+
1. Ensure `/usr/local/bin` exists (it usually does, but be explicit).
81+
2. Template `backup.sh.j2` to `/usr/local/bin/forums-backup.sh` with mode `0750`, owner `root`.
82+
3. Install a cron job running as root:
83+
- Command: `/usr/local/bin/forums-backup.sh >> /var/log/forums-backup.log 2>&1`
84+
- Schedule: `backup_cron_hour` / `backup_cron_minute` daily.
85+
- Name the cron entry `forums-mongodb-backup` so Ansible can manage it idempotently.
86+
87+
---
88+
89+
## playbook.yml Integration
90+
91+
Add the role to the existing play:
92+
93+
```yaml
94+
- hosts: forums
95+
tags: forums
96+
roles:
97+
- nginx_forums_conf
98+
- role: forums_backup
99+
vars:
100+
backup_remote_host: "your-backup-server.example.com"
101+
```
102+
103+
The `backup_remote_host` must be overridden - either in the inventory file, in a group_vars
104+
file, or with `--extra-vars` at run time. The role should fail with a clear error if it is
105+
empty.
106+
107+
Add a task at the top of `tasks/main.yml` to assert `backup_remote_host != ""`:
108+
```yaml
109+
- name: check backup_remote_host is set
110+
assert:
111+
that: backup_remote_host | length > 0
112+
fail_msg: "backup_remote_host must be set (hostname or IP of the rsync destination)"
113+
```
114+
115+
---
116+
117+
## Manual Setup Required on the Remote Server (document in a README)
118+
119+
These steps are NOT automated by Ansible (they are done once by an operator):
120+
121+
```bash
122+
# On the remote server:
123+
useradd -m -s /bin/bash backups
124+
mkdir -p /backups/forums
125+
chown backups:backups /backups/forums
126+
127+
# Authorize the forums server's public key
128+
mkdir -p /home/backups/.ssh
129+
echo "<contents of /root/.ssh/id_backup.pub from the forums server>" \
130+
>> /home/backups/.ssh/authorized_keys
131+
chmod 700 /home/backups/.ssh
132+
chmod 600 /home/backups/.ssh/authorized_keys
133+
chown -R backups:backups /home/backups/.ssh
134+
```
135+
136+
---
137+
138+
## Testing the Script Manually
139+
140+
After Ansible deploys the script:
141+
142+
```bash
143+
# Run once by hand to verify it works end-to-end
144+
sudo /usr/local/bin/forums-backup.sh
145+
146+
# Check what landed on the remote
147+
ssh -i /root/.ssh/id_backup backups@<remote-host> \
148+
"ls -lh /backups/forums/"
149+
```
150+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Publish Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}/nodebb
11+
12+
jobs:
13+
build-and-push:
14+
name: Build and push Docker image
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Log in to GitHub Container Registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ${{ env.REGISTRY }}
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Extract Docker metadata
32+
id: meta
33+
uses: docker/metadata-action@v5
34+
with:
35+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
36+
tags: |
37+
type=raw,value=latest
38+
type=sha,prefix=,format=short
39+
40+
- name: Build and push Docker image
41+
uses: docker/build-push-action@v6
42+
with:
43+
context: ./node-bb
44+
file: ./node-bb/Dockerfile
45+
push: true
46+
tags: ${{ steps.meta.outputs.tags }}
47+
labels: ${{ steps.meta.outputs.labels }}
48+

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
.docs/
3+

‎docker-compose.yml‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
services:
2+
mongodb:
3+
image: mongo:8.0.21
4+
restart: unless-stopped
5+
volumes:
6+
- mongodb_data:/data/db
7+
networks:
8+
- forums_internal
9+
10+
nodebb:
11+
build: ./node-bb
12+
restart: unless-stopped
13+
user: "1001:1001"
14+
ports:
15+
# Bind only to localhost so nginx (running on the host) can proxy to
16+
# port 4567, but the port is not reachable from outside the server.
17+
- "127.0.0.1:4567:4567"
18+
volumes:
19+
- /opt/triplea-forums/config.json:/opt/config/config.json:ro
20+
- /opt/triplea-forums/uploads:/usr/src/app/public/uploads
21+
depends_on:
22+
- mongodb
23+
networks:
24+
- forums_internal
25+
26+
volumes:
27+
mongodb_data:
28+
29+
networks:
30+
forums_internal:
31+

‎node-bb/Dockerfile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM nodebb/docker:v1.13.3
2-
1+
FROM ghcr.io/nodebb/nodebb:4.10.3
2+
33
WORKDIR /usr/src/app
44

55
ARG NODE_ENV

‎node-bb/README.md‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ The software that powers the TripleA Forums is [NodeBB](https://github.com/NodeB
44
This document is instructions on how to build and run the Docker container of the forums using an
55
[official Docker container of NodeBB](https://hub.docker.com/r/nodebb/docker) as the base image.
66

7+
## Docker Compose
8+
9+
The recommended way to run the forums in production. From the project root:
10+
11+
```shell
12+
# First-time setup: create host directories and drop in a config.json
13+
mkdir -p /opt/triplea-forums/uploads/{category,emoji,files,profile,sounds,system}
14+
cp node-bb/example/config.json /opt/triplea-forums/config.json
15+
# Edit config.json: set a random secret and verify the mongo connection details
16+
17+
# Build and start (MongoDB + NodeBB)
18+
docker compose up -d --build
19+
20+
# View logs
21+
docker compose logs -f nodebb
22+
23+
# Stop everything
24+
docker compose down
25+
26+
# Upgrade NodeBB: update the FROM version in node-bb/Dockerfile, then
27+
docker compose up -d --build
28+
```
29+
30+
NodeBB listens on `127.0.0.1:4567` and is proxied by nginx running on the host.
31+
MongoDB is on an internal Docker network and not reachable from outside the server.
32+
33+
---
34+
735
## Building
836

937
1. Build the Docker image using the command ```docker build -t triplea-forums:latest .```
@@ -65,7 +93,7 @@ the list of Docker image versions on [https://hub.docker.com/_/mongo](https://hu
6593

6694
This command will start the Docker container:
6795
```shell script
68-
docker run --rm --name mongodb -p 27017:27017 -d mongo:4.4
96+
docker run --rm --name mongodb -p 27017:27017 -d mongo:8.0.21
6997
```
7098

7199
To stop the container, run ```docker stop mongodb```
@@ -83,7 +111,7 @@ purposes if you mount a volume directory for storing the data. The below command
83111
This command will start the Docker container:
84112

85113
```shell script
86-
docker run --rm --name mongodb -p 27017:27017 -v /opt/triple-forums/datadir:/data/db -d mongo:4.4
114+
docker run --rm --name mongodb -p 27017:27017 -v /opt/triple-forums/datadir:/data/db -d mongo:8.0.21
87115
```
88116

89117
To stop the container, run ```docker stop mongodb```

‎node-bb/example/config.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"url": "http://0.0.0.0:8080",
3-
"secret": "6c5e0d10-ea73-4835-bf2b-51979001fbd8",
2+
"url": "https://forums.triplea-game.org",
3+
"secret": "replace-with-a-random-secret",
44
"database": "mongo",
5-
"port": "8080",
5+
"port": "4567",
66
"mongo": {
7-
"host": "192.168.1.231",
7+
"host": "mongodb",
88
"port": "27017",
99
"username": "",
1010
"password": "",

‎node-bb/install/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The install/package.json is largely mirroring the package.json
2+
from nobeBB: https://github.com/NodeBB/NodeBB/blob/master/install/package.json

0 commit comments

Comments
 (0)