Skip to content

Commit

Permalink
Add support to exclude named CFs and support for SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelaws committed Feb 11, 2017
1 parent fbadbf7 commit c9ac8ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ The help should already contain some useful information:
usage: cassandradump.py [-h] [--cf CF] [--export-file EXPORT_FILE]
[--filter FILTER] [--host HOST] [--port PORT]
[--import-file IMPORT_FILE] [--keyspace KEYSPACE]
[--no-create] [--no-insert] [--password PASSWORD]
[--exclude-cf EXCLUDE_CF] [--no-create] [--no-insert]
[--password PASSWORD]
[--protocol-version PROTOCOL_VERSION] [--quiet]
[--sync] [--username USERNAME]
[--sync] [--username USERNAME] [--ssl]
[--certfile CERTFILE]

A data exporting tool for Cassandra inspired from mysqldump, with some added
slice and dice capabilities.
Expand All @@ -70,6 +72,9 @@ The help should already contain some useful information:
import data from the specified file
--keyspace KEYSPACE export a keyspace along with all its column families.
Can be specified multiple times
--exclude-cf EXCLUDE_CF
when using --keyspace, specify column family to
exclude. Can be specified multiple times
--no-create don't generate create (and drop) statements
--no-insert don't generate insert statements
--password PASSWORD set password for authentication (only if
Expand All @@ -81,6 +86,9 @@ The help should already contain some useful information:
--sync import data in synchronous mode (default asynchronous)
--username USERNAME set username for auth (only if protocol-version is
set)
--ssl enable ssl connection to Cassandra cluster. Must also
set --certfile.
--certfile CERTFILE ca cert file for SSL. Assumes --ssl.

In its simplest invocation, it exports data and schemas for all
keyspaces:
Expand Down
26 changes: 23 additions & 3 deletions cassandradump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import itertools
import codecs
from ssl import PROTOCOL_TLSv1

try:
import cassandra
Expand Down Expand Up @@ -207,6 +208,7 @@ def export_data(session):
f = codecs.open(args.export_file, 'w', encoding='utf-8')

keyspaces = None
exclude_list = []

if selection_options == 0:
log_quiet('Exporting all keyspaces\n')
Expand All @@ -222,6 +224,8 @@ def export_data(session):

if args.keyspace is not None:
keyspaces = args.keyspace
if args.exclude_cf is not None:
exclude_list.extend(args.exclude_cf)

if keyspaces is not None:
for keyname in keyspaces:
Expand All @@ -233,7 +237,10 @@ def export_data(session):
f.write(keyspace.export_as_string() + '\n')

for tablename, tableval in keyspace.tables.iteritems():
if tableval.is_cql_compatible:
if tablename in exclude_list:
log_quiet('Skipping data export for column family ' + keyname + '.' + tablename + '\n')
continue
elif tableval.is_cql_compatible:
if not args.no_insert:
log_quiet('Exporting data for column family ' + keyname + '.' + tablename + '\n')
table_to_cqlfile(session, keyname, tablename, None, tableval, f, limit)
Expand Down Expand Up @@ -300,6 +307,12 @@ def setup_cluster():
else:
port = int(args.port)

if args.ssl is not None and args.certfile is not None:
ssl_opts = { 'ca_certs': args.certfile,
'ssl_version': PROTOCOL_TLSv1 }
else:
ssl_opts = {}

cluster = None

if args.protocol_version is not None:
Expand All @@ -311,9 +324,9 @@ def setup_cluster():
elif args.protocol_version > 1:
auth = PlainTextAuthProvider(username=args.username, password=args.password)

cluster = Cluster(contact_points=nodes, port=port, protocol_version=args.protocol_version, auth_provider=auth, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes))
cluster = Cluster(contact_points=nodes, port=port, protocol_version=args.protocol_version, auth_provider=auth, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes), ssl_options=ssl_opts)
else:
cluster = Cluster(contact_points=nodes, port=port, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes))
cluster = Cluster(contact_points=nodes, port=port, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes), ssl_options=ssl_opts)

session = cluster.connect()

Expand All @@ -339,6 +352,7 @@ def main():
parser.add_argument('--port', help='the port of a Cassandra node in the cluster (9042 if omitted)')
parser.add_argument('--import-file', help='import data from the specified file')
parser.add_argument('--keyspace', help='export a keyspace along with all its column families. Can be specified multiple times', action='append')
parser.add_argument('--exclude-cf', help='when using --keyspace, specify column family to exclude. Can be specified multiple times', action='append')
parser.add_argument('--no-create', help='don\'t generate create (and drop) statements', action='store_true')
parser.add_argument('--no-insert', help='don\'t generate insert statements', action='store_true')
parser.add_argument('--password', help='set password for authentication (only if protocol-version is set)')
Expand All @@ -347,6 +361,8 @@ def main():
parser.add_argument('--sync', help='import data in synchronous mode (default asynchronous)', action='store_true')
parser.add_argument('--username', help='set username for auth (only if protocol-version is set)')
parser.add_argument('--limit', help='set number of rows return limit')
parser.add_argument('--ssl', help='enable ssl connection to Cassandra cluster. Must also set --certfile.', action='store_true')
parser.add_argument('--certfile', help='ca cert file for SSL. Assumes --ssl.')

args = parser.parse_args()

Expand All @@ -358,6 +374,10 @@ def main():
sys.stderr.write('--import-file and --export-file can\'t be specified at the same time\n')
sys.exit(1)

if args.ssl is True and args.certfile is None:
sys.stderr.write('--certfile must also be specified when using --ssl\n')
sys.exit(1)

session = setup_cluster()

if args.import_file:
Expand Down

0 comments on commit c9ac8ae

Please sign in to comment.