-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathkibana-export-data-view.sh
More file actions
executable file
·69 lines (56 loc) · 1.75 KB
/
kibana-export-data-view.sh
File metadata and controls
executable file
·69 lines (56 loc) · 1.75 KB
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
#!/bin/bash
# Fail on any error
set -e
# Load variables
source .env
# Check required environment variables
if [[ -z "$KIBANA_USERNAME" || -z "$KIBANA_PASSWORD" ]]; then
echo "Error: KIBANA_USERNAME and KIBANA_PASSWORD environment variables must be set."
exit 1
fi
# Default Kibana URL if not set
KIBANA_URL="${KIBANA_URL:-http://localhost:5601}"
# Output directory
EXPORT_DIR="data-views"
mkdir -p "$EXPORT_DIR"
# Usage check
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <data_view_id>"
exit 1
fi
DATA_VIEW_ID="$1"
# Fetch the data view to get its name
RESPONSE=$(curl --user "$KIBANA_USERNAME:$KIBANA_PASSWORD" \
--silent --show-error \
--request GET "$KIBANA_URL/api/saved_objects/index-pattern/$DATA_VIEW_ID" \
--header "kbn-xsrf: true")
DATA_VIEW_NAME=$(echo "$RESPONSE" | jq -r '.attributes.name')
if [[ "$DATA_VIEW_NAME" == "null" || -z "$DATA_VIEW_NAME" ]]; then
echo "Error: Could not retrieve data view name for ID '$DATA_VIEW_ID'."
exit 2
fi
# Sanitize the name for use as a filename
SAFE_NAME=$(echo "$DATA_VIEW_NAME" | tr -cd '[:alnum:]._-')
OUTFILE="$EXPORT_DIR/${SAFE_NAME}.ndjson"
# Export the data view using the Saved Objects API
curl --user "$KIBANA_USERNAME:$KIBANA_PASSWORD" \
--silent --show-error \
--request POST "$KIBANA_URL/api/saved_objects/_export" \
--header "kbn-xsrf: true" \
--header "Content-Type: application/json" \
--data-raw '{
"objects": [
{
"type": "index-pattern",
"id": "'"$DATA_VIEW_ID"'"
}
],
"includeReferencesDeep": false
}' \
--output "$OUTFILE"
if [[ $? -eq 0 ]]; then
echo "Data view exported to $OUTFILE"
else
echo "Failed to export data view: $DATA_VIEW_ID"
exit 3
fi