forked from bognix/sandbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_prod_sqlite.sh
More file actions
executable file
·64 lines (49 loc) · 1.4 KB
/
copy_prod_sqlite.sh
File metadata and controls
executable file
·64 lines (49 loc) · 1.4 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
#!/usr/bin/env bash
# Finds the first pod in namespace "dev" (context "kube-sjc-dev") whose name
# contains "sandbot", then copies /usr/sandbot/sandbot.sqlite from that pod
# to a local file. The local destination path can be passed as an argument;
# it defaults to ./sandbot-prod.sqlite if not provided.
#
set -euo pipefail
DEFAULT_DEST="./sandbot-prod.sqlite"
usage() {
cat <<EOF
Usage: ${0##*/} [DEST_PATH]
If DEST_PATH is not specified, defaults to ${DEFAULT_DEST}.
Examples:
${0##*/}
${0##*/} ./data.sqlite
EOF
exit 1
}
if [[ "${1-}" == "-h" || "${1-}" == "--help" ]]; then
usage
fi
if [[ "$#" -gt 1 ]]; then
echo "Error: Too many arguments." >&2
usage
fi
DEST_PATH="${1:-$DEFAULT_DEST}"
KUBE_CONTEXT="kube-sjc-dev"
NAMESPACE="dev"
SEARCH_PATTERN="sandbot"
pod_name=$(
kubectl get pods \
--context="${KUBE_CONTEXT}" \
--namespace="${NAMESPACE}" \
--no-headers \
| grep "${SEARCH_PATTERN}" \
| awk '{print $1}' \
| head -n 1
)
if [[ -z "$pod_name" ]]; then
echo "Error: No pod found matching pattern \"${SEARCH_PATTERN}\" in namespace \"${NAMESPACE}\" (context \"${KUBE_CONTEXT}\")" >&2
exit 2
fi
echo "Found pod: ${pod_name}"
echo "Copying /usr/sandbot/sandbot.sqlite from pod ${pod_name} to ${DEST_PATH}..."
kubectl cp \
--context="${KUBE_CONTEXT}" \
"${NAMESPACE}/${pod_name}:/usr/sandbot/sandbot.sqlite" \
"${DEST_PATH}"
echo "Done. File saved to ${DEST_PATH}."