#!/bin/sh
#
# M/MONIT SQLITE TO SQL EXPORT
#

MMONIT_HOME=`dirname ${0}`/..
SQLITE="sqlite3"
DBPATH="$MMONIT_HOME/db/mmonit.db"
DBTYPE=""


help() {
       /bin/echo "Usage: $0 -t {mysql|postgresql} [-h]";
       /bin/echo "Options are as follows:";
       /bin/echo "    -t Target database type (mysql or postgresql)";
       /bin/echo "    -h Print this text";
       /bin/echo "Use this script to dump data from a M/Monit SQLite database into a MySQL or a PostgreSQL database.";
       /bin/echo "Examples:";
       /bin/echo "    Export to MySQL: ./db/migrate_db.sh -t mysql | mysql -u mmonit mmonit -p";
       /bin/echo "    Export to PostgreSQL: ./db/migrate_db.sh -t postgresql | psql -U mmonit mmonit 2>&1 | grep ERROR";
       exit 0;
}


error() {
        /bin/echo "Error: $1";
        exit 1;
}

# note1: keep the order which reflects foreign keys dependency (parent before children)
# note2: the "session" table is skipped (the sqlite BLOB type is dumped as hexa, which is not compatible with the postgresql's BYTEA and the insert fails)
# note3: the target postgresql database needs to be created from scratch before migration (if the postgresql schema was upgraded, the sqlite's column position may not match postgresql's)
tables="mmonit
name
host
hostgroup
groupedhost
service
servicegroup
groupedservices
users
roles
rolerow
rolerowhost
rolerowhostgroup
rolerowpermission
userroles
event
eventnotice
eventfilter
rule
rulerow
rulerowhost
rulerowhostgroup
rulerowservice
rulerowservicegroup
rulerowstate
rulerowevent
actionrow
actionrowtarget
actionrowoption
actionqueue
mailserver
jabberserver
alertservice
messageformat
messagequeue
message
messagerecipients
statistics
statistics_string
statistics_int
statistics_llong
statistics_float
statistics_double
statistics_events
statistics_aggregate_1m
statistics_aggregate_2m
statistics_aggregate_15m
statistics_aggregate_30m
statistics_aggregate_1h
statistics_aggregate_2h
statistics_aggregate_4h
statistics_aggregate_12h
statistics_aggregate_1d
statistics_aggregate_3d
statistics_aggregate_7d
statistics_aggregate_14d
statistics_aggregate_4w";

while getopts ":ht:" c
do
case $c in
        t) case $OPTARG in
                mysql | postgresql)
                        DBTYPE=$OPTARG;
                        ;;
                *)
                        /bin/echo "Unknown database type: $OPTARG";
                        exit 1;
                        ;;
           esac;;
        *) help;;
esac
done


if test x$DBTYPE = "x"
then
        /bin/echo "Error: -t option required"
        help
fi


if [ ! -e "$DBPATH" ]
then
        error "SQLite database file '$DBPATH' does not exist";
else
        # purge data from all tables
        for i in $tables;
        do
                echo "DELETE FROM $i;";
        done

        # import data from sqlite
        for i in $tables;
        do
                echo "BEGIN;"
                $SQLITE $DBPATH ".dump --data-only --newlines $i"
                echo "COMMIT;"
        done
fi

exit;

