forked from argussecurity/docker-cassandra2s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.sh
executable file
·62 lines (51 loc) · 1.29 KB
/
restore.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
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
set -e
# Environment variables
if [[ -z "$DB_USER" || -z "$DB_PASS" || -z "$INPUT_DIR" ]]; then
echo "ERROR: All required environment variables must be defined."
exit 1
fi
# Constants
BASE_PATH=${INPUT_DIR}
CQL_VERSION=${CQL_VERSION:-3.1.7}
IP=${IP:-$(hostname --ip)}
CLIENT_TIMEOUT=${CLIENT_TIMEOUT:-10}
function init_cqlsh {
mkdir ~/.cassandra
printf "[connection]\nclient_timeout = $CLIENT_TIMEOUT" > ~/.cassandra/cqlshrc
}
# Input:
# cqlsh command to execute
# Output: CQLSH_RET
function cqlsh_exec {
CQLSH_RET=$(cqlsh --cqlversion ${CQL_VERSION} -u ${DB_USER} -p ${DB_PASS} -e "$1" ${IP})
}
# Input:
# keyspace
# table
function import_table {
keyspace=$1
table=$2
echo "Importing table '$table'..."
cqlsh_exec "USE $keyspace; COPY $table FROM '$BASE_PATH/$keyspace-$table.csv' WITH PAGETIMEOUT=$CLIENT_TIMEOUT"
echo "Done importing table '$table'."
}
# Input:
# keyspace
# table (optional)
function import_db {
keyspace=$1
table=$2
keyspace_tables+=(${table})
for table in ${keyspace_tables[@]}; do
import_table ${keyspace} ${table}
done
echo "Done importing keyspace '$keyspace'."
}
# Execution
init_cqlsh
echo "Importing tables..."
for import_tuple in "$@"; do
IFS=: read keyspace table <<< ${import_tuple}
import_db ${keyspace} ${table}
done