#!/bin/bash

backup_db::label() {
  echo "Backup database"
}

backup_db::doc() {
  cat <<EOT

Before performing an upgrade of the database content, a full backup has to be done.
When coming from the outside, a port forward to the Postgresql port needs to be created with oc port-forward.
Standard pg_dump should be used to create the backup of the database.
To avoid local installation issues and to guarantee version conformance to the database in use with syndesis-db, pg_dump should be taken directly from the Postgres image used by syndesis-db by starting this image from a local Docker daemon.

The backup itself should be stored into a local directory, which can also be configured during startup

As a bonus, a dedicated --db-backup option could be provided to the CLI only to perform a DB backup.

.Rollback

The rollback step should clean up the database dump file (or kept for a later manual rollback).
EOT
}

backup_db::run() {
    local backupdir=$1

    pg_backup "$backupdir/db" "syndesis"
}

backup_db::rollback() {
    local backupdir=${1}
    local workdir=${2}
    local cleanup=${3:-}

    [ -f "${backupdir}/db/syndesis" ] && rm "${backupdir}/db/syndesis"
}
