Skip to content

Instantly share code, notes, and snippets.

@bcachet
Last active April 24, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcachet/52f1cf2b1d5cf21a7b70107a2b0f53d0 to your computer and use it in GitHub Desktop.
Save bcachet/52f1cf2b1d5cf21a7b70107a2b0f53d0 to your computer and use it in GitHub Desktop.
Cassandra: Export all tables from keyspaces on server
#!/bin/bash
function join
{
local IFS="$1"; shift; echo "$*";
}
function cqlsh_on
{
server=$1
cmd="${@:2}"
ssh -T $server << EOSSH
cqlsh -e "$cmd"
EOSSH
}
function export_cassandra_keyspaces
{
server=$1
keyspaces="${@:2}"
out_dir=export-keyspaces-$(join - $keyspaces)
for namespace in ${keyspaces}; do
tables=$(cqlsh_on $server "USE ${namespace}; DESCRIBE tables;")
mkdir -p ${out_dir}/${namespace}
for table in ${tables}; do
cmd="COPY ${namespace}.${table} TO STDOUT WITH HEADER=true;"
cqlsh_on $server $cmd | tee ${out_dir}/${namespace}/${table}.csv
done;
done;
}
export_cassandra_keyspaces "$@"
@bcachet
Copy link
Author

bcachet commented Apr 24, 2018

Usage is: ./export-cassandra-keyspaces.sh HOSTNAME KEYSPACE1 KEYSPACE2
It will generate and export-KEYSPACE1-KEYSPACE2 directory with a directory for each keyspace inside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment