2022-03-18 09:13:12 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
script_name=$0
|
2022-06-13 19:54:47 +00:00
|
|
|
DB_BACKEND=$(echo "${script_name}" | cut -d- -f2)
|
2022-03-28 08:41:32 +00:00
|
|
|
export DB_BACKEND
|
2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
die() {
|
|
|
|
echo >&2 "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
about() {
|
2022-06-13 19:54:47 +00:00
|
|
|
die "usage: ${script_name} [ config-yaml | setup | dump <backup_file> | restore <backup_file> ]"
|
2022-03-18 09:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#shellcheck disable=SC1007
|
|
|
|
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
2022-03-21 14:51:05 +00:00
|
|
|
cd "${THIS_DIR}"/../../
|
|
|
|
#shellcheck disable=SC1091
|
2022-03-18 09:13:12 +00:00
|
|
|
. ./.environment.sh
|
|
|
|
|
2022-04-13 13:44:23 +00:00
|
|
|
exec_sql() {
|
|
|
|
sqlite3 "${DB_FILE}" "$@"
|
|
|
|
}
|
|
|
|
|
2022-04-19 09:41:50 +00:00
|
|
|
setup() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
|
|
|
dump() {
|
|
|
|
backup_file="${1?Missing file to backup database to}"
|
|
|
|
# dirty fast cp. nothing should be accessing it right now, anyway.
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ -f "${DB_FILE}" ]] || die "missing file ${DB_FILE}"
|
|
|
|
cp "${DB_FILE}" "${backup_file}"
|
2022-04-19 09:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restore() {
|
|
|
|
backup_file="${1?missing file to restore database from}"
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
|
|
|
|
cp "${backup_file}" "${DB_FILE}"
|
2022-04-19 09:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
# you have not removed set -u above, have you?
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ -z "${CONFIG_YAML-}" ]] && die "\$CONFIG_YAML must be defined."
|
2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-04-19 09:25:27 +00:00
|
|
|
# In most cases this is called with setup argument, and it shouldn't fail for missing config file.
|
2022-06-13 19:54:47 +00:00
|
|
|
if [[ -f "${CONFIG_YAML}" ]]; then
|
2022-06-22 08:00:51 +00:00
|
|
|
DATA_DIR=$(yq e '.config_paths.data_dir' "${CONFIG_YAML}")
|
2022-04-15 13:39:17 +00:00
|
|
|
DB_FILE="${DATA_DIR}/crowdsec.db"
|
2022-06-13 19:54:47 +00:00
|
|
|
export DB_FILE
|
2022-04-15 13:39:17 +00:00
|
|
|
fi
|
2022-03-18 09:13:12 +00:00
|
|
|
|
2022-03-28 08:41:32 +00:00
|
|
|
config_yaml() {
|
2022-04-20 13:05:34 +00:00
|
|
|
yq e '
|
2022-03-30 09:33:26 +00:00
|
|
|
.db_config.type=strenv(DB_BACKEND) |
|
2022-10-31 09:02:51 +00:00
|
|
|
.db_config.db_path=strenv(DB_FILE) |
|
|
|
|
.db_config.use_wal=true
|
2022-03-28 08:41:32 +00:00
|
|
|
' -i "${CONFIG_YAML}"
|
|
|
|
}
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ $# -lt 1 ]] && about
|
2022-03-28 08:41:32 +00:00
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
case "$1" in
|
2022-03-21 14:51:05 +00:00
|
|
|
config-yaml)
|
2022-10-31 09:02:51 +00:00
|
|
|
config_yaml
|
2022-03-18 16:36:51 +00:00
|
|
|
;;
|
|
|
|
setup)
|
|
|
|
;;
|
2022-03-21 14:51:05 +00:00
|
|
|
dump)
|
2022-04-19 09:41:50 +00:00
|
|
|
shift
|
|
|
|
dump "$@"
|
2022-03-18 09:13:12 +00:00
|
|
|
;;
|
|
|
|
restore)
|
2022-04-19 09:41:50 +00:00
|
|
|
shift
|
|
|
|
restore "$@"
|
2022-03-18 09:13:12 +00:00
|
|
|
;;
|
2022-04-13 13:44:23 +00:00
|
|
|
exec_sql)
|
|
|
|
shift
|
|
|
|
exec_sql "$@"
|
|
|
|
;;
|
2022-03-18 09:13:12 +00:00
|
|
|
*)
|
|
|
|
about
|
|
|
|
;;
|
|
|
|
esac;
|