#!/bin/bash
#
# OVH / shared-hosting mysqldump wrapper (full dump, all tables).
# Usage: db-backup-ovh.sh <result-file> <host> <port> <user> <password> <database>
#
set -u

RESULT_FILE="$1"
HOST="$2"
PORT="$3"
USER="$4"
PASSWORD="$5"
DATABASE="$6"

if [[ -z "$RESULT_FILE" || -z "$HOST" || -z "$USER" || -z "$DATABASE" ]]; then
  echo "db-backup-ovh.sh: missing required arguments" >&2
  exit 1
fi

if [[ -z "$PORT" ]]; then
  PORT=3306
fi

mysqldump \
  --host="$HOST" \
  --port="$PORT" \
  --user="$USER" \
  --password="$PASSWORD" \
  --default-character-set=utf8 \
  --single-transaction \
  --quick \
  --no-tablespaces \
  --skip-routines \
  --skip-triggers \
  --add-drop-table \
  --comments \
  --result-file="$RESULT_FILE" \
  "$DATABASE"

DUMP_EXIT=$?

if [[ -s "$RESULT_FILE" ]]; then
  exit 0
fi

exit "$DUMP_EXIT"
