#!/bin/bash

set -eo pipefail

{% if sftpgo_db_engine == 'sqlite' %}
sqlite3 {{ sftpgo_root_dir }}/data/sftpgo.sqlite .dump | zstd -c > {{ sftpgo_root_dir }}/backup/sftpgo.sql.zst
{% elif sftpgo_db_engine == 'mysql' %}
/usr/bin/mysqldump \
{% if sftpgo_db_server not in ['localhost', '127.0.0.1'] %}
  --user={{ sftpgo_db_user | quote }} \
  --password={{ sftpgo_db_pass | quote }} \
  --host={{ sftpgo_db_server | quote }} \
  --port={{ sftpgo_db_port }} \
{% endif %}
  --quick --single-transaction \
  --add-drop-table {{ sftpgo_db_name }} | \
  zstd -c > {{ sftpgo_root_dir }}/backup/{{ sftpgo_db_name }}.sql.zst
{% endif %}

cp -a {{ sftpgo_root_dir }}/etc/id_* {{ sftpgo_root_dir }}/backup/
