File: instance-sqlite

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (87 lines) | stat: -rwxr-xr-x 1,824 bytes parent folder | download | duplicates (3)
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
#!/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) |
            .db_config.use_wal=true
    ' -i "${CONFIG_YAML}"
}

[[ $# -lt 1 ]] && about

case "$1" in
    config-yaml)
        config_yaml
        ;;
    setup)
        ;;
    dump)
        shift
        dump "$@"
        ;;
    restore)
        shift
        restore "$@"
        ;;
    exec_sql)
        shift
        exec_sql "$@"
        ;;
    *)
        about
        ;;
esac;