|
| 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 | + |
0 commit comments