Vai al contenuto

Utenti di database

Tenax esegue il backup di ogni database con un solo utente, tenax, la cui password si trova in un Secret chiamato tenax-* accanto al database. Non usa mai un account di amministratore. Per creare l'utente bastano due comandi, più una riga di configurazione del server per poter ripristinare qualsiasi secondo.

NS=team-a                         # the database's namespace
PASSWORD=$(openssl rand -hex 24)  # hex: no quoting surprises

# 1. The Secret Tenax reads
kubectl -n $NS create secret generic tenax-postgres \
  --from-literal=username=tenax --from-file=password=<(printf %s "$PASSWORD")

# 2. The user, as a superuser, through the database's pod (deploy/…, sts/… or a pod)
kubectl -n $NS exec -i deploy/postgres -- psql -U postgres -v ON_ERROR_STOP=1 <<SQL
\set tenax_password '$PASSWORD'
SELECT 'CREATE ROLE tenax LOGIN REPLICATION' WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tenax') \gexec
ALTER ROLE tenax WITH LOGIN REPLICATION PASSWORD :'tenax_password';
GRANT pg_read_all_data, pg_monitor TO tenax;
ALTER SYSTEM SET max_slot_wal_keep_size = '2GB';
SELECT pg_reload_conf();
SQL

3. Per poter ripristinare qualsiasi secondo, consenti all'utente la replica: aggiungi questa riga a pg_hba.conf e ricarica la configurazione (senza riavvio).

host  replication  tenax  all  scram-sha-256

Se Postgres è gestito da un operator o da un chart Helm, aggiungi la riga dove questo configura pg_hba (per esempio spec.postgresql.pg_hba in CloudNativePG, o il valore pgHbaConfiguration del chart).

Lo stesso SQL come file, con commenti (docs/sql/postgres-users.sql)
docs/sql/postgres-users.sql
-- Tenax's database user for PostgreSQL 15/16/17 (run once as a superuser, e.g. with psql):
--
--   psql -v tenax_password="$TENAX_PASSWORD" -f postgres-users.sql
--
-- One user, `tenax`, does everything (ADR 0016): inventory queries, logical dumps (pg_dump), exports and checks
-- with pg_read_all_data + pg_monitor; physical base backups (pg_basebackup) and the WAL stream with REPLICATION.
--
-- For physical backups and point-in-time restore pg_hba.conf must also allow it to replicate, then reload:
--     host replication tenax all scram-sha-256
-- Without that rule Tenax still works: it takes logical backups and tells you the rule to add.
-- Also needed: wal_level = replica (the default) and max_wal_senders >= 2 (default 10).
\set ON_ERROR_STOP on

SELECT format('CREATE ROLE tenax LOGIN REPLICATION PASSWORD %L', :'tenax_password')
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'tenax') \gexec
ALTER ROLE tenax WITH LOGIN REPLICATION NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD :'tenax_password';
GRANT pg_read_all_data, pg_monitor TO tenax;
-- Keep backup sessions bounded.
ALTER ROLE tenax SET idle_in_transaction_session_timeout = '10min';

-- A stopped streamer must never fill the database disk: cap WAL retained by replication slots (no restart needed).
ALTER SYSTEM SET max_slot_wal_keep_size = '2GB';
SELECT pg_reload_conf();
NS=team-a
PASSWORD=$(openssl rand -hex 24)

# 1. The Secret Tenax reads
kubectl -n $NS create secret generic tenax-mysql \
  --from-literal=username=tenax --from-file=password=<(printf %s "$PASSWORD")

# 2. The user, as root (the official image has the root password in MYSQL_ROOT_PASSWORD)
kubectl -n $NS exec -i deploy/mysql -- sh -c 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot' <<SQL
CREATE USER IF NOT EXISTS 'tenax'@'%' IDENTIFIED BY '$PASSWORD';
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES, PROCESS, RELOAD, SHOW_ROUTINE,
      REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'tenax'@'%';
SQL

3. Il ripristino a qualsiasi secondo richiede il binlog attivo, in formato row: entrambi sono i valori predefiniti di MySQL 8. select @@log_bin, @@binlog_format mostra 1, ROW.

Lo stesso SQL come file, con commenti (docs/sql/mysql-users.sql)
docs/sql/mysql-users.sql
-- Tenax's database user for MySQL 8.4 (run once as root, replace the password):
--
--   mysql -uroot -p < mysql-users.sql
--
-- One user, `tenax`, does everything (ADR 0016): consistent logical dumps (mysqldump --single-transaction
-- --source-data=2), inventory and checks, and the binlog stream (mysqlbinlog --read-from-remote-server).
-- Without REPLICATION SLAVE / REPLICATION CLIENT Tenax still takes dumps, but without point-in-time restore.
--
-- Server settings (all MySQL 8 defaults except GTIDs): log_bin=ON, binlog_format=ROW, a unique server_id;
-- gtid_mode=ON with enforce_gtid_consistency=ON is recommended; binlog_expire_logs_seconds long enough for the
-- streamer to catch up after an outage (default 30 days).
CREATE USER IF NOT EXISTS 'tenax'@'%' IDENTIFIED BY 'CHANGE_ME_PASSWORD';
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES, PROCESS, RELOAD, SHOW_ROUTINE,
      REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'tenax'@'%';
FLUSH PRIVILEGES;

Fatto. Fai puntare il target al Secret:

spec:
  credentials:
    secretRef: { name: tenax-postgres }

Senza il passo 3

Non fallisce niente. Tenax prova a replicare con l'utente e, se il database rifiuta:

  • i backup completi sono logici, secondo la pianificazione della policy;
  • la condizione Replication del target, la console e gli avvisi di ogni backup dicono esattamente cosa aggiungere;
  • Tenax ricontrolla ogni pochi minuti e avvia lo streaming da solo non appena l'hai aggiunto.
L'utente può Ti dà Privilegi
Leggere tutto backup logici, inventari, esportazioni, controlli Postgres pg_read_all_data + pg_monitor; MySQL SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES, PROCESS, RELOAD, SHOW_ROUTINE
Replicare backup fisici (Postgres) e il flusso di modifiche: ripristino a qualsiasi secondo Postgres REPLICATION più la regola in pg_hba.conf; MySQL REPLICATION SLAVE, REPLICATION CLIENT

Slot di replica

Lo stream di Postgres conserva il WAL in uno slot di replica finché Tenax non l'ha salvato. L'SQL limita a 2 GB ciò che uno streamer fermo può trattenere (max_slot_wal_keep_size), Tenax genera l'alert TenaxSlotWALRetentionHigh molto prima di quella soglia, ed elimina lo slot quando il target viene cancellato o il ripristino point-in-time viene disattivato.

Avanzato: mantenere l'utente sincronizzato automaticamente, per configurazioni GitOps in cui le password ruotano e i database vengono ricostruiti.