Database backup

How to back up and restore Comentario database in a Kubernetes cluster

It’s usually a good idea to make periodic backups of Comentario database. Below are procedures that apply when you run Comentario in a Kubernetes cluster.

SQLite

Making a backup

When using a local, file-based SQLite storage, you specify the database file location yourself. In this case you can simply make a copy of that file to another medium.

However, Comentario should not be running at the moment of making the copy. This downtime requirement makes the local storage option pretty inconvenient.

Restoring a backup

The same applies to restoring the backup:

  • Make sure Comentario is shut down.
  • Copy the file from the backup over to the required location.

PostgreSQL

The commands below assume PostgreSQL is run by CloudNativePG as a cluster named comentario-db, as described in Deployment, and the NAMESPACE variable is set to your namespace name. They run pg_dump and psql inside the primary PostgreSQL pod.

Making a backup

To get a full database dump, issue the following command:

kubectl exec -n $NAMESPACE \
    $(kubectl get -n $NAMESPACE pods -l cnpg.io/cluster=comentario-db,cnpg.io/instanceRole=primary -o name) \
    -c postgres -- pg_dump -d comentario > /path/to/comentario.sql

Restoring a backup

To restore the database from a previously downloaded dump file (made above), stop Comentario first, then feed the dump to psql (note the -i, which passes the standard input through to the pod):

kubectl exec -i -n $NAMESPACE \
    $(kubectl get -n $NAMESPACE pods -l cnpg.io/cluster=comentario-db,cnpg.io/instanceRole=primary -o name) \
    -c postgres -- psql -d comentario -v ON_ERROR_STOP=1 < /path/to/comentario.sql

The dump is meant to be restored into an empty comentario database, such as that of a newly created cluster.

Continuous backup

Apart from one-off dumps, CloudNativePG can continuously back up the database to object storage (such as S3), which allows restoring it to any point in time. Refer to the CloudNativePG documentation for details.