#!/bin/bash set -e

#Utility function to fire queries opening a connection with database function target_execute() {

MYSQL_PWD='{{source.password}}' mysql -u{{source.username}} -h{{source.host}} -P{{source.port}} {{target_db}} -e "$1"

}

function execute() {

MYSQL_PWD='{{source.password}}' mysql -u{{source.username}} -h{{source.host}} -P{{source.port}} -e "$1"

}

#Pull schema from source database echo “Pulling schema from {{source.database}}..” MYSQL_PWD='{{source.password}}' mysqldump -u{{source.username}} -h{{source.host}} -P{{source.port}} –no-data {{source.database}} > /tmp/databender_schema.sql

#Drop & create destination database. Load source schema. echo “Creating and loading schema to {{target_db}}..” execute 'drop database if exists {{target_db}};' execute 'create database {{target_db}};' MYSQL_PWD='{{source.password}}' mysql -u{{source.username}} -h{{source.host}} -P{{source.port}} {{target_db}} < /tmp/databender_schema.sql

echo “” echo “Generating subset database…”

## Process each table {{#entries}} echo “033[36mProcessing 033[32m{{{table}}}033[0m…” target_execute “{{{sql}}}” {{/entries}}

echo “Done!” echo “”

mkdir -p dumps

MYSQL_PWD='{{source.password}}' mysqldump -u{{source.username}} -h{{source.host}} -P{{source.port}} {{target_db}} | gzip > dumps/{{database}}.sql.gz

echo “033[92mThe dump of db_subset is available at dumps/{{database}}.sql.gz. Use gunzip to extract followed by mysql command to load. The subset database is intact in the db server too!033[0m”