aboutsummaryrefslogtreecommitdiffstats
path: root/rebasedb
diff options
context:
space:
mode:
authorGravatar Tom Willemse2014-03-10 20:32:10 +0100
committerGravatar Tom Willemse2014-03-10 20:32:10 +0100
commit2cd07d308c7a218bba0e321fa59f3d54d5c522ef (patch)
tree5bac478bb2e25e630ba1a0846d4fe07797db0f72 /rebasedb
downloaddb-utilities-2cd07d308c7a218bba0e321fa59f3d54d5c522ef.tar.gz
db-utilities-2cd07d308c7a218bba0e321fa59f3d54d5c522ef.zip
Initial commit
Diffstat (limited to 'rebasedb')
-rwxr-xr-xrebasedb40
1 files changed, 40 insertions, 0 deletions
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