-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql-import
executable file
·59 lines (47 loc) · 1.45 KB
/
mysql-import
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
HOST=
PORT='3306'
USERNAME=
PASSWORD=
DATABASE=
FILE=
MYSQL_VERSION='8'
print_usage() {
printf "Usage:\tmysql-import\n\t\t-h <host>\n\t\t-P <port>\n\t\t-u <username>\n\t\t-p <password>\n\t\t-d <database-name>\n\t\t-f <file-name>\n\t\t-v <mysql-server-version>\n\n"
printf "\t-P\tport is optional, defaults to 3306\n"
printf "\t-v\tmysql-server-version is optional, defaults to 8\n"
}
while getopts 'h:P:u:p:d:f:v:' flag; do
case "${flag}" in
h) HOST="${OPTARG}" ;;
P) PORT="${OPTARG}" ;;
u) USERNAME="${OPTARG}" ;;
p) PASSWORD="${OPTARG}" ;;
d) DATABASE="${OPTARG}" ;;
f) FILE="${OPTARG}" ;;
v) MYSQL_VERSION="${OPTARG}" ;;
esac
done
if [ -z "$HOST" ]; then
print_usage
exit 2;
fi
if [ -z "$USERNAME" ]; then
print_usage
exit 2;
fi
if [ -z "$PASSWORD" ]; then
print_usage
exit 2;
fi
if [ -z "$DATABASE" ]; then
print_usage
exit 2;
fi
CONTAINER_NAME=noahjahn-utils-mysql-import-mysql
docker run --name $CONTAINER_NAME --rm --entrypoint=tail -d mysql:$MYSQL_VERSION -f /dev/null
docker cp $FILE $CONTAINER_NAME:/$FILE
docker exec -i $CONTAINER_NAME mysql --host $HOST --port $PORT --user $USERNAME -p$PASSWORD -e 'SET FOREIGN_KEY_CHECKS=0;'
docker exec -i $CONTAINER_NAME mysql --host $HOST --port $PORT --user $USERNAME -p$PASSWORD $DATABASE < $FILE
docker exec -i $CONTAINER_NAME mysql --host $HOST --port $PORT --user $USERNAME -p$PASSWORD -e 'SET FOREIGN_KEY_CHECKS=1;'
docker stop -t 1 $CONTAINER_NAME