diff options
author | Tom Willemse | 2014-03-10 20:32:10 +0100 |
---|---|---|
committer | Tom Willemse | 2014-03-10 20:32:10 +0100 |
commit | 2cd07d308c7a218bba0e321fa59f3d54d5c522ef (patch) | |
tree | 5bac478bb2e25e630ba1a0846d4fe07797db0f72 | |
download | db-utilities-2cd07d308c7a218bba0e321fa59f3d54d5c522ef.tar.gz db-utilities-2cd07d308c7a218bba0e321fa59f3d54d5c522ef.zip |
Initial commit
-rw-r--r-- | README.org | 7 | ||||
-rwxr-xr-x | copydb | 44 | ||||
-rwxr-xr-x | copydb-current-branch | 28 | ||||
-rwxr-xr-x | rebasedb | 40 |
4 files changed, 119 insertions, 0 deletions
diff --git a/README.org b/README.org new file mode 100644 index 0000000..b99350e --- /dev/null +++ b/README.org @@ -0,0 +1,7 @@ +Some simple utilities for working with MySQL databases. + +- copydb :: Create a copy of a database under a new name. +- copydb-current-branch :: Create a copy of a database under a new + name based on the current git branch. Requires =copydb=. +- rebasedb :: Destroy and recreate a database based on another + database. @@ -0,0 +1,44 @@ +#!/bin/zsh +## copydb --- Create a copy of a database under a new name + +## Commentary: + +# Create a copy of the database specified as the first argument as a +# new database named by the second argument. Making usage: + +# copydb <from> <to> + +# This doesn't try to check for any existing information, it just +# tries to create and copy. It is up to the user to make sure that +# that can actually be done. + +# During execution it asks for the root password of MySQL so it can +# create a new database. It will also ask for another user and its +# password to load the database with, this may still be the same user, +# but allows one to specify different users if necessary. + +## Code: + +echo -n 'Database root PW (for DB creation): ' +read -s ROOTPW +echo + +echo -n 'Database user for new DB: ' +read DBUSER + +echo -n 'Database user PW for new DB: ' +read -s DBPASSW +echo + +mysql -u root -p"$ROOTPW" -B <<EOF +CREATE DATABASE \`$2\`; +GRANT ALL ON \`$2\`.* to '$DBUSER'@'localhost'; +EOF + +mysqldump -u "$DBUSER" -p"$DBPASSW" "$1" \ + | mysql -u "$DBUSER" -p"$DBPASSW" "$2" \ +|| mysql -u root -p"$ROOTPW" -B <<EOF +DROP DATABASE \`$2\`; +EOF + +unset ROOTPW diff --git a/copydb-current-branch b/copydb-current-branch new file mode 100755 index 0000000..1228941 --- /dev/null +++ b/copydb-current-branch @@ -0,0 +1,28 @@ +#!/usr/bin/zsh +## copydb-current-branch --- Create a copy of a database for the current branch + +## Commentary: + +# Use `copydb' to create a copy of the database named after the +# current branch. This requires the current working directory to be a +# git repository. The name of the database is created by substituting +# all occurrences of `-' with `_'. The database copied from is always +# called `aeos_babel'. The database copied to is called +# `aeos_<branchname>'. + +# This program is just a wrapper for `copydb', so all information +# related to it is also relevant, please read its comments for more +# info. + +# Usage is: + +# copydb-current-branch + +## Code: + +base="aeos_babel" +branch=$(git branch --no-color | grep '*' | sed -e 's/* //' -e 's/-/_/g') +nbranch="aeos_${branch}" + +echo Copying "$base" to "$nbranch" +copydb "$base" "$nbranch" diff --git a/rebasedb b/rebasedb new file mode 100755 index 0000000..8d80a8a --- /dev/null +++ b/rebasedb @@ -0,0 +1,40 @@ +#!/bin/zsh +## rebasedb --- Recreate a database based on another database. + +## Commentary: + +# Recreate the database specified as the first parameter based on the +# database named by the second parameter. Basically this deletes the +# database named by the first argument and then copies the database +# named by the second argument into its place. + +# This command asks for the password of the root user to drop, create +# and fill the database. It is assumed that the proper rights have +# been specified before and that they will be carried over. + +# For usage information run the program without any arguments. + +## Code: + +if [ ! ${#@} -eq 2 ]; then + echo "Usage: $(basename $0) [todb] [fromdb]" + echo + echo "Rebases TODB on FROMDB so that TODB holds the same schema and " + echo "information as FROMDB" + exit 1 +fi + +echo -n 'Database root PW (for DB manipulation): ' +read -s ROOTPW +echo + +mysql -u root -p"$ROOTPW" -B <<EOF +DROP DATABASE $1; CREATE DATABASE $1; +EOF + +if [ $? -eq 0 ]; then echo "Recreated database $1"; else exit 2; fi + +mysqldump -u root -p"$ROOTPW" "$2" \ + | mysql -u root -p"$ROOTPW" -B "$1" + +if [ $? -eq 0 ]; then echo "Copied $2 to $1"; fi |