-
Notifications
You must be signed in to change notification settings - Fork 0
/
psql-backup
executable file
·92 lines (79 loc) · 2.42 KB
/
psql-backup
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
#!/bin/sh
set -e
set -u
fn_help() { (
echo "psql-backup v1.0.0 - creates portable (across instances) SQL schema & data backups"
echo ""
echo "USAGE"
echo " psql-backup <user> [host] [port] [dbname]"
echo ""
echo "EXAMPLE"
echo " psql-backup 'foobar-xxxxxx' 'pg-1.example.com' 5432 'foobar-xxxxxx'"
echo ""
echo "NOTES"
echo " - the username and database name should typically be the same"
echo ""
echo "COPYING"
echo " Copyright (c) 2024 AJ ONeal <[email protected]>"
echo " Licensed under the MPL-2.0"
); }
fn_pg_dump_parts() { (
b_user="${1}"
b_host="${2}"
b_port="${3}"
b_db="${4}"
# Note: schema includes extensions such as htstore and uuid
pg_dump --no-privileges --no-owner --schema-only --clean \
--username "${b_user}" --no-password --host "${b_host}" --port "${b_port}" \
-f ./"${b_db}".schema.drop.sql "${b_db}" >&2
echo ./"${b_db}".schema.drop.sql
pg_dump --no-privileges --no-owner --schema-only \
--username "${b_user}" --no-password --host "${b_host}" --port "${b_port}" \
-f ./"${b_db}".schema.sql "${b_db}" >&2
echo ./"${b_db}".schema.sql
pg_dump --no-privileges --no-owner --data-only \
--username "${b_user}" --no-password --host "${b_host}" --port "${b_port}" \
-f ./"${b_db}".data.sql "${b_db}" >&2
echo ./"${b_db}".data.sql
); }
# fn_pg_dump_full() { (
# b_user="${1}"
# b_host="${2}"
# b_port="${3}"
# b_db="${4}"
# pg_dump --no-privileges --no-owner \
# --username "${b_user}" --no-password \
# -f ./"${b_user}".full.sql "${b_user}" >&2
# ); }
# fn_pg_dumpall() { (
# b_user="${1}"
# b_host="${2}"
# b_port="${3}"
# b_db="${4}"
# pg_dumpall --no-privileges --globals-only \
# --username "${b_user}" --no-password --host "${b_host}" --port "${b_port}" \
# -f ./"${b_user}".globals.sql --dbname "${b_db}" >&2
# ); }
main() { (
case ${1:-} in
--help | help)
fn_help
return 0
;;
-V | --version | version)
fn_help
return 0
;;
"")
fn_help >&2
return 1
;;
*) ;;
esac
b_pguser="${1:-}"
b_pghost="${2:-localhost}"
b_pgport="${3:-5432}"
b_pgdb="${4:-$b_pguser}"
fn_pg_dump_parts "${b_pguser}" "${b_pghost}" "${b_pgport}" "${b_pgdb}"
); }
main "${@:-}"