Skip to content

Instantly share code, notes, and snippets.

@platinumthinker
Last active April 29, 2024 07:56
Show Gist options
  • Save platinumthinker/bfdb2d47a41d63b4a7d719251e739c44 to your computer and use it in GitHub Desktop.
Save platinumthinker/bfdb2d47a41d63b4a7d719251e739c44 to your computer and use it in GitHub Desktop.
Cassandra backup/restore

Backup:

  1. Create snapshot:

> nodetool snapshot thingsboard (or you cat set you namespace) Requested creating snapshot(s) for [all keyspaces] with snapshot name [1522059918232] and options {skipFlush=false} Snapshot directory: 1522059918232

  1. Copy snapshot on another directory:

> backup.sh thingsboard 1522059918232 /var/lib/cassandra /my_backup_dir

Restore:

  1. Install cassandra and recreate scheme

  2. Run script for restore from backup:

> restore.sh thingsboard /var/lib/cassandra /my_backup_dir

  1. Run cassandra
#!/bin/bash
if [[ $# != 4 ]]; then
echo "Usage: $0 keyspace snapshot bd_dir backup_path"
exit 1
fi
export KEYSPACE=$1
export SNAPSHOT=$2
export BD_DIR=$3/data
export BACKUP_DIR=$4
for DIR in "$BD_DIR/$KEYSPACE/snapshots"/*; do
DEST=$(basename "$(dirname "$DIR")" | rev | cut -c34- | rev);
DEST="$BACKUP_DIR/$KEYSPACE/$DEST"
mkdir -p "$DEST"
cp "$DIR/$SNAPSHOT"/*.db "$DEST/"
cp "$DIR/$SNAPSHOT"/*.txt "$DEST/"
done
#!/bin/bash
set -x
if [[ $# != 4 ]]; then
echo "Usage: $0 keyspace snapshot bd_dir backup_path"
exit 1
fi
export KEYSPACE=$1
export SNAPSHOT=$2
export BD_DIR=$3/data
export BACKUP_DIR=$4
for DIR in $BD_DIR/$KEYSPACE/*/snapshots/; do
DEST=$(basename "$(dirname "$DIR")" | rev | cut -c34- | rev);
DEST="$BACKUP_DIR/$KEYSPACE/$DEST"
mkdir -p "$DEST"
cp "$DIR/$SNAPSHOT"/*.db "$DEST/"
cp "$DIR/$SNAPSHOT"/*.txt "$DEST/"
done
#!/bin/bash
set -e
if [[ $# != 3 ]]; then
echo "Usage: $0 keyspace bd_dir backup_path"
exit 1
fi
## Ignore error if cassandra already stopped
nodetool stopdaemon > /dev/null || true
export KEYSPACE=$1
export BD_DIR=$2
export BACKUP_DIR=$3
rm -rf "$BD_DIR/commitlog/"*.log
for DIR in "$BACKUP_DIR/$KEYSPACE/"*; do
CF=$(basename "$DIR")
if [ ! "$(ls -A "$DIR")" ]; then
continue
fi
cp "$DIR/"*.db "$BD_DIR/data/$KEYSPACE/$CF"-*/
cp "$DIR/"*.txt "$BD_DIR/data/$KEYSPACE/$CF"-*/
done
@dumy
Copy link

dumy commented Dec 7, 2021

Thx, for this code, you made great job.

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