Revision 9014
Added by Aaron Marcuse-Kubitza over 11 years ago
lib/sh/local.sh | ||
---|---|---|
1 | 1 |
#!/bin/bash -e |
2 | 2 |
# links to locally-available resources |
3 | 3 |
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh |
4 |
. "$(dirname "${BASH_SOURCE[0]}")"/db.sh |
|
4 | 5 |
|
5 | 6 |
if self_not_included; then |
6 | 7 |
|
lib/sh/util.sh | ||
---|---|---|
315 | 315 |
|
316 | 316 |
localize_url () { test _"$1" = _"$(hostname -f)" || echo "$1"; } |
317 | 317 |
|
318 |
#### databases |
|
319 |
|
|
320 |
# using prefixed connection vars |
|
321 |
alias use_local='declare prefix=local_; import_vars' |
|
322 |
alias use_remote='declare prefix=remote_; import_vars' |
|
323 |
alias use_local_remote='use_local; use_remote' |
|
324 |
|
|
325 |
quote='"' |
|
326 |
|
|
327 |
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; } |
|
328 |
|
|
329 |
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; } |
|
330 |
|
|
331 |
alias mk_schema_esc='declare schema_esc; mk_esc_name schema' |
|
332 |
alias mk_table_esc='declare table_esc; mk_esc_name table' |
|
333 |
|
|
334 |
fi # load new aliases |
|
335 |
if self_being_included; then |
|
336 |
|
|
337 |
log_sql () { test "$verbosity" -ge 2; } |
|
338 |
|
|
339 |
### MySQL |
|
340 |
|
|
341 |
# auto-adds connection/login opts when specified |
|
342 |
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; } |
|
343 |
{ |
|
344 |
echo_func |
|
345 |
local ssh_server="$(localize_url "$ssh_server")" |
|
346 |
local server="$(localize_url "$server")" |
|
347 |
if test -n "$ssh_server"; then |
|
348 |
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}" |
|
349 |
fi |
|
350 |
if test -n "$schema"; then local database="${database-$schema}"; fi |
|
351 |
|
|
352 |
local var=ssh_dest; local_inv |
|
353 |
extern ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \ |
|
354 |
${server:+ --host="$server" }${user:+--user="$user" } --password\ |
|
355 |
${password+="$password"} ${database:+--databases "$database" --tables } "$@" |
|
356 |
} |
|
357 |
|
|
358 |
mysql () { echo_func; mysql_cmd --verbose "$@"; } |
|
359 |
|
|
360 |
mysql_ANSI () |
|
361 |
{ |
|
362 |
echo_func |
|
363 |
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@" |
|
364 |
} |
|
365 |
|
|
366 |
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...] |
|
367 |
{ |
|
368 |
echo_func |
|
369 |
mysql_cmd --quick --lock-tables=false --set-charset \ |
|
370 |
${postgres_compat:+--compatible=postgresql --add-locks=false }\ |
|
371 |
${schema:+--no-data }${data:+--no-create-info }"$@" |
|
372 |
} |
|
373 |
|
|
374 |
mysqldump_diffable () |
|
375 |
{ |
|
376 |
echo_func |
|
377 |
mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/' |
|
378 |
} |
|
379 |
|
|
380 |
### PostgreSQL |
|
381 |
|
|
382 |
pg_copy_to () |
|
383 |
{ |
|
384 |
echo_func |
|
385 |
if test -z "$source"; then |
|
386 |
: "${table:?}"; mk_table_esc |
|
387 |
if test -z "$limit"; then local source="$table_esc" |
|
388 |
else local source="(SELECT * FROM $table_esc LIMIT $limit)" |
|
389 |
fi |
|
390 |
fi |
|
391 |
local pg_copy_format="${pg_copy_format-CSV HEADER}" |
|
392 |
|
|
393 |
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;" |
|
394 |
} |
|
395 |
|
|
396 |
pg_header () |
|
397 |
{ |
|
398 |
echo_func |
|
399 |
local pg_copy_format="CSV HEADER" limit=0 |
|
400 |
pg_copy_to "$@"|echo_stdin |
|
401 |
} |
|
402 |
|
|
403 |
pg_export_table_no_header () |
|
404 |
{ |
|
405 |
echo_func |
|
406 |
local pg_copy_format="CSV" |
|
407 |
pg_copy_to "$@" |
|
408 |
} |
|
409 |
|
|
410 |
pg_export_table_to_dir_no_header () |
|
411 |
{ |
|
412 |
echo_func |
|
413 |
local table="$1"; shift; mk_table_esc |
|
414 |
local cols="$(pg_header)" |
|
415 |
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv" |
|
416 |
} |
|
417 |
|
|
418 | 318 |
fi |
lib/sh/db.sh | ||
---|---|---|
1 |
#!/bin/bash -e |
|
2 |
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh |
|
3 |
|
|
4 |
if self_not_included; then |
|
5 |
|
|
6 |
# using prefixed connection vars |
|
7 |
alias use_local='declare prefix=local_; import_vars' |
|
8 |
alias use_remote='declare prefix=remote_; import_vars' |
|
9 |
alias use_local_remote='use_local; use_remote' |
|
10 |
|
|
11 |
quote='"' |
|
12 |
|
|
13 |
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; } |
|
14 |
|
|
15 |
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; } |
|
16 |
|
|
17 |
alias mk_schema_esc='declare schema_esc; mk_esc_name schema' |
|
18 |
alias mk_table_esc='declare table_esc; mk_esc_name table' |
|
19 |
|
|
20 |
fi # load new aliases |
|
21 |
if self_being_included; then |
|
22 |
|
|
23 |
log_sql () { test "$verbosity" -ge 2; } |
|
24 |
|
|
25 |
### MySQL |
|
26 |
|
|
27 |
# auto-adds connection/login opts when specified |
|
28 |
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; } |
|
29 |
{ |
|
30 |
echo_func |
|
31 |
local ssh_server="$(localize_url "$ssh_server")" |
|
32 |
local server="$(localize_url "$server")" |
|
33 |
if test -n "$ssh_server"; then |
|
34 |
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}" |
|
35 |
fi |
|
36 |
if test -n "$schema"; then local database="${database-$schema}"; fi |
|
37 |
|
|
38 |
local var=ssh_dest; local_inv |
|
39 |
extern ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \ |
|
40 |
${server:+ --host="$server" }${user:+--user="$user" } --password\ |
|
41 |
${password+="$password"} ${database:+--databases "$database" --tables } "$@" |
|
42 |
} |
|
43 |
|
|
44 |
mysql () { echo_func; mysql_cmd --verbose "$@"; } |
|
45 |
|
|
46 |
mysql_ANSI () |
|
47 |
{ |
|
48 |
echo_func |
|
49 |
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@" |
|
50 |
} |
|
51 |
|
|
52 |
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...] |
|
53 |
{ |
|
54 |
echo_func |
|
55 |
mysql_cmd --quick --lock-tables=false --set-charset \ |
|
56 |
${postgres_compat:+--compatible=postgresql --add-locks=false }\ |
|
57 |
${schema:+--no-data }${data:+--no-create-info }"$@" |
|
58 |
} |
|
59 |
|
|
60 |
mysqldump_diffable () |
|
61 |
{ |
|
62 |
echo_func |
|
63 |
mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/' |
|
64 |
} |
|
65 |
|
|
66 |
### PostgreSQL |
|
67 |
|
|
68 |
pg_copy_to () |
|
69 |
{ |
|
70 |
echo_func |
|
71 |
if test -z "$source"; then |
|
72 |
: "${table:?}"; mk_table_esc |
|
73 |
if test -z "$limit"; then local source="$table_esc" |
|
74 |
else local source="(SELECT * FROM $table_esc LIMIT $limit)" |
|
75 |
fi |
|
76 |
fi |
|
77 |
local pg_copy_format="${pg_copy_format-CSV HEADER}" |
|
78 |
|
|
79 |
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;" |
|
80 |
} |
|
81 |
|
|
82 |
pg_header () |
|
83 |
{ |
|
84 |
echo_func |
|
85 |
local pg_copy_format="CSV HEADER" limit=0 |
|
86 |
pg_copy_to "$@"|echo_stdin |
|
87 |
} |
|
88 |
|
|
89 |
pg_export_table_no_header () |
|
90 |
{ |
|
91 |
echo_func |
|
92 |
local pg_copy_format="CSV" |
|
93 |
pg_copy_to "$@" |
|
94 |
} |
|
95 |
|
|
96 |
pg_export_table_to_dir_no_header () |
|
97 |
{ |
|
98 |
echo_func |
|
99 |
local table="$1"; shift; mk_table_esc |
|
100 |
local cols="$(pg_header)" |
|
101 |
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv" |
|
102 |
} |
|
103 |
|
|
104 |
fi |
|
0 | 105 |
Also available in: Unified diff
lib/sh/util.sh: split databases utils out into separate db.sh