Skip to content

Instantly share code, notes, and snippets.

@jcormier
Created September 26, 2017 13:15
Show Gist options
  • Save jcormier/79c8acaa98d33b82d936e2527f0a0cc7 to your computer and use it in GitHub Desktop.
Save jcormier/79c8acaa98d33b82d936e2527f0a0cc7 to your computer and use it in GitHub Desktop.
sameersbn/docker-redmine mysql load old database
#!/bin/bash
# Exit on error
set -e
# Get script dir
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "$DIR/../"
# Require 1 argument
if [[ $# -eq 0 ]] ; then
echo 'Error: Requires path to redmine.sql'
echo 'Example: $0 backup/redmine.sql'
exit 0
fi
database_file=$(readlink -f $1)
gzip_file=false
database_production_password=password
database_production_host=mysql
database_production_port=3306
database_production_username=redmine
database_production_encoding=utf8
database_production_database=redmine_production
MYSQL_CMD="docker-compose run --rm redmine mysql \
--host ${database_production_host} --port ${database_production_port} \
--user ${database_production_username} --password=${database_production_password} --default-character-set ${database_production_encoding}"
# Verify pv is installed
pv -V >/dev/null || (echo "pv utility required. sudo apt-get install pv" && exit 1)
# Unzip file if its zipped
case "$database_file" in
*.sql.gz )
echo "Gzip file detected, unzipping"
# it's gzipped
gunzip --keep "$database_file"
# Remove .gz extension
database_file=${database_file%.gz}
gzip_file=true
;;
*.sql )
# it's not, no work needed
;;
*)
echo "Unexpected file extension on file: $database_file"
exit 1
;;
esac
echo "Starting mysql"
docker-compose up -d mysql
echo "Waiting for mysql"
until $MYSQL_CMD -e 'SELECT VERSION();'; do
>&2 echo "Mysql is unavailable - sleeping"
sleep 1
done
echo "Deleting old DB"
$MYSQL_CMD -e "DROP DATABASE IF EXISTS $database_production_database"
echo "Creating DB"
$MYSQL_CMD -e "CREATE DATABASE $database_production_database"
echo "Restoring MySQL database..."
# http://vitobotta.com/smarter-faster-backups-restores-mysql-databases-with-mysqldump/
(
echo "SET AUTOCOMMIT=0;"
echo "SET UNIQUE_CHECKS=0;"
echo "SET FOREIGN_KEY_CHECKS=0;"
pv "$database_file"
echo "SET FOREIGN_KEY_CHECKS=1;"
echo "SET UNIQUE_CHECKS=1;"
echo "SET AUTOCOMMIT=1;"
echo "COMMIT;"
) | \
$MYSQL_CMD ${database_production_database}
echo "Migrating DB"
sudo rm -rf ../redmine-data/tmp/
docker-compose run --rm redmine app:init
echo "Stopping mysql"
docker-compose down
if [ "$gzip_file" = true ]; then
rm $database_file
fi
@jcormier
Copy link
Author

jcormier commented Sep 26, 2017

Looks like what I did was to bring up the redmine container and let it create the database. Then I brought everything down. Brought up the sql database, then "dropped" the database, created it and then loaded the mysql dump.

I created this script to load the database a while back. Haven't tested it since but it worked for me back then. Note it expects that you are using docker-compose and that it is run in the directory with the compose file. Also that the redmine container is named redmine and the mysql container is named mysql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment