Apache Cassandra allows us to take backups of our data using the nodetool command. Join us in this tutorial as we explore how to create snapshots in Apache Cassandra.
NOTE: Before taking the snapshot of your cluster, make sure that you have a sufficient disk space and no sessions are active.
Cassandra flushes all the in-memory writes to the disk and performs a hard link to the SSTable files in the cluster.
Cassandra Nodetool Snapshot Command
The following snippet shows the syntax of the nodetool snapshot command:
[(-pp | --print-port)] [(-pw | --password )]
[(-pwf | --password-file )]
[(-u | --username )] snapshot
[(-cf <table> | --column-family <table> | --table <table>)]
[(-kt | --kt-list | -kc | --kc.list )]
[(-sf | --skip-flush)] [(-t | --tag )] [--ttl ] [--]
[]
The following shows the parameters supported by the command:
- -h – Specifies the hostname or the IP address of the target cluster.
- -p – Sets the port number to the Cassandra cluster.
- -pwf – Specifies the password file that is used for cluster authentication.
- -pw – Specifies the password for a specified username.
- -u – Defines the username to login into the cluster.
- -cf – Sets the names of the tables that you wish to backup.
- -kc – Specifies the keyspace.tables to backup.
- -kt – Defines the list of keyspace.tables to backup.
- -sf – Prevents the SSTable flushing operation.
- -t – Name of the snapshot.
- Keyspace – Names of the keyspaces to backup. Defaults to all keyspaces.
Cassandra Backup All Keyspaces
To create a snapshot of all the keyspaces in a given cluster, we can run the following command:
The given command initializes a backup process for all the keyspaces in the cluster.
Cassandra stores the snapshot files in the data directory. You can check your cluster configuration to determine the cluster data directory.
Cassandra Backup Selective Snapshots
We can take the snapshots of multiple keyspaces by specifying them as shown in the following syntax:
For example, suppose we wish to backup the Linuxhint and system_auth keyspaces. We can run the following command:
The previous command should return a sample output as shown in the following:
Requested creating snapshot(s) for [linuxhint, system_auth] with snapshot name [1663410336447] and options {skipFlush=false}
Snapshot directory: 1663410336447
Cassandra Table Snapshot
You can take a snapshot of a given table as shown in the following syntax:
For example, suppose we wish to backup the table sample_table from the Linuxhint keyspace. We can run the following command:
Conclusion
In this post, you learned how to use the nodetool snapshot command to take snapshots of various objects in your Cassandra cluster.
Thanks for reading!