-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint.sh
51 lines (39 loc) · 1.7 KB
/
docker-entrypoint.sh
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
#!/bin/bash
set -eo pipefail
SECONDS=0
# required environment variables
: "${AWS_ACCESS_KEY_ID:?Please set the environment variable.}"
: "${AWS_REGION:?Please set the environment variable.}"
: "${AWS_S3_ENDPOINT:?Please set the environment variable.}"
: "${AWS_SECRET_ACCESS_KEY:?Please set the environment variable.}"
: "${CLICKHOUSE_QUERY:?Please set the environment variable.}"
# optional environment variables with defaults
AWS_S3_STORAGE_CLASS="${AWS_S3_STORAGE_CLASS:-STANDARD_IA}"
FILE_NAME="${FILE_NAME:-"%F_%T"}"
CLICKHOUSE_HOST="${CLICKHOUSE_HOST:-clickhouse}"
CLICKHOUSE_PORT="${CLICKHOUSE_PORT:-9000}"
# logic starts here
DATED_FILE_NAME=$(date +"${FILE_NAME}")
DATED_FILE_NAME_GZ="${DATED_FILE_NAME}.gz"
CLICKHOUSE_CMD="clickhouse-client --host=${CLICKHOUSE_HOST} --port=${CLICKHOUSE_PORT} --query='${CLICKHOUSE_QUERY}' --format=Native"
if [ -n "${CLICKHOUSE_DATABASE}" ]; then
CLICKHOUSE_CMD+=" --database=${CLICKHOUSE_DATABASE}"
fi
if [ -n "${CLICKHOUSE_PASSWORD}" ]; then
CLICKHOUSE_CMD+=" --password=${CLICKHOUSE_PASSWORD}"
fi
if [ -n "${CLICKHOUSE_USER}" ]; then
CLICKHOUSE_CMD+=" --user=${CLICKHOUSE_USER}"
fi
echo "Exporting the results for the query..."
eval "${CLICKHOUSE_CMD} | pigz --fast > ${DATED_FILE_NAME_GZ}"
echo "Exporting the results for the query... Done."
echo "Uploading to S3..."
rclone copyto \
--s3-no-check-bucket \
"./${DATED_FILE_NAME_GZ}" \
":s3,access_key_id=${AWS_ACCESS_KEY_ID},provider=AWS,region=${AWS_REGION},secret_access_key=${AWS_SECRET_ACCESS_KEY},storage_class=${AWS_S3_STORAGE_CLASS}:${AWS_S3_ENDPOINT}/${DATED_FILE_NAME_GZ}"
echo "Uploading to S3... Done."
if [ -n "${WEBGAZER_HEARTBEAT_URL}" ]; then
curl "${WEBGAZER_HEARTBEAT_URL}?seconds=${SECONDS}"
fi