---
title: Database Management
product: trilio-site-recovery-for-openstack
doc_type: guide
version: master
source: git2docs (code-derived, validation-filtered)
canonical: https://git2docs.com/mark-reid/docs/trilio-site-recovery-for-openstack/trilio-site-recovery-for-openstack/database-management
---

# Database Management

_db sync / migrations and maintenance_

## Overview

This page covers database setup, schema migration, and ongoing maintenance for Trilio Site Recovery. The service stores all DR state — sites, protection groups, consistency groups, members, volumes, operations, replication policies, and tenant mappings — in a MariaDB or MySQL database named `protector`. Schema evolution is managed by Alembic migrations, invoked through the `protector-manage db sync` command. Understanding how migrations run, how to verify schema state, and how to recover from common failure modes is essential before upgrading the service or diagnosing a failed deployment.

## Prerequisites

Before performing any database management task, ensure:

- **MariaDB or MySQL** is running and accessible from the node running the protector service. The database named `protector` and the `protector` user must exist with `ALL PRIVILEGES` on that database.
- **`trilio-site-recovery` package** is installed on the node where you will run `protector-manage`. The command is shipped as a console script by this package.
- **`/etc/protector/protector.conf`** is present and contains a valid `[database] connection` string pointing to the protector database.
- For Juju deployments: the `trilio-site-recovery-for-openstack` charm unit is in an appropriate state. Use Juju actions rather than running `protector-manage` directly.
- For Kolla and bare-metal deployments: you have shell access to a node where the `trilio-site-recovery` package is installed.
- When crossing migration `022_add_reconcile_operation_type`, you must run `protector-manage db preflight` **before** `db sync` on Kolla and bare-metal deployments. The Juju charm's `db-sync` action runs the preflight automatically.
- The `dr_operator` and `dr_site_admin` Keystone roles must exist on both sites before the service starts, as trust creation fails if a role named in `default_trust_roles` is absent.

## Installation

### Create the database and user

Before running migrations, the database and user must exist. On the MariaDB or MySQL host, run the following SQL (adjust the password to match your `protector.conf`):

```sql
CREATE DATABASE IF NOT EXISTS protector CHARACTER SET utf8;

GRANT ALL PRIVILEGES ON protector.* TO 'protector'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON protector.* TO 'protector'@'%' IDENTIFIED BY 'your-password';

FLUSH PRIVILEGES;
```

The default database name and user are both `protector` (as configured by the Juju charm).

---

### Juju deployment

For charm-managed deployments, database migration is handled by the `db-sync` Juju action. The charm runs the preflight check automatically before applying migrations.

**Run the initial migration (or upgrade):**

```bash
juju run trilio-site-recovery-for-openstack/leader db-sync
```

The charm reads `protector.conf` it has written and passes `--config-file` to `protector-manage` automatically.

---

### Kolla-Ansible deployment

On a Kolla-managed node, migrations run inside the `protector_api` container. When crossing migration `022`, run the preflight first:

```bash
# 1. Run the preflight check
docker exec protector_api protector-manage db preflight

# 2. Only proceed if the preflight exits 0
docker exec protector_api protector-manage db sync
```

The Kolla install script (`deploy/kolla/install.sh`) handles this sequence during initial deployment.

---

### OpenShift (RHOSO18) deployment

On OpenShift, migrations are run by the `job-db-sync` init Job, which is applied by the installer script as part of the full deployment sequence:

```bash
bash deploy/openshift/scripts/install.sh
```

Do not apply individual manifests manually — the installer applies twelve manifests in a specific order, including init Jobs gated on completion checks. See the OpenShift deployment guide for full details.

---

### Manual / bare-metal deployment

If you are running `protector-manage` directly on a machine where the `trilio-site-recovery` package is installed:

```bash
# 1. Run the preflight check (required when crossing migration 022)
protector-manage --config-file /etc/protector/protector.conf db preflight

# 2. Apply all pending migrations
protector-manage --config-file /etc/protector/protector.conf db sync
```

The `--config-file` flag is optional if `/etc/protector/protector.conf` is in its default location, but specifying it explicitly avoids ambiguity.

## Usage

### The `protector-manage` command

`protector-manage` is the single supported entry point for all database management operations. It is installed as a console script by the `trilio-site-recovery` package at `/usr/bin/protector-manage`.

**General syntax:**

```bash
protector-manage [--config-file <path>] [--debug] db <subcommand> [args]
```

The `--config-file` flag may be repeated to merge multiple configuration files. When omitted, the command reads `/etc/protector/protector.conf`.

---

### Subcommands

#### `db sync` — apply all pending migrations

Brings the schema to the current head revision. This is the standard command for initial setup and upgrades.

```bash
protector-manage db sync
```

