This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.sh
129 lines (117 loc) · 4.06 KB
/
database.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#****c* skn-bashlib/database
# NAME
# Database Functions
#***
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#****f* database/sqlQuery
# DESCRIPTION
# Executes a single sql query. It sets a file containing
# the query and executes it with sqlImport.
#***
function sqlQuery {
requiredArgs $# 7 $FUNCNAME
local type=${1}
local query=${2}
local output=${3}
local host=${4}
local user=${5}
local password=${6}
local name=${7}
local remove="true"
# checks
requiredVar "${type}" "$FUNCNAME: provide a database type"
requiredVar "${query}" "$FUNCNAME: query should not be empty"
local queryfile=$(mktemp)
echo ${query} > ${queryfile}
sqlImport "${type}" "${queryfile}" "${output}" "${host}" "${user}" "${password}" "${name}" "${remove}"
}
#****f* database/sqlImport
# DESCRIPTION
# Executes an sql file.
#***
function sqlImport {
requiredArgs $# 7 $FUNCNAME
local type=${1}
local input=${2}
local output=${3}
local host=$(setOrDefault "${4}" "localhost")
local user=${5}
local password=${6}
local name=${7}
local remove=$(setOrDefault "${8}" "false")
# checks and defaults
requiredVar "${type}" "$FUNCNAME: provide a database type"
[ ! -r ${input} ] && msgError "input file not readable: ${input}" && exit 1
[ -z ${output} ] && msgWarning "no output file defined, using /dev/null" && output="/dev/null"
requiredWrite "${output}" "$FUNCNAME: output file not writable: ${output}"
requiredVar "${name}" "$FUNCNAME: a database name is required"
case ${type} in
"mysql")
[ ! -z ${user} ] && user="-u ${user}"
[ ! -z ${password} ] && password="-p${password}"
mysql -h ${host} ${user} ${password} ${name} < ${input} > ${output} 2>&1 ;;
"postgresql")
# no password yet, test with .pgpass file
[ ! -z ${user} ] && user="-U ${user}"
psql -h ${host} ${user} -d ${name} < ${input} > ${output} 2>&1 ;;
"sqlite")
cat ${input} | sqlite3 ${name} > ${output} 2>&1 ;;
*)
msgError "database type not recognized: ${type}" && exit 1
esac
checkTrue "${remove}" && rm ${input}
}
#****f* database/sqlDump
# DESCRIPTION
# Dumps an entire database into an SQL file
#***
function sqlDump {
requiredArgs $# 6 $FUNCNAME
local type=${1}
local output=${2}
local host=$(setOrDefault "${3}" "localhost")
local user=${4}
local password=${5}
local name=${6}
local zip=$(setOrDefault "${7}" "")
local ext=""
requiredVar "${type}" "$FUNCNAME: provide a database type"
requiredVar "${name}" "$FUNCNAME: a database name is required"
requiredVar "${output}" "$FUNCNAME: dumping requires an output file"
requiredWrite "${output}" "$FUNCNAME: output file not writable: ${output}"
case ${zip} in
"gz")
local zip="| gzip "
local ext=".gz";;
"bz")
local zip="| bzip2 "
local ext=".bz";;
"xz")
local zip="| xz "
local ext=".xz";;
esac
case ${type} in
"mysql")
[ ! -z ${user} ] && user="-u${user}"
[ ! -z ${password} ] && password="-p{password}"
mysqldump -h${host} ${user} ${password} ${name} ${zip}> ${output}${ext} 2>&1 ;;
"postgresql")
[ ! -z ${user} ] && user="-U ${user}"
[ ! -z ${password} ] && password="-W{password}"
pg_dump -h ${host} ${user} ${password} ${name} ${zip}> ${output}${ext} 2>&1 ;;
"sqlite")
sqlite3 ${name} .dump ${zip}> ${output}${ext} 2>&1 ;;
*)
msgError "database type not recognized: ${type}" && exit 1
esac
}