Migrate data into MySQL from bootstrap.

Use Heat Metadata to specify host and credentials to pull whole database
snapshot and setup Replication. Also include root credentials for use after
the snapshot has been applied.

Change-Id: Ie3aa92463d28db54a523f520c2b4cdfb528acf9d
This commit is contained in:
Clint Byrum 2013-03-21 16:36:54 -07:00
parent 3ace1ba29e
commit 948347131f
5 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,41 @@
Migrate data from another MySQL server into the local one using
os-config-applier and os-refresh-config.
Please note the migration process is *destructive* to any data currently
in the MySQL database running on the target host. Safeguards are in
place to ensure the process only happens once on any machine.
Configuration
-------------
Pass in Heat Metadata with the following structure in the
OpenStack::Config sub-key.
mysql:
users:
root:
username: rootuser
password: XXXXXXX
dump:
username: dumpuser
password: XXXXXXX
mysql-migration:
bootstrap_host: x.y.z
slave_user: slave-bot1
slave_password: XXXXXXXX
The migration process assumes `dump` and `root` exist on the
`bootstrap_host` and have access from this host.
The `dump` user will be used to dump data from `bootstrap_host`. The
`root` user will be used for localhost access after the database is
migrated. If `slave_user` and `slave_password` are set to non-empty
strings, replication will be setup against the `bootstrap_host` using
this user/password combination.
Special /root/.my.cnf
---------------------
As a convenience, we copy the given `dump` and `root` user names and
passwords to /root/.my.cnf after migration. If this file is overwritten,
they will also be available as /root/metadata.my.cnf

View File

@ -0,0 +1,3 @@
mysql
os-config-applier
os-refresh-config

View File

@ -0,0 +1,4 @@
MIGRATION_HOST={{mysql-migration.bootstrap_host}}
MIGRATION_USER={{mysql-migration.slave_user}}
MIGRATION_PASSWORD={{mysql-migration.slave_password}}
MIGRATION_DUMP_USER={{mysql-migration.users.dump.username}}

View File

@ -0,0 +1,10 @@
{{#mysql-migration.users.root}}
[client]
user={{username}}
password={{password}}
{{/mysql-migration.users.root}}
{{#mysql-migration.users.dump}}
[mysqldump]
user={{username}}
password={{password}}
{{/mysql-migration.users.dump}}

View File

@ -0,0 +1,76 @@
#!/bin/bash
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
set -eux
# Quietly go away unless a migration has been asked for
DEFAULTS=/etc/mysql/migration_default
[ -e $DEFAULTS ] || exit 0
source $DEFAULTS
DONE_FILE=/etc/mysql/migration_done
if [ -e $DONE_FILE ] ; then
echo migration from [$MIGRATION_HOST] already completed.
ls -l $DONE_FILE
exit 0
fi
# MySQL may be stopped pre-configuration, so try to start it
if [ -d /etc/init ] ; then
# Upstart: During initial boot, mysql will start in parallel with os-refresh-config
# wait-for-state is a job that allows blocking until a particular state is reached.
start wait-for-state WAIT_FOR=mysql WAITER=$(basename $0) WAIT_FOREVER=Y TARGET_GOAL=start WAIT_STATE=running
else
service mysql start || :
fi
local_mysql() {
if [ -e /root/.my.cnf ] ; then
mysql --defaults-extra-file=/root/.my.cnf "$@"
else
mysql "$@"
fi
}
local_mysql -e 'SHOW GRANTS'
# This runs as root. We assume root has a .my.cnf or access
# via localhost.
if [ -n "$MIGRATION_HOST" ] ; then
local_mysql -e 'STOP SLAVE' || :
# If we are planning on setting up a full slave
if [ -n "$MIGRATION_USER" ] && [ -n "$MIGRATION_PASSWORD" ] ; then
local_mysql -e "CHANGE MASTER TO master_host='${MIGRATION_HOST}', master_user='${MIGRATION_USER}', master_password='${MIGRATION_PASSWORD}'"
fi
mysqldump --defaults-extra-file=/root/metadata.my.cnf \
-u $MIGRATION_DUMP_USER
--single-transaction \
--all-databases \
--master-data \
-h $MIGRATION_HOST | local_mysql
# After this following command, our ~/.my.cnf may stop working as its
# password may change due to the dump loaded above.
local_mysql -e 'FLUSH PRIVILEGES'
# Now that database has been loaded, use creds that should match
cp -f /root/metadata.my.cnf /root/.my.cnf
# Now get the slave going if creds were provided
if [ -n "$MIGRATION_USER" ] && [ -n "$MIGRATION_PASSWORD" ] ; then
local_mysql -e "START SLAVE"
fi
touch $DONE_FILE
fi