-
Notifications
You must be signed in to change notification settings - Fork 1
/
invenio-sql-command.bash
executable file
·104 lines (87 loc) · 1.88 KB
/
invenio-sql-command.bash
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
#!/bin/bash
APP_NAME="$(basename "$0")"
#
# This script can bring invenio-rdm down in an orderly fashion
# or start it back up.
#
function usage() {
cat <<EOT
% ${APP_NAME}() ${APP_NAME} user manual
% R. S. Doiel
% August 17, 2022
# NAME
${APP_NAME}
# SYNOPSIS
${APP_NAME} CONTAINER_NAME BACKUP_DIR
# DESCRIPTION
Dump the Postgres databases for CONTAINER_NAME into BACKUP_DIR.
CONTAINER_NAME is the name of your Invenio instance and will be
used as the prefix to the backup up filename.
# EXAMPLES
Backup the Postgres running in 'caltechdata_db_1' and write them
to '/var/backups/postgres'.
~~~shell
${APP_NAME} caltechauthors_db_1 '\dt'
bash invenio-sql-command.bash caltechauthors_db_1 'SELECT json from vocabularies_metadata;'
~~~
EOT
}
function run_command() {
DOCKER="$1"
CONTAINER="$2"
COMMAND="$3"
if [ "${CONTAINER}" = "" ]; then
echo "Missing the container name"
exit 1
fi
$DOCKER container exec \
"${CONTAINER}" /usr/bin/psql \
--username="${DB_USERNAME}" \
-d "${DB_NAME}" -c "${COMMAND}"
}
function run_all() {
#
# Sanity check our requiremented environment
#
SCRIPTNAME="$(readlink -f "$0")"
DNAME="$(dirname "${SCRIPTNAME}")"
cd "${DNAME}" || exit 1
# Source the file "postgres_env.cfg" it contains the
# value $DB_USERNAME.
if [ -f postgres_env.cfg ]; then
. postgres_env.cfg
fi
if [ "$DB_NAME" = "" ]; then
echo "The environment variable DB_NAME is not set."
exit 1
fi
if [ "$DB_USERNAME" = "" ]; then
echo "The environment variable DB_USERNAME is not set."
exit 1
fi
DOCKER="/usr/bin/docker"
if [ ! -f "${DOCKER}" ]; then
DOCKER=$(which docker)
fi
if [ "${DOCKER}" = "" ]; then
echo "Cannot find docker program, aborting"
exit 1
fi
run_command "$DOCKER" "$1" "$2"
}
#
# Main entry script point.
#
case "$1" in
h | help | -h | --help)
usage
exit 0
;;
*)
if [ "$1" = "" ]; then
usage
exit 1
fi
run_all "$1" "$2"
;;
esac