-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump
executable file
·102 lines (82 loc) · 2.52 KB
/
dump
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
function execDump() {
remoteHost=$1
remotePort=$2
remoteUser=$3
remotePassword=$4
dbName=$5
table=$6
fields=$7
conditions=$8
echo "SELECT $fields FROM $table WHERE $conditions"
mysqldump --single-transaction --opt -h $remoteHost --port=$remotePort -u $remoteUser -p$remotePassword --no-data $dbName $table > /out/$dbName.$table.sql
if [ "$FORMAT" = "sql" ]; then
mysqldump --single-transaction --opt -h $remoteHost --port=$remotePort -u $remoteUser -p$remotePassword --no-create-info $dbName $table --where "$conditions" > /out/$dbName.$table.data.sql
elif [ "$FORMAT" = "csv" ]; then
mysql -h $remoteHost --port=$remotePort -u $remoteUser -p$remotePassword $dbName -e "SELECT $fields FROM \`$table\` WHERE $conditions" | sed "s/^/\"/;s/$/\"/;s/\t/\"\t\"/g;s/\n//g" > /out/$dbName.$table.csv
elif [ "$FORMAT" = "json" ]; then
mysql -h $remoteHost --port=$remotePort -u $remoteUser -p$remotePassword $dbName -e "SELECT $fields FROM \`$table\` WHERE $conditions" > /out/$dbName.$table.data.sql
else
echo "config error: 'FORMAT'"
fi
}
function dump() {
dbName=$1
table=$2
fields=$3
conditions=$4
echo "Dump '$dbName'.'$table' Start..."
host=$HOST
port=$PORT
user=$USER
password=$PASSWORD
execDump $host $port $user $password $dbName $table "$fields" "$conditions"
echo "Dump '$dbName'.'$table' End"
echo
}
function getLength() {
echo "$1" | jq ".$2 | length"
}
function indexes() {
echo $(seq 0 $(($1-1)))
}
function get() {
echo "$1" | jq ".$3[$2]"
}
function getDBName() {
echo "$1" | jq '.name' | sed -e "s/[\"]//g"
}
function getTable() {
echo $(get "$1" $2 'tables')
}
function getTableName() {
echo "$1" | jq '.name' | sed -e "s/[\"]//g"
}
function getTableFields() {
echo "$1" | jq '.fields'
}
function getTableConditions() {
echo "$1" | jq '.conditions'
}
dbs=$(cat /targets/$TARGET_FILE)
dbCount=$(getLength "$dbs")
for dbIndex in $(indexes $dbCount); do
db=$(get "$dbs" $dbIndex)
dbName=$(getDBName "$db")
tableCount=$(getLength "$db" 'tables')
for tableIndex in $(indexes $tableCount); do
table=$(getTable "$db" $tableIndex)
tableName=$(getTableName "$table")
fields=$(getTableFields "$table")
fields=${fields:1:-1}
if [ "$fields" = "" ] || [ "$fields" = "ul" ]; then
fields="*"
fi
conditions=$(getTableConditions "$table")
conditions=${conditions:1:-1}
if [ "$conditions" = "" ] || [ "$conditions" = "ul" ]; then
conditions="1"
fi
dump $dbName $tableName "$fields" "$conditions"
done
done