| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -eu |
| 4 |
script_name=$0 |
| 5 |
DB_BACKEND=$(echo "${script_name}" | cut -d- -f2) |
| 6 |
export DB_BACKEND |
| 7 |
|
| 8 |
die() { |
| 9 |
echo >&2 "$@" |
| 10 |
exit 1 |
| 11 |
} |
| 12 |
|
| 13 |
PGHOST=${PGHOST:-127.0.0.1} |
| 14 |
PGPORT=${PGPORT:-5432} |
| 15 |
PGPASSWORD=${PGPASSWORD:-postgres} |
| 16 |
PGUSER=${PGUSER:-postgres} |
| 17 |
export PGHOST |
| 18 |
export PGPORT |
| 19 |
export PGPASSWORD |
| 20 |
export PGUSER |
| 21 |
|
| 22 |
about() { |
| 23 |
die "usage: ${script_name} [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]" |
| 24 |
} |
| 25 |
|
| 26 |
check_requirements() { |
| 27 |
if ! command -v psql >/dev/null; then |
| 28 |
die "missing required program 'psql' as a postgres client (package postgressql-client on debian like system)" |
| 29 |
fi |
| 30 |
if ! command -v pg_dump >/dev/null; then |
| 31 |
die "missing required program 'pg_dump' (package postgresql-client on debian like system)" |
| 32 |
fi |
| 33 |
if ! command -v pg_restore >/dev/null; then |
| 34 |
die "missing required program 'pg_restore' (package postgresql-client on debian like system)" |
| 35 |
fi |
| 36 |
} |
| 37 |
|
| 38 |
exec_sql() { |
| 39 |
cmd="${1?Missing required sql command}" |
| 40 |
psql <<< "${cmd}" |
| 41 |
} |
| 42 |
|
| 43 |
setup() { |
| 44 |
exec_sql "DROP DATABASE IF EXISTS crowdsec_test;" |
| 45 |
exec_sql "CREATE DATABASE crowdsec_test;" |
| 46 |
exec_sql "DROP USER IF EXISTS crowdsec_test;" |
| 47 |
exec_sql "CREATE USER crowdsec_test WITH ENCRYPTED PASSWORD 'crowdsec_test';" |
| 48 |
exec_sql "GRANT ALL PRIVILEGES ON DATABASE crowdsec_test TO crowdsec_test;" |
| 49 |
} |
| 50 |
|
| 51 |
dump() { |
| 52 |
backup_file="${1?Missing file to backup database to}" |
| 53 |
pg_dump -Ft --dbname crowdsec_test --clean --create --file "${backup_file}" |
| 54 |
} |
| 55 |
|
| 56 |
restore() { |
| 57 |
backup_file="${1?missing file to restore database from}" |
| 58 |
[[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist" |
| 59 |
pg_restore --dbname crowdsec_test --clean "${backup_file}" |
| 60 |
} |
| 61 |
|
| 62 |
config_yaml() { |
| 63 |
yq e ' |
| 64 |
.db_config.type=strenv(DB_BACKEND)| |
| 65 |
.db_config.user="crowdsec_test" | |
| 66 |
.db_config.password="crowdsec_test" | |
| 67 |
.db_config.db_name="crowdsec_test" | |
| 68 |
.db_config.host=strenv(PGHOST) | |
| 69 |
.db_config.port=env(PGPORT) | |
| 70 |
.db_config.sslmode="disable" | |
| 71 |
del(.db_config.db_path) |
| 72 |
' -i "${CONFIG_YAML}" |
| 73 |
} |
| 74 |
|
| 75 |
[[ $# -lt 1 ]] && about |
| 76 |
|
| 77 |
check_requirements |
| 78 |
|
| 79 |
case "$1" in |
| 80 |
setup) |
| 81 |
setup |
| 82 |
;; |
| 83 |
config-yaml) |
| 84 |
config_yaml |
| 85 |
;; |
| 86 |
dump) |
| 87 |
shift |
| 88 |
dump "$@" |
| 89 |
;; |
| 90 |
restore) |
| 91 |
shift |
| 92 |
restore "$@" |
| 93 |
;; |
| 94 |
exec_sql) |
| 95 |
shift |
| 96 |
exec_sql "$@" |
| 97 |
;; |
| 98 |
*) |
| 99 |
about |
| 100 |
;; |
| 101 |
esac; |