12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #!/usr/bin/env bash
- set -eu
- script_name=$0
- DB_BACKEND=$(echo "${script_name}" | cut -d- -f2)
- export DB_BACKEND
- die() {
- echo >&2 "$@"
- exit 1
- }
- about() {
- die "usage: ${script_name} [ config-yaml | setup | dump <backup_file> | restore <backup_file> ]"
- }
- #shellcheck disable=SC1007
- THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
- cd "${THIS_DIR}"/../../
- #shellcheck disable=SC1091
- . ./.environment.sh
- exec_sql() {
- sqlite3 "${DB_FILE}" "$@"
- }
- setup() {
- :
- }
- dump() {
- backup_file="${1?Missing file to backup database to}"
- # dirty fast cp. nothing should be accessing it right now, anyway.
- [[ -f "${DB_FILE}" ]] || die "missing file ${DB_FILE}"
- cp "${DB_FILE}" "${backup_file}"
- }
- restore() {
- backup_file="${1?missing file to restore database from}"
- [[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
- cp "${backup_file}" "${DB_FILE}"
- }
- # you have not removed set -u above, have you?
- [[ -z "${CONFIG_YAML-}" ]] && die "\$CONFIG_YAML must be defined."
- # ---------------------------
- # In most cases this is called with setup argument, and it shouldn't fail for missing config file.
- if [[ -f "${CONFIG_YAML}" ]]; then
- DATA_DIR=$(yq e '.config_paths.data_dir' "${CONFIG_YAML}")
- DB_FILE="${DATA_DIR}/crowdsec.db"
- export DB_FILE
- fi
- config_yaml() {
- yq e '
- .db_config.type=strenv(DB_BACKEND) |
- .db_config.db_path=strenv(DB_FILE)
- ' -i "${CONFIG_YAML}"
- }
- [[ $# -lt 1 ]] && about
- case "$1" in
- config-yaml)
- ;;
- setup)
- ;;
- dump)
- shift
- dump "$@"
- ;;
- restore)
- shift
- restore "$@"
- ;;
- exec_sql)
- shift
- exec_sql "$@"
- ;;
- *)
- about
- ;;
- esac;
|