On a fresh database with no `alembic_version` table (created via `CREATE DATABASE` but never migrated), `db sync` stamps the schema at the bootstrap revision `012_add_activedr_columns` and then runs all newer migrations. This handles the case where the schema was created by `SQLAlchemy.create_all()` before Alembic tracking was added.

> **Important:** When upgrading across migration `022_add_reconcile_operation_type`, run `db preflight` first on Kolla and bare-metal deployments. The Juju charm's `db-sync` action does this automatically.

---

#### `db preflight` — validate the schema before migrating

Checks that the `dr_operations.operation_type` ENUM column holds the correct token set before migration `022` is applied. Exits non-zero if the column contains tokens the ORM does not recognise as member names, which would cause data corruption if `MODIFY COLUMN` ran against them.

```bash
protector-manage db preflight
```

Run this **before** `db sync` on every Kolla and bare-metal node when upgrading to a release that includes migration `022` or later. If the preflight exits non-zero, do not run `db sync` — contact Trilio support.

The preflight is a no-op on databases other than MySQL and MariaDB (which have a native ENUM type). On those backends it always exits 0.

---

#### `db current` — show the current schema revision

Prints the Alembic revision currently recorded in the database.

```bash
protector-manage db current
```

The current head revision is `025_add_member_removing`. If `db current` returns anything earlier, pending migrations exist.

---

#### `db stamp` — record a revision without running migrations

Sets the `alembic_version` row to a specific revision without executing any migration scripts. Use this only when you have applied schema changes by another means and need Alembic to reflect the state.

```bash
# Stamp at the current head
protector-manage db stamp head

# Stamp at a specific revision
protector-manage db stamp 022_add_reconcile_operation_type
```

> **Caution:** Stamping at the wrong revision causes subsequent `db sync` calls to skip or re-apply migrations. Use `db current` to verify the result.

---

### Verifying schema state with `schema_revision.py`

The `tools/schema_revision.py` script compares the database's recorded revision against what the installed code expects, and lists any pending migrations with the columns they add:

```bash
python3 tools/schema_revision.py --config-file /etc/protector/protector.conf
```

Use `--check` for a terse one-line output suitable for scripts:

```bash
python3 tools/schema_revision.py --check
# Exit 0: in step
# Exit 2: database behind the code
# Exit 3: database at an unknown (newer) revision
# Exit 1: could not determine
```

This script is particularly useful before restarting the service after an upgrade, to confirm migrations have been applied before the new code starts reading columns it now declares.

---

### Purging the database (Juju)

The `purge-database` Juju action drops all tables from the protector database (keeping the database itself, since it was created by the `shared-db` relation):

```bash
juju run trilio-site-recovery-for-openstack/leader purge-database
```

After purging, run `db-sync` to recreate the schema:

```bash
juju run trilio-site-recovery-for-openstack/leader db-sync
```

To drop the database itself (not just the tables), pass `drop_schema=true` when invoking the underlying `db_purge.py` script directly — but note this is not exposed as a Juju action parameter and requires manual invocation.

## Examples

### Check the current schema revision

```bash
protector-manage --config-file /etc/protector/protector.conf db current
```

---

### Run the preflight check then apply migrations

```bash
# Step 1: preflight (required when crossing migration 022)
protector-manage --config-file /etc/protector/protector.conf db preflight

# Step 2: apply migrations (only if preflight exits 0)
protector-manage --config-file /etc/protector/protector.conf db sync
```

---

### Apply migrations inside a Kolla container

```bash
docker exec protector_api protector-manage db preflight
docker exec protector_api protector-manage db sync
```

---

### Run the Juju db-sync action

```bash
juju run trilio-site-recovery-for-openstack/leader db-sync
```

The charm runs `db preflight` automatically before `db sync`.

---

### Stamp the database at the bootstrap revision (fresh schema)

Use this when a schema was created by `create_all()` before Alembic tracking was introduced and `db sync` complains that it cannot find the `alembic_version` table:

```bash
protector-manage db stamp 012_add_activedr_columns
protector-manage db sync
```

---

### Check whether the schema is in step with the installed code

```bash
python3 tools/schema_revision.py --config-file /etc/protector/protector.conf --check
```

---

### Create the protector database and user (SQL)

```sql
CREATE DATABASE IF NOT EXISTS protector CHARACTER SET utf8;

GRANT ALL PRIVILEGES ON protector.* TO 'protector'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON protector.* TO 'protector'@'%' IDENTIFIED BY 'your-password';

FLUSH PRIVILEGES;
```

---

### Purge and recreate the schema (Juju)

```bash
juju run trilio-site-recovery-for-openstack/leader purge-database
juju run trilio-site-recovery-for-openstack/leader db-sync
```

## Troubleshooting

### Service fails to start: `Unknown column` errors in logs

**Symptom:** The protector-api or protector-engine log shows `Unknown column 'X' in 'field list'` or similar SQLAlchemy errors immediately after an upgrade.

**Cause:** The service was started before `db sync` completed. The SQLAlchemy models declare columns that do not yet exist in the database. Every read of the affected table fails until the migration runs.

**Fix:**
1. Stop the service.
2. Run `protector-manage db preflight` (required if crossing migration `022`).
3. Run `protector-manage db sync`.
4. Confirm with `protector-manage db current` that the revision matches `025_add_member_removing`.
5. Restart the service.

On Juju: `juju run trilio-site-recovery-for-openstack/leader db-sync`, then `juju run trilio-site-recovery-for-openstack/leader health-check`.

---

### `db preflight` exits non-zero before migration `022`

**Symptom:** `protector-manage db preflight` prints a message about unexpected tokens in `dr_operations.operation_type` and exits with status 1.

**Cause:** The `operation_type` ENUM column on disk holds tokens that do not match the ORM's member names (for example, lowercase values like `'failover'` instead of `'FAILOVER'`). Running `db sync` in this state would cause migration `022` to rewrite the ENUM and corrupt every existing operation row's label.

**Fix:** Do not run `db sync`. Contact Trilio support with the output of:
```sql
SHOW COLUMNS FROM dr_operations LIKE 'operation_type';
```

---

### `db sync` reports: `Could not find alembic.ini`

**Symptom:** `protector-manage db sync` exits with `RuntimeError: Could not find alembic.ini in: [...]`.

**Cause:** The `trilio-site-recovery` package is not installed, or is installed in an unexpected location. The command searches `/opt/openstack-protector/alembic.ini` and `/usr/share/trilio-site-recovery/alembic.ini`.

**Fix:** Verify the package is installed:
```bash
dpkg -l trilio-site-recovery
```
If missing, install it from the Trilio apt repository configured at `/etc/apt/sources.list.d/trilio-site-recovery.list`:
```bash
apt-get install trilio-site-recovery
```
Do not place `alembic.ini` under `/etc/protector/` — it is not installed there.

---

### `db sync` fails: `Access denied for user 'protector'`

**Symptom:** `db sync` exits with a SQLAlchemy `OperationalError` containing `Access denied`.

**Cause:** The `protector` database user does not have the required privileges, or the password in `protector.conf` does not match the one set in MySQL.

**Fix:** On the MySQL host:
```sql
GRANT ALL PRIVILEGES ON protector.* TO 'protector'@'%' IDENTIFIED BY 'your-password';
FLUSH PRIVILEGES;
```
Verify the connection string in `[database] connection` matches the credentials you granted.

---

### Schema revision check shows database ahead of the code

**Symptom:** `tools/schema_revision.py` exits with status 3: "the database is at X, which this code does not know about."

**Cause:** A newer version of the service has previously run `db sync` on this database, and the installed code is older than the schema.

**Fix:** Upgrade the `trilio-site-recovery` package to the version that introduced the recorded revision before starting the service. Do not run `db sync` with the older code against the newer schema.

---

### Juju unit in BLOCKED state after upgrade: `set local-site, pure-primary-site-name, pure-secondary-site-name`

**Symptom:** After upgrading the charm to revision 16, the unit enters BLOCKED with the message `set local-site, pure-primary-site-name, pure-secondary-site-name`.

**Cause:** This is expected behaviour in charm revision 16 (1.0.24). The charm requires `local-site` to be explicitly set — the service no longer infers which site it is. When a Pure storage array is configured, the two site names are also required.

**Fix:**
```bash
juju config trilio-site-recovery-for-openstack local-site=<your-site-name>
# If using Pure storage:
juju config trilio-site-recovery-for-openstack \
  pure-primary-site-name=<primary-site-name> \
  pure-secondary-site-name=<secondary-site-name>
```
After setting these, the unit will leave BLOCKED and re-run `db-sync` automatically.

---

### `protector-manage db sync` on OpenShift applies migrations incorrectly

**Symptom:** Migrations appear to run but the service cannot authenticate tokens or reach the database after an OpenShift deployment.

**Cause:** The OpenShift deployment requires twelve manifests applied in a specific order, including init Jobs (`job-db-init`, `job-rabbitmq-init`, `job-keystone-init`, `job-db-sync`). Running `protector-manage db sync` manually against an incomplete OpenShift deployment skips the database creation, RabbitMQ, and Keystone registration steps.

**Fix:** Use the installer script, which applies all manifests in the correct order and gates each init Job on its completion:
```bash
bash deploy/openshift/scripts/install.sh
```
Do not apply individual manifests manually for a new OpenShift deployment.
