diff options
Diffstat (limited to 'rebasedb')
-rwxr-xr-x | rebasedb | 40 |
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 |