-
Notifications
You must be signed in to change notification settings - Fork 5
/
zzmysqlimp.sh
executable file
·84 lines (58 loc) · 2.28 KB
/
zzmysqlimp.sh
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
## bash-fx
if [ -z $(command -v curl) ]; then sudo apt update && sudo apt install curl -y; fi
if [ -f "/usr/local/turbolab.it/bash-fx/bash-fx.sh" ]; then
source "/usr/local/turbolab.it/bash-fx/bash-fx.sh"
else
source <(curl -s https://raw.githubusercontent.com/TurboLabIt/bash-fx/main/bash-fx.sh)
fi
## bash-fx is ready
fxHeader "↖ zzmysqlimp"
fxConfigLoader
## Checking input file
DUMP_FULLPATH=$(readlink -f "$1")
if [ -z "$DUMP_FULLPATH" ] || [ ! -f "$DUMP_FULLPATH" ]; then
fxCatastrophicError "Dump to import not found!
🕳 $DUMP_FULLPATH
"
fi
fxTitle "Extracting..."
DUMP_DIR=$(dirname "$DUMP_FULLPATH")/
DUMPFILE_FULLPATH=${DUMP_FULLPATH%.7z}
echo "Directory: ##${DUMP_DIR}##"
echo "Filename: ##${DUMPFILE_FULLPATH}##"
if [ ! -f "$DUMPFILE_FULLPATH" ] || [ "$(find "$DUMPFILE_FULLPATH" -mmin +3600)" ]; then
7za e "${1}" -o"${DUMP_DIR}" -y
else
fxInfo "The extracted file exists and was created today. Skipping the un-7zipping!"
fi
fxTitle "Listing..."
ls -lh "${DUMP_DIR}" | grep $(basename ${DUMPFILE_FULLPATH})
fxTitle "Changing database name..."
function sedReplace()
{
sed -i "s|${REPLACE_REGEX}|${REPLACE_WITH_TEXT}|g" "${DUMPFILE_FULLPATH}" --regexp-extended
fxOK "Done"
}
if [ ! -z "$2" ]; then
fxInfo "OK, the new database name will be #$2#"
echo ""
REPLACE_REGEX='^-- Current Database: `.+`'
REPLACE_WITH_TEXT="-- Database renamed to ##$2## with https://github.com/TurboLabIt/zzmysqldump"
sed -i "s|${REPLACE_REGEX}|&\n${REPLACE_WITH_TEXT}|g" "${DUMPFILE_FULLPATH}" --regexp-extended
fxOK "Note added"
REPLACE_REGEX='^/\*!40000 DROP DATABASE IF EXISTS `.+`\*/;'
REPLACE_WITH_TEXT="/\\*!40000 DROP DATABASE IF EXISTS \`$2\`\\*/;"
sedReplace "$REPLACE_REGEX" "$REPLACE_WITH_TEXT"
REPLACE_REGEX='^CREATE DATABASE /\*!32312 IF NOT EXISTS\*/ `.+`'
REPLACE_WITH_TEXT="CREATE DATABASE /\\*!32312 IF NOT EXISTS\\*/ \`$2\`"
sedReplace "$REPLACE_REGEX" "$REPLACE_WITH_TEXT"
REPLACE_REGEX='^USE `.+`;'
REPLACE_WITH_TEXT="USE \`$2\`;"
sedReplace "$REPLACE_REGEX" "$REPLACE_WITH_TEXT"
else
fxInfo "New database name not provided. Using the same name provided by the dump"
fi
fxTitle "Importing the extracted dump..."
mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_HOST" < "$DUMPFILE_FULLPATH"
fxEndFooter