Skip to content

Commit

Permalink
Merge pull request #349 from Progress1/postgres_16
Browse files Browse the repository at this point in the history
Upgrade database to Postgres v16 (breaking change)
  • Loading branch information
Progress1 authored Sep 12, 2024
2 parents ea7b0c6 + 566e171 commit 770b6cf
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HTTPS_PROXY=

# Versions
REDIS_TAG=6-alpine
POSTGRES_TAG=13-alpine
POSTGRES_TAG=16-alpine
TARANIS_NG_TAG=v23.12.1

# Timezone for all containers
Expand Down
152 changes: 152 additions & 0 deletions docker/MIGRATE_DB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Database migration from Postgres version 13 to 16

Unfortunately, this process can't be fully automated, therefore manual work is required. With the new update, a new database container `taranis-ng-postgres-1` is created, and you only need to transfer data from the old container `taranis-ng-database-1` to the new one. This approach keeps your old database container intact if the migration fails, allowing you to revert the change if necessary. If the migration is successful, you can delete the old container.

## Database update
Edit your `.env` file and change variable `POSTGRES_TAG` from `13-alpine` to `16-alpine`:
```
POSTGRES_TAG=16-alpine
```

Update (pulling images or building from Git):

```
docker compose -f docker/docker-compose.yml pull
```
or on Windows:
```
docker compose -f docker\docker-compose.yml build
```

Run containers with:

```
docker compose -f docker/docker-compose.yml up --no-build -d
```
or on Windows:
```
docker compose -f docker\docker-compose.yml up --no-build -d
```

The new `taranis-ng-postgres-1` container has been created.

## Migration

### 1. Backup database
Get into `taranis-ng-database-1` container shell:

```
docker exec -it taranis-ng-database-1 /bin/sh
```

Run inside the `taranis-ng-database-1` container to create a backup:
```
pg_dump -U taranis-ng -d taranis-ng -Z 9 --clean > db_bak.gz
exit
```

The backup file has been created in the container and you should be back in the host shell.

### 2. Copy backup to local machine
Now let's copy the backup file from the container to the host. The backup file `db_bak.gz` will appear in the current directory:

```
docker cp taranis-ng-database-1:/db_bak.gz db_bak.gz
```

### 3. Copy backup to the new `taranis-ng-postgres-1` container
The backup file must be in the current directory:

```
docker cp db_bak.gz taranis-ng-postgres-1:/db_bak.gz
```

Now the backup is inside the new container.

### 4. Restore Data
Stop the `taranis-ng-core-1` container:

```
docker stop taranis-ng-core-1
```

Get inside the new `taranis-ng-postgres-1` container:

```
docker exec -it taranis-ng-postgres-1 /bin/sh
```

Now let's restore the backup. The backup file must be in the current directory:

```
zcat db_bak.gz | psql -U taranis-ng -d taranis-ng
exit
```

Start the `taranis-ng-core-1` container:

```
docker start taranis-ng-core-1
```

If there are no errors in the output log, you can stop the old `taranis-ng-database-1` container:

```
docker stop taranis-ng-database-1
```

Check if everything runs correctly. After some time, you can delete the old `taranis-ng-database-1` container.

If there are any issues, you can revert and use the old `taranis-ng-database-1` container by mapping it back in `docker-compose.yml` (see below for configuration changes).

## Configuration Changes

### Before (v13)

File `.env` modified line:

```
POSTGRES_TAG=13-alpine
```

File `docker-compose.yml` modified lines:

```
database:
volumes:
- "database_data:/var/lib/postgresql/data"
core:
depends_on:
- "database"
environment:
DB_URL: "database"
volumes:
database:
```

### After (v16)

File `.env` modified line:

```
POSTGRES_TAG=16-alpine
```

File `docker-compose.yml` modified lines:

```
postgres:
volumes:
- "postgres_data:/var/lib/postgresql/data"
core:
depends_on:
- "postgres"
environment:
DB_URL: "postgres"
volumes:
postgres:
```
11 changes: 6 additions & 5 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ services:
max-size: "200k"
max-file: "10"

database:
postgres:
image: "postgres:${POSTGRES_TAG}"
restart: unless-stopped
environment:
POSTGRES_DB: "taranis-ng"
POSTGRES_USER: "taranis-ng"
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
TZ: "${TZ}"
PGTZ: "${TZ}"
command: ["postgres", "-c", "shared_buffers=${DB_SHARED_BUFFERS}", "-c", "max_connections=${DB_MAX_CONNECTIONS}"]
volumes:
- "database_data:/var/lib/postgresql/data"
- "postgres_data:/var/lib/postgresql/data"
logging:
driver: "json-file"
options:
Expand All @@ -34,7 +35,7 @@ services:
core:
depends_on:
- "redis"
- "database"
- "postgres"
restart: unless-stopped
image: "skcert/taranis-ng-core:${TARANIS_NG_TAG}"
build:
Expand All @@ -47,7 +48,7 @@ services:
https_proxy: "${HTTPS_PROXY}"
environment:
REDIS_URL: "redis://redis"
DB_URL: "database"
DB_URL: "postgres"
DB_DATABASE: "taranis-ng"
DB_USER: "taranis-ng"
DB_POOL_SIZE: 100
Expand Down Expand Up @@ -285,7 +286,7 @@ secrets:

volumes:
redis_conf:
database_data:
postgres_data:
core_data:
presenters_templates:
collector_storage:

0 comments on commit 770b6cf

Please sign in to comment.