1 |
9014
|
aaronmk
|
#!/bin/bash -e
|
2 |
|
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3 |
13185
|
aaronmk
|
.rel sync.sh
|
4 |
9014
|
aaronmk
|
|
5 |
|
|
if self_not_included; then
|
6 |
|
|
|
7 |
|
|
# using prefixed connection vars
|
8 |
9641
|
aaronmk
|
alias ssh2local='declare prefix=ssh_; import_vars; unset prefix
|
9 |
|
|
declare $(prefix=ssh_ get_prefix_vars); unset $(prefix=ssh_ get_prefix_vars)'
|
10 |
9464
|
aaronmk
|
alias use_local='declare prefix=local_; import_vars; unset prefix'
|
11 |
|
|
alias use_remote='declare prefix=remote_; import_vars; unset prefix'
|
12 |
11784
|
aaronmk
|
alias use_local_remote='{ use_remote; use_local; }'
|
13 |
9167
|
aaronmk
|
# *must* be run inside a function
|
14 |
9574
|
aaronmk
|
alias use_root='declare prefix=root_; import_vars; unset prefix'
|
15 |
9014
|
aaronmk
|
|
16 |
|
|
quote='"'
|
17 |
|
|
|
18 |
9074
|
aaronmk
|
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
19 |
9014
|
aaronmk
|
|
20 |
9423
|
aaronmk
|
mk_esc_name_alias() # usage: mk_esc_name_alias schema_esc
|
21 |
9426
|
aaronmk
|
{ alias mk_"$1"='declare '"$1"'="${'"$1"':-$(esc_name "$'"${1%_esc}"'")}"; '\
|
22 |
|
|
'echo_vars '"$1"; }
|
23 |
9014
|
aaronmk
|
|
24 |
9423
|
aaronmk
|
mk_esc_name_alias schema_esc
|
25 |
|
|
mk_esc_name_alias table_esc
|
26 |
|
|
|
27 |
9470
|
aaronmk
|
fi # load new aliases
|
28 |
|
|
if self_being_included; then
|
29 |
|
|
|
30 |
9623
|
aaronmk
|
skip_table() # usage: create_table || table=... skip_table || return 0
|
31 |
|
|
{ what="table $table" already_exists_msg; }
|
32 |
|
|
|
33 |
9817
|
aaronmk
|
: "${test=$(isset limit; exit2bool)}" # test mode when using limited # rows
|
34 |
|
|
|
35 |
9698
|
aaronmk
|
# usage: (set_large_table; to_file db_cmd...) || return
|
36 |
|
|
alias set_large_table='declare del="${del-$(test "$limit"; exit2bool)}"
|
37 |
|
|
echo_vars del'
|
38 |
|
|
|
39 |
11578
|
aaronmk
|
limit() # usage: "query... $([prefix=$'| |\n'] [limit|n=#] limit)"
|
40 |
9467
|
aaronmk
|
{
|
41 |
|
|
echo_func; kw_params prefix; local prefix="${prefix-
|
42 |
|
|
}"
|
43 |
11578
|
aaronmk
|
if isset n; then local limit="${limit-$n}"; fi
|
44 |
9467
|
aaronmk
|
echo -n "${limit:+${prefix}LIMIT $limit}"
|
45 |
|
|
}
|
46 |
|
|
|
47 |
10401
|
aaronmk
|
cols2list() # usage: cols2list col...
|
48 |
|
|
{ echo_func; cmd=esc_name foreach_arg; delim=', ' join "$@"; }
|
49 |
|
|
|
50 |
10827
|
aaronmk
|
mk_select() # usage: {query=... | table=... [cols=...] [filter=...] \
|
51 |
11579
|
aaronmk
|
# [order_by=...]} [limit|n=#] mk_select
|
52 |
9468
|
aaronmk
|
{
|
53 |
|
|
echo_func; kw_params query table cols filter; mk_table_esc
|
54 |
10827
|
aaronmk
|
if is_array cols ; then cols="$( cols2list "${cols[@]}" )"; fi
|
55 |
|
|
if is_array order_by; then order_by="$(cols2list "${order_by[@]}")"; fi
|
56 |
9468
|
aaronmk
|
echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
|
57 |
9335
|
aaronmk
|
}FROM $table_esc
|
58 |
|
|
${filter:+WHERE $filter
|
59 |
10827
|
aaronmk
|
}${order_by:+ORDER BY $order_by
|
60 |
9467
|
aaronmk
|
}}")\
|
61 |
|
|
$(limit)"
|
62 |
9468
|
aaronmk
|
}
|
63 |
9088
|
aaronmk
|
|
64 |
9468
|
aaronmk
|
# export func usage: export_func() { ...; mk_select_var; ... }
|
65 |
|
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
|
66 |
|
|
# cmd line usage: [limit=...] caller
|
67 |
|
|
alias mk_select_var='declare query="$(mk_select)"'
|
68 |
|
|
|
69 |
9014
|
aaronmk
|
fi # load new aliases
|
70 |
|
|
if self_being_included; then
|
71 |
|
|
|
72 |
9482
|
aaronmk
|
mk_drop() # usage: table=... mk_drop|*sql_ANSI
|
73 |
|
|
{
|
74 |
12828
|
aaronmk
|
log_local; log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
75 |
9482
|
aaronmk
|
echo "DROP TABLE IF EXISTS $table_esc"
|
76 |
|
|
}
|
77 |
|
|
|
78 |
9465
|
aaronmk
|
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
|
79 |
9432
|
aaronmk
|
{
|
80 |
12828
|
aaronmk
|
log_local; log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
81 |
9432
|
aaronmk
|
echo "TRUNCATE $table_esc"
|
82 |
|
|
}
|
83 |
9014
|
aaronmk
|
|
84 |
9432
|
aaronmk
|
|
85 |
9014
|
aaronmk
|
### MySQL
|
86 |
|
|
|
87 |
9094
|
aaronmk
|
alias set_database=\
|
88 |
|
|
'if test "$schema"; then declare database="${database-$schema}"; fi'
|
89 |
|
|
|
90 |
|
|
fi # load new aliases
|
91 |
|
|
if self_being_included; then
|
92 |
|
|
|
93 |
9099
|
aaronmk
|
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
|
94 |
10888
|
aaronmk
|
# caller usage: [ssh_server=...] [server=...] [user=...] [password=...] mysql*()
|
95 |
9099
|
aaronmk
|
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
|
96 |
9271
|
aaronmk
|
# auto-adds connection/login opts when specified
|
97 |
9014
|
aaronmk
|
{
|
98 |
|
|
echo_func
|
99 |
9099
|
aaronmk
|
local cmd="$1"; shift
|
100 |
9014
|
aaronmk
|
local ssh_server="$(localize_url "$ssh_server")"
|
101 |
|
|
local server="$(localize_url "$server")"
|
102 |
9050
|
aaronmk
|
if test "$ssh_server"; then
|
103 |
9014
|
aaronmk
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
104 |
|
|
fi
|
105 |
|
|
|
106 |
|
|
local var=ssh_dest; local_inv
|
107 |
9701
|
aaronmk
|
command "time" ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
|
108 |
9014
|
aaronmk
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
109 |
9694
|
aaronmk
|
${password+="$password"} --quick "$@" # --quick: don't buffer entire result
|
110 |
9014
|
aaronmk
|
}
|
111 |
9099
|
aaronmk
|
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
|
112 |
9014
|
aaronmk
|
|
113 |
9099
|
aaronmk
|
fi # load new aliases
|
114 |
|
|
if self_being_included; then
|
115 |
|
|
|
116 |
10887
|
aaronmk
|
function mysql() # usage: [database=...] [data_only=1] [log_queries=] mysql ...
|
117 |
9095
|
aaronmk
|
{
|
118 |
9578
|
aaronmk
|
echo_func; kw_params output_data data_only query_verbosity
|
119 |
9557
|
aaronmk
|
if test "$data_only"; then local output_data="${output_data-1}"; fi
|
120 |
9578
|
aaronmk
|
local log_queries="${log_queries-1}"
|
121 |
9095
|
aaronmk
|
set_database
|
122 |
9101
|
aaronmk
|
|
123 |
9557
|
aaronmk
|
set -- ${database:+--database="$database" }--local-infile=1 \
|
124 |
|
|
--${data_only:+skip-}column-names "$@"
|
125 |
9150
|
aaronmk
|
if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
|
126 |
9591
|
aaronmk
|
else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose --verbose --verbose }\
|
127 |
|
|
"$@" # --verbose*3: echo runtimes
|
128 |
9101
|
aaronmk
|
fi
|
129 |
9095
|
aaronmk
|
}
|
130 |
9014
|
aaronmk
|
|
131 |
9074
|
aaronmk
|
mysql_ANSI()
|
132 |
9014
|
aaronmk
|
{
|
133 |
|
|
echo_func
|
134 |
|
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
135 |
|
|
}
|
136 |
|
|
|
137 |
13168
|
aaronmk
|
# always use ANSI mode, to support "" identifiers. `` *are* still supported in
|
138 |
9706
|
aaronmk
|
# this mode, so it also works with SHOW CREATE TABLE output and dumpfiles.
|
139 |
|
|
alias mysql='mysql_ANSI'
|
140 |
|
|
|
141 |
9707
|
aaronmk
|
fi # load new aliases
|
142 |
|
|
if self_being_included; then
|
143 |
|
|
|
144 |
|
|
mysql_root() { echo_func; use_root; mysql "$@"; }
|
145 |
|
|
|
146 |
9777
|
aaronmk
|
mysql_truncate() { echo_func; mk_truncate|mysql; }
|
147 |
9466
|
aaronmk
|
|
148 |
9456
|
aaronmk
|
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
|
149 |
|
|
# without append=1, first ensures the table is empty
|
150 |
9430
|
aaronmk
|
{
|
151 |
|
|
echo_func
|
152 |
|
|
mk_table_esc
|
153 |
|
|
local mysql_load_data_format="${mysql_load_data_format-\
|
154 |
|
|
FIELDS TERMINATED BY ','
|
155 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
156 |
|
|
}"
|
157 |
|
|
|
158 |
9466
|
aaronmk
|
if test ! "$append"; then mysql_truncate; fi
|
159 |
9430
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
160 |
|
|
}"
|
161 |
9642
|
aaronmk
|
ssh2local # ssh does not tunnel nonstandard fds
|
162 |
9777
|
aaronmk
|
mysql "$@" 40<&0 <<EOF
|
163 |
9651
|
aaronmk
|
LOAD DATA LOCAL INFILE '/dev/fd/40'
|
164 |
9660
|
aaronmk
|
${append:+IGNORE }INTO TABLE $table_esc
|
165 |
9430
|
aaronmk
|
$mysql_load_data_format
|
166 |
|
|
IGNORE 1 LINES
|
167 |
|
|
EOF
|
168 |
|
|
}
|
169 |
|
|
|
170 |
9152
|
aaronmk
|
mysql_export() # does not support CSV
|
171 |
10887
|
aaronmk
|
# caller usage: [database=...] {query=... | table=... [cols=...] [filter=...]} \
|
172 |
|
|
# mysql_export
|
173 |
9348
|
aaronmk
|
# cmd line usage: [limit=...] caller
|
174 |
9102
|
aaronmk
|
{
|
175 |
|
|
echo_func
|
176 |
9463
|
aaronmk
|
mk_select_var
|
177 |
9152
|
aaronmk
|
|
178 |
9777
|
aaronmk
|
output_data=1 mysql "$@" <<<"$query"
|
179 |
9152
|
aaronmk
|
}
|
180 |
|
|
|
181 |
|
|
mysql_export_outfile() # supports CSV, but requires the FILE privilege
|
182 |
|
|
{
|
183 |
|
|
echo_func
|
184 |
9102
|
aaronmk
|
: "${file:?}"
|
185 |
9463
|
aaronmk
|
mk_select_var
|
186 |
9102
|
aaronmk
|
local mysql_load_data_format="${mysql_load_data_format-\
|
187 |
|
|
FIELDS TERMINATED BY ','
|
188 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
189 |
|
|
}"
|
190 |
|
|
|
191 |
|
|
local head="${query%%FROM*}" # includes trailing newline
|
192 |
9105
|
aaronmk
|
head="${head%
|
193 |
|
|
}"
|
194 |
9102
|
aaronmk
|
local tail="${query#$head}"
|
195 |
9106
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
196 |
|
|
}"
|
197 |
9777
|
aaronmk
|
mysql "$@" <<EOF
|
198 |
9105
|
aaronmk
|
$head
|
199 |
|
|
INTO OUTFILE '$file'
|
200 |
9106
|
aaronmk
|
$mysql_load_data_format
|
201 |
|
|
$tail
|
202 |
9102
|
aaronmk
|
EOF
|
203 |
|
|
}
|
204 |
|
|
|
205 |
13184
|
aaronmk
|
## backups
|
206 |
|
|
|
207 |
10649
|
aaronmk
|
mysqldump() # usage: [schema=1 | data=1] [create_db=1] mysqldump db [table...]
|
208 |
9014
|
aaronmk
|
{
|
209 |
10649
|
aaronmk
|
echo_func
|
210 |
|
|
echo_vars schema data create_db
|
211 |
9231
|
aaronmk
|
|
212 |
10649
|
aaronmk
|
local var=create_db; local_inv
|
213 |
|
|
mysql_cmd ${database:+--databases "$database" ${no_create_db:+--tables }} \
|
214 |
9694
|
aaronmk
|
--lock-tables=false --set-charset \
|
215 |
10446
|
aaronmk
|
${postgres_compat:+${data:+--compatible=postgresql }--add-locks=false }\
|
216 |
9014
|
aaronmk
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
217 |
|
|
}
|
218 |
|
|
|
219 |
9074
|
aaronmk
|
mysqldump_diffable()
|
220 |
9014
|
aaronmk
|
{
|
221 |
|
|
echo_func
|
222 |
9550
|
aaronmk
|
mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
|
223 |
9014
|
aaronmk
|
}
|
224 |
|
|
|
225 |
9588
|
aaronmk
|
mysql_rm_privileged_statements()
|
226 |
|
|
{
|
227 |
|
|
echo_func
|
228 |
|
|
grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
|
229 |
|
|
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
|
230 |
|
|
}
|
231 |
9017
|
aaronmk
|
|
232 |
9632
|
aaronmk
|
## permissions
|
233 |
9588
|
aaronmk
|
|
234 |
9632
|
aaronmk
|
mysql_seal_table() # usage: table=... user=... mysql_seal_table
|
235 |
|
|
# prevents further modifications to a table by a user
|
236 |
|
|
{
|
237 |
9634
|
aaronmk
|
echo_func; kw_params table user; : "${table:?}" "${user:?}"; mk_table_esc
|
238 |
9632
|
aaronmk
|
|
239 |
9635
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE ALL PRIVILEGES ON $table FROM '$user'@'%'" || true
|
240 |
9636
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE GRANT OPTION ON $table FROM '$user'@'%'" || true
|
241 |
9632
|
aaronmk
|
}
|
242 |
|
|
|
243 |
13184
|
aaronmk
|
## server admin
|
244 |
9632
|
aaronmk
|
|
245 |
13184
|
aaronmk
|
mysql_ctl() # usage: mysql_ctl {start|stop|restart|...}
|
246 |
13191
|
aaronmk
|
{
|
247 |
|
|
echo_func
|
248 |
|
|
pattern='^stop: Unknown instance:' ignore_e=1 ignore_err_msg \
|
249 |
|
|
sudo service mysql "$@" # ignore errors if not running
|
250 |
|
|
}
|
251 |
13184
|
aaronmk
|
|
252 |
13252
|
aaronmk
|
mysql_snapshot() # usage: [live=] [from=...] [to=...] mysql_snapshot
|
253 |
13185
|
aaronmk
|
{
|
254 |
13252
|
aaronmk
|
echo_func; kw_params from to; local_export live="${live-1}"
|
255 |
13186
|
aaronmk
|
local from="${from:-/var/lib/mysql}"; local to="${to:-$from.bak}"
|
256 |
13251
|
aaronmk
|
sudo mkdir "$to"
|
257 |
13185
|
aaronmk
|
|
258 |
|
|
mysql_ctl stop
|
259 |
13253
|
aaronmk
|
prep_try; local_dir="$from" remote_url="$to" inplace=1 "try" sudo upload
|
260 |
|
|
# inplace: for large files, don't re-copy entire file
|
261 |
13185
|
aaronmk
|
mysql_ctl start
|
262 |
13187
|
aaronmk
|
rethrow
|
263 |
13185
|
aaronmk
|
}
|
264 |
13184
|
aaronmk
|
|
265 |
13185
|
aaronmk
|
|
266 |
9014
|
aaronmk
|
### PostgreSQL
|
267 |
|
|
|
268 |
9648
|
aaronmk
|
alias use_pg='declare prefix=pg_; import_vars; unset prefix'
|
269 |
|
|
|
270 |
9649
|
aaronmk
|
fi # load new aliases
|
271 |
|
|
if self_being_included; then
|
272 |
|
|
|
273 |
10763
|
aaronmk
|
# usage: pg_*() { ...; pg_cmd "$@"; } (uses alias)
|
274 |
|
|
function pg_cmd() # usage for fn: pg_*() { ...; pg_cmd cmd "$@"; }
|
275 |
|
|
# auto-adds connection/login opts when specified
|
276 |
9646
|
aaronmk
|
{
|
277 |
13165
|
aaronmk
|
echo_func; kw_params as_root
|
278 |
9646
|
aaronmk
|
|
279 |
9649
|
aaronmk
|
use_pg
|
280 |
12828
|
aaronmk
|
log_local
|
281 |
10154
|
aaronmk
|
log- 2
|
282 |
11356
|
aaronmk
|
if test "$server"; then local PGHOST="$server"; export PGHOST; fi
|
283 |
|
|
if test "$user"; then local PGUSER="$user"; export PGUSER; fi
|
284 |
12846
|
aaronmk
|
if test "$password"; then local PGPASSWORD="$password"; log++ export PGPASSWORD;fi
|
285 |
11356
|
aaronmk
|
if test "$database"; then local PGDATABASE="$database"; export PGDATABASE;fi
|
286 |
9649
|
aaronmk
|
log+ 2
|
287 |
|
|
|
288 |
13183
|
aaronmk
|
time if test "$as_root"; then sudo -u postgres "$@"; else command "$@"; fi
|
289 |
10763
|
aaronmk
|
}
|
290 |
|
|
alias pg_cmd='"pg_cmd" "${FUNCNAME%%__*}"'
|
291 |
|
|
|
292 |
|
|
fi # load new aliases
|
293 |
|
|
if self_being_included; then
|
294 |
|
|
|
295 |
12739
|
aaronmk
|
psql() # usage: [stdin=copy_pstdin_file] [output_data=1] psql \
|
296 |
|
|
# <<<"cmds (eg. \copy from pstdin)"
|
297 |
11360
|
aaronmk
|
# SUDO_USER: when set, avoids outputting to /dev/fd/#, because this causes a
|
298 |
|
|
# "Permission denied" error when running as sudo on Linux
|
299 |
10763
|
aaronmk
|
{
|
300 |
12739
|
aaronmk
|
echo_func; kw_params stdin output_data
|
301 |
12740
|
aaronmk
|
local bypass_ok="$(! isset SUDO_USER; exit2bool)"; echo_vars bypass_ok
|
302 |
|
|
local verbose_="$(test "$bypass_ok" && can_log; exit2bool)"
|
303 |
12737
|
aaronmk
|
echo_vars verbose_
|
304 |
12740
|
aaronmk
|
local data_bypasses_filter="$(test "$output_data" -a "$bypass_ok";
|
305 |
12739
|
aaronmk
|
exit2bool)"; echo_vars data_bypasses_filter
|
306 |
10763
|
aaronmk
|
|
307 |
12737
|
aaronmk
|
if test "$verbose_"; then set -- --echo-all --echo-hidden "$@"; fi
|
308 |
9651
|
aaronmk
|
local redirs=("${redirs[@]}" '40<&0' "0<${stdin:-&20}" '41>&1')
|
309 |
9646
|
aaronmk
|
(
|
310 |
10295
|
aaronmk
|
# hide stack traces/DETAIL sections of error messages at verbosity <2
|
311 |
12829
|
aaronmk
|
if ! log++ can_log; then echo '\set VERBOSITY terse'; fi
|
312 |
12738
|
aaronmk
|
if test "$verbose_"; then echo '\timing on'; fi
|
313 |
|
|
echo "SET client_min_messages = \
|
314 |
|
|
$(if test "$verbose_"; then echo NOTICE; else echo WARNING; fi);"
|
315 |
9646
|
aaronmk
|
cat
|
316 |
12740
|
aaronmk
|
)|cmd_log_fd=${bypass_ok:+1} pg_cmd --file /dev/fd/40 \
|
317 |
12739
|
aaronmk
|
${data_bypasses_filter:+--output /dev/fd/41 }--set ON_ERROR_STOP=1 --quiet "$@"\
|
318 |
12731
|
aaronmk
|
|| verbosity_min=2 die_error_hidden
|
319 |
9646
|
aaronmk
|
# --output is for query *results*, not echoed statements
|
320 |
|
|
}
|
321 |
|
|
|
322 |
10755
|
aaronmk
|
pg_export() # usage: mk_select_opts... [pg_copy_format=CSV...] pg_export >out
|
323 |
9014
|
aaronmk
|
{
|
324 |
|
|
echo_func
|
325 |
9463
|
aaronmk
|
mk_select_var
|
326 |
9014
|
aaronmk
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
327 |
|
|
|
328 |
12739
|
aaronmk
|
output_data=1 psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
329 |
9014
|
aaronmk
|
}
|
330 |
|
|
|
331 |
9074
|
aaronmk
|
pg_header()
|
332 |
9014
|
aaronmk
|
{
|
333 |
|
|
echo_func
|
334 |
|
|
local pg_copy_format="CSV HEADER" limit=0
|
335 |
9083
|
aaronmk
|
pg_export "$@"|echo_stdout
|
336 |
9014
|
aaronmk
|
}
|
337 |
|
|
|
338 |
9074
|
aaronmk
|
pg_export_table_no_header()
|
339 |
9014
|
aaronmk
|
{
|
340 |
|
|
echo_func
|
341 |
|
|
local pg_copy_format="CSV"
|
342 |
9083
|
aaronmk
|
pg_export "$@"
|
343 |
9014
|
aaronmk
|
}
|
344 |
|
|
|
345 |
10825
|
aaronmk
|
pg_export_table_to_dir()
|
346 |
|
|
{
|
347 |
|
|
echo_func
|
348 |
|
|
local table="$1"; shift; mk_table_esc
|
349 |
|
|
stdout="$exports_dir/$table.csv" to_file pg_export "$@"
|
350 |
|
|
}
|
351 |
|
|
|
352 |
9074
|
aaronmk
|
pg_export_table_to_dir_no_header()
|
353 |
9014
|
aaronmk
|
{
|
354 |
|
|
echo_func
|
355 |
|
|
local table="$1"; shift; mk_table_esc
|
356 |
9476
|
aaronmk
|
stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
|
357 |
9042
|
aaronmk
|
pg_export_table_no_header "$@"
|
358 |
9014
|
aaronmk
|
}
|
359 |
|
|
|
360 |
13166
|
aaronmk
|
pg_dump() # usage: database={...|} [schema=...] [struct=1 | data=1] [owners=1] \
|
361 |
|
|
# [compress=1] [create_schema=] [users=1] pg_dump [opts...] >out
|
362 |
|
|
# database='': entire cluster
|
363 |
10764
|
aaronmk
|
{
|
364 |
13166
|
aaronmk
|
echo_func;kw_params database struct data owners compress create_schema users
|
365 |
|
|
: "${database?}"
|
366 |
10775
|
aaronmk
|
local create_schema="${create_schema-1}"
|
367 |
10764
|
aaronmk
|
|
368 |
|
|
# subset
|
369 |
|
|
if test "$schema"; then set -- --schema="\"$schema\"" "$@"; fi
|
370 |
13166
|
aaronmk
|
if test "$struct"; then set -- "$@" --schema-only; fi
|
371 |
|
|
if test "$data" ; then set -- "$@" --data-only ; fi
|
372 |
10764
|
aaronmk
|
|
373 |
13166
|
aaronmk
|
if test "$database"; then # just one DB
|
374 |
|
|
# format
|
375 |
|
|
if test ! "$owners"; then set -- "$@" --no-owner ; fi
|
376 |
|
|
if test "$compress"; then set -- "$@" --format=custom --compress=9
|
377 |
|
|
else set -- "$@" --format=plain --inserts
|
378 |
10774
|
aaronmk
|
fi
|
379 |
|
|
|
380 |
13166
|
aaronmk
|
(
|
381 |
|
|
# filter output
|
382 |
|
|
if test ! "$create_schema"; then
|
383 |
|
|
fd=1 redirs= filter_fd sed 's/^CREATE SCHEMA/--&/'
|
384 |
|
|
fi
|
385 |
|
|
|
386 |
|
|
pattern='^pg_dump: No matching tables were found$' ignore_err_msg \
|
387 |
12193
|
aaronmk
|
pg_cmd "$@"
|
388 |
13166
|
aaronmk
|
)
|
389 |
|
|
else # entire cluster
|
390 |
|
|
# subset
|
391 |
|
|
if test "$users"; then set -- "$@" --globals-only; fi
|
392 |
|
|
|
393 |
|
|
(cd /; as_root=1 "pg_cmd" pg_dumpall "$@")
|
394 |
|
|
fi
|
395 |
10764
|
aaronmk
|
}
|
396 |
|
|
|
397 |
12800
|
aaronmk
|
pg_schema_exists() # usage: schema=... pg_schema_exists
|
398 |
11855
|
aaronmk
|
{
|
399 |
|
|
echo_func; : "${schema:?}"; mk_schema_esc
|
400 |
12800
|
aaronmk
|
pattern='cannot create temporary relation in non-temporary schema' \
|
401 |
12829
|
aaronmk
|
ignore_e=3 log++ stderr_matches psql <<<"CREATE TEMP TABLE $schema_esc.t ()"
|
402 |
11855
|
aaronmk
|
}
|
403 |
|
|
|
404 |
11859
|
aaronmk
|
pg_require_schema() # usage: schema=... pg_require_schema
|
405 |
|
|
{
|
406 |
|
|
echo_func; : "${schema:?}"
|
407 |
12806
|
aaronmk
|
pg_schema_exists || die "schema $schema does not exist"
|
408 |
11859
|
aaronmk
|
}
|
409 |
|
|
|
410 |
12800
|
aaronmk
|
pg_table_exists() # usage: [schema=...] table=... pg_table_exists
|
411 |
11768
|
aaronmk
|
{
|
412 |
|
|
echo_func; : "${table:?}"; mk_table_esc
|
413 |
12865
|
aaronmk
|
! pattern='relation .* does not exist' \
|
414 |
12829
|
aaronmk
|
ignore_e=3 log++ stderr_matches psql <<<"SELECT NULL FROM $table_esc LIMIT 0"
|
415 |
11768
|
aaronmk
|
}
|
416 |
|
|
|
417 |
9014
|
aaronmk
|
fi
|