#!/bin/bash

set -x
set -e

main() {
    local src_node
    local dst_node
    [ -n "$1" ] || {
        echo "No source DB node"
        exit 1
    }
    src_node=$1
    [ -n "$2" ] || {
        echo "No destination DB node"
        exit 1
    }
    dst_node=$2
    dbs="keystone nova heat neutron glance cinder"
    ssh $src_node "mysqldump --lock-all-tables --add-drop-database --databases $dbs" \
        | gzip | tee dbs.original.sql.gz \
        | ssh $dst_node "zcat | mysql"
    ssh $dst_node "keystone-manage db_sync;
nova-manage db sync;
heat-manage db_sync;
neutron-db-manage --config-file=/etc/neutron/neutron.conf upgrade head;
glance-manage db upgrade;
cinder-manage db sync"
}

get_ctrl() {
    [ -z "$1" ] && exit 1
    echo $(fuel node --env $1 | awk '/controller/ {print "node-" $1;exit}')
}

orig_ctrl=$(get_ctrl $1)
seed_ctrl=$(get_ctrl $2)
main $orig_ctrl $seed_ctrl
exit 0

#!/bin/bash

set -x
set -e

main() {
    local src_node
    local dst_node
    [ -n "$1" ] || {
        echo "No source DB node"
        exit 1
    }
    src_node=$1
    [ -n "$2" ] || {
        echo "No destination DB node"
        exit 1
    }
    dst_node=$2
    dbs="keystone nova heat neutron glance cinder"
    dbs_tuple=$(echo $dbs | sed -re "s%^%'%;s% %', '%g;s%$%'%")
    echo "USE information_schema;
SELECT 'SET FOREIGN_KEY_CHECKS = 0;' UNION
SELECT concat('DROP TABLE IF EXISTS ', TABLE_SCHEMA, '.', TABLE_NAME, ';')
FROM tables
WHERE TABLE_SCHEMA in ($dbs_tuple);" | ssh $dst_node "mysql -N | mysql"
    ssh $src_node "mysqldump --lock-all-tables --databases $dbs | gzip " \
    | tee dbs.original.sql.gz | ssh $dst_node "zcat | mysql"
    ssh $dst_node "keystone-manage db_sync;
nova-manage db sync;
heat-manage db_sync;
neutron-db-manage --config-file=/etc/neutron/neutron.conf upgrade head;
glance-manage db upgrade;
cinder-manage db sync"
}

get_ctrl() {
    [ -z "$1" ] && exit 1
    echo $(fuel node --env $1 | awk '/controller/ {print "node-" $1;exit}')
}

orig_ctrl=$(get_ctrl $1)
seed_ctrl=$(get_ctrl $2)
main $orig_ctrl $seed_ctrl
exit 0

Drop tables in 6.0 DB before database upgrade
Drop tables in 6.0 DB before database upgrade

Dropping database doesn't work with Galera cluster. Instead, drop all tables in
databases of OpenStack services in 6.0 env before transferring data. Ignore
foreign keys when deleting tables.
