aboutsummaryrefslogtreecommitdiffstats
path: root/rebasedb
blob: ab4de9b6336889df3b3d3531acc00d35520df7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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