aboutsummaryrefslogtreecommitdiffstats
path: root/copydb
blob: df05f4477ae615dc1d9aca19934138921712ac82 (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
41
42
43
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