I have accidentally truncated a table in Cassandra. I would like to know whether there is any tool available to restore data in it. Any help would be appreciated. Thanks.
auto_snapshot (Default: true)
Whether Cassandra takes a snapshot of the data before truncating a keyspace or dropping a table. To prevent data loss, DataStax strongly advises using the default setting. If you set auto_snapshot to false, data loss occurs on truncation or drop.
(taken from https://docs.datastax.com/en/cassandra/3.0/cassandra/configuration/configCassandra_yaml.html)
If you are in luck, look into the snapshot directories for your old sstables. For restoring they "only" need to be copied back to their original locations.
Here is what happens:
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/backups
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Data.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Index.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Filter.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Summary.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Digest.crc32
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-CompressionInfo.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-Statistics.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/mc-1-big-TOC.txt
Then I issued TRUNCATE demokeyspace.demo
- after that it is something like this:
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/backups
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Summary.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-TOC.txt
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Digest.crc32
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Filter.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-CompressionInfo.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Index.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Data.db
./data1/demokeyspace/demo-0d1a38b05fe211e8875d13cbb58d64f2/snapshots/truncated-1527228644868-demo/mc-1-big-Statistics.db
Simply copy those files back. But keep in mind that you need to do this on all nodes and run nodetool refresh demokeyspace demo
afterwards to reread the tables (of course with your keyspace and columnfamily).