How to Backup Cassandra Database: Full Guide
Backing up a Cassandra database is essential for protecting your data against node failures, data corruption, or accidental deletions. This guide walks you through the key steps to create consistent backups using Cassandra’s built-in tools, including nodetool snapshots and commit log archiving. You’ll also learn how to restore from a backup, what to avoid, and how to troubleshoot common issues.
What You’ll Need
- Access to a Cassandra node (either via SSH or a local shell).
- Sufficient disk space on a backup location (local or remote).
- Knowledge of your keyspace names and table names (can be found with
DESCRIBE keyspaces;in cqlsh). - For incremental backups, ensure
incremental_backupsis enabled incassandra.yaml(default is false). - If using a multi-node cluster, perform backups on each node individually.
Step-by-Step
- Take a snapshot using nodetool. Run
nodetool snapshoton each node. This creates a hard-link snapshot of the SSTable files. For a specific keyspace, usenodetool snapshot -kt keyspace_name. For all keyspaces, justnodetool snapshot. Note the snapshot name (e.g.,snapshot_20250210_123456) or provide a custom tag:nodetool snapshot -t my_backup_tag. - Locate the snapshot files. Snapshots are stored under the data directory, typically
/var/lib/cassandra/data/keyspace/table-UUID/snapshots/your_tag/. You can find the data directory by runningnodetool infoand looking for “Data Directory”. - Copy the snapshot files to a safe location. Use
rsyncorcpto copy the entire snapshot directory to a backup storage (e.g., an external drive, cloud storage, or a different server). Example:rsync -av /var/lib/cassandra/data/ /backup/cassandra/. Ensure you copy thesnapshotsfolder within each table directory. - Archive the commit log (optional but recommended). To enable point-in-time recovery, copy the commit log files from
/var/lib/cassandra/commitlog/to your backup location. This allows you to replay mutations that occurred after the snapshot. - Clear the snapshot from the node. After copying, remove the snapshot from the node to free up disk space:
nodetool clearsnapshot -t your_tag. - Verify the backup. Check that the copied files are intact and that the directory structure matches the original. You can use
du -shto compare sizes. - Schedule regular backups. Automate the snapshot and copy process using cron or a scheduling tool. Run snapshots during low-traffic periods to minimize impact.
Common Mistakes
- Forgetting to enable incremental backups. If you want incremental backups, set
incremental_backups: trueincassandra.yaml. Without this, you only have full snapshots. - Not backing up all nodes. Each node holds a portion of the data (based on replication factor). You must back up every node to have a complete dataset.
- Copying only the snapshot, not the schema. The schema (keyspace definitions) is stored in the system keyspace. If you restore to a new cluster, you need to recreate the schema or back up the system keyspace as well.
- Taking a snapshot while a repair is running. This can lead to inconsistent snapshots. Wait for repairs to finish.
- Ignoring the commit log. Without the commit log, you lose any data written after the snapshot. For full recovery, always include the commit log.
Tips
- Use a consistent snapshot tag (e.g.,
daily_backup) to make management easier. - Test your backups regularly by restoring them to a staging environment.
- Store backups off-site or in a different availability zone to protect against datacenter failures.
- Monitor disk space on nodes; snapshots can consume significant space.
- For large clusters, consider using a distributed backup tool like Apache Cassandra’s
cassandra-backupor a third-party solution.
Troubleshooting
- Snapshot fails with “Requested range not satisfiable” or “No such file or directory”. Ensure the keyspace/table exists and that you have permission to read the data directory. Check
nodetool statusto see if the node is up. - Backup is too large or slow. Consider using incremental backups (if enabled) and streaming the snapshot to a remote location with
rsyncto avoid filling local disk. - Restore fails because of schema mismatch. When restoring to a different cluster, recreate the schema first (e.g., using
cqlshand the originalCREATE KEYSPACEandCREATE TABLEstatements) before copying data files. - Commit log replay errors. If the commit log is corrupted or from a different node, Cassandra may fail to start. In that case, you can start with
-Dcassandra.commitlog.ignore.replay.errors=trueto skip bad segments, but you may lose some recent data.
Frequently Asked Questions
Can I back up only a single table?
Yes, use nodetool snapshot -kt keyspace table to snapshot a specific table. The snapshot will be stored under that table’s directory.
Do I need to stop Cassandra to take a snapshot?
No, snapshots are online and do not require downtime. They use hard links to create a consistent view of the data files.
How long does a snapshot take?
It depends on the size of your data. Snapshots are fast because they only create hard links, but copying the files to backup storage can take time.
How do I restore from a snapshot?
Restore by copying the snapshot files back to the data directory, then restart Cassandra. You may also need to replay the commit log. Detailed steps vary by version; consult the official documentation.
What is the difference between a snapshot and an incremental backup?
A snapshot is a full copy of all SSTables at a point in time. An incremental backup copies only the SSTables that have changed since the last snapshot, reducing storage and time.
Can I back up to cloud storage directly?
Yes, you can use tools like s3cmd or gsutil to upload the snapshot files to cloud storage, or use a backup tool that integrates with cloud providers.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.