Skip to content

Instantly share code, notes, and snippets.

@fubarhouse
Last active November 7, 2018 01:03
Show Gist options
  • Save fubarhouse/2cd49f457aa2aec1a3bf2d9915284313 to your computer and use it in GitHub Desktop.
Save fubarhouse/2cd49f457aa2aec1a3bf2d9915284313 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Database importer.
#
# Used with the govCMS scaffolding projects by Department of Finance.
# Created independently of the Department of Finance by Karl Hepworth.
# @fubarhouse / karl.hepworth@gmail.com
#
# Pass one argument to this script to provide the path or drush alias
# for either syncing or importing from a sql file. This should ideally
# be invoked from the command line with tools such as Ahoy.
#
# usage: db-import.sh [path|null]
#
# License: MIT
# This tool comes with absolutely no warranty, use at your own discretion.
# Variables
FILENAME="database.sql";
FILEPATH=${1:-""};
CLICONTAINER=$(docker-compose ps -q cli);
ALIAS=$(drush sa | grep "${FILEPATH}" | wc -l | xargs);
SQLCOUNT=$(ls -lah *.sql | wc -l | xargs);
SQLFILE=$(ls *.sql);
# dbimport will import the input file to the database on $CLICONTAINER.
#
function dbimport() {
# Before we do this, make sure a file path was passed to this function.
if [[ -n "${1}" && -f ${1} ]]; then
# Copy the file into the container, prevent edge-cases.
docker cp "${SQLFILE}" "${CLICONTAINER}:/app/${FILENAME}";
# Actually import the database.
docker-compose exec -T cli drush sql-cli < "${1}";
else
echo "Error occured, importing cannot proceed.";
fi
# In case an error was presented, we should make an effort to report it.
if [[ ! $? -eq 0 ]]; then
echo "Error was encountered during importing.";
fi
}
# If Filepath is provided, we can test against it and perform the import.
if [[ -n "${FILEPATH}" ]]; then
# Check if argument is a file.
if [[ -f "${FILEPATH}" ]]; then
dbimport "${FILEPATH}";
# It isn\'t a file, so is must be a drush alias?
elif [[ ${ALIAS} == 1 ]]; then
# Syncronise the two databases.
docker-compose exec -T cli drush sql-sync "${FILEPATH}" @self;
elif [[ ! ${ALIAS} == 1 ]]; then
echo "Too many drush aliases to pick from, please specify one."
# No file, no drush alias, we should bail!
else
echo "Could not find ${FILEPATH}, does not exist or permissions are not granted."
stat "{$FILEPATH}" || true
fi
# We can now exit!
exit 0;
fi
# If Filepath is not provided, we can attempt to discover possibilities.
if [[ -z "${FILEPATH}" ]]; then
# How many databases are we attempting to look at automatically?
# We need to be sure we only have one.
if [[ $SQLCOUNT == 1 ]]; then
dbimport $(ls *.sql);
# If we have more, we should exit.
elif [[ ! $SQLCOUNT -gt 1 ]]; then
echo "Too many databases to choose from, please specify just one."
# There''s nothing for us to work from...
elif [[ ! -a "${FILEPATH}" ]]; then
echo "Could not find ${FILEPATH}, does not exist or permissions are not granted."
stat "${FILEPATH}" || true
# All else fails, unconditionally.
else
echo "Could not find a database file, please provide one as a parameter.";
fi
# And now we can exit.
exit 0;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment