Marat Salakhutdinov | 92154fb | 2020-07-30 11:05:32 -0400 | [diff] [blame] | 1 | #!/bin/sh |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 2 | |
| 3 | # Initialize variables |
| 4 | ss_dir="" |
| 5 | base_db_dir="" |
| 6 | ss_name="" |
| 7 | ss="snapshots" |
| 8 | me=`basename $0` |
| 9 | |
Guillaume Lambert | 785bc38 | 2021-03-09 21:41:30 +0100 | [diff] [blame] | 10 | find_target_table_name () |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 11 | { |
| 12 | dest_path=$1 |
| 13 | keyspace_name=$2 |
| 14 | src_table_name=$3 |
| 15 | find_in_dir=$dest_path/$keyspace_name |
| 16 | tname_without_uuid=$(echo $src_table_name | cut -d '-' -f 1) |
| 17 | dest_table_name=$(ls -td -- $find_in_dir/$tname_without_uuid-* | head -n 1 | rev | cut -d'/' -f1 | rev) |
| 18 | printf $dest_table_name |
| 19 | } |
| 20 | |
Guillaume Lambert | 785bc38 | 2021-03-09 21:41:30 +0100 | [diff] [blame] | 21 | print_usage () |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 22 | { |
| 23 | echo "NAME" |
| 24 | echo " Script to restore Cassandra database from Nuvo/Cain snapshot" |
| 25 | echo "SYNOPSIS" |
| 26 | echo " $me [--help|-h] [--base_db_dir|-b] [--snapshot_dir|-s] [--keyspace|-k] [--tag|-t]" |
| 27 | echo " MUST OPTIONS: base_db_dir, snapshot_dir, keyspace_name" |
| 28 | echo "DESCRIPTION" |
| 29 | echo " --base_db_dir, -b" |
| 30 | echo " Location of running Cassandra database" |
| 31 | echo " --snapshot_dir, -s" |
| 32 | echo " Snapshot location of Cassandra database taken by Nuvo/Cain" |
| 33 | echo " --keyspace, -k" |
| 34 | echo " Name of the keyspace to restore" |
| 35 | echo "EXAMPLE" |
| 36 | echo " $me -b /var/lib/cassandra/data -s /root/data.ss -k DISCOVERY_SERVER -t 1234567" |
| 37 | exit |
| 38 | } |
| 39 | if [ $# -eq 0 ] |
| 40 | then |
| 41 | print_usage |
| 42 | fi |
| 43 | |
Guillaume Lambert | ba6e50f | 2021-04-26 21:46:56 +0200 | [diff] [blame^] | 44 | while [ $# -gt 0 ] |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 45 | do |
| 46 | key="$1" |
| 47 | shift |
| 48 | |
| 49 | case $key in |
| 50 | -h|--help) |
| 51 | print_usage |
| 52 | ;; |
| 53 | -b|--base_db_dir) |
| 54 | base_db_dir="$1" |
| 55 | shift |
| 56 | ;; |
| 57 | -s|--snapshot_dir) |
| 58 | ss_dir="$1" |
| 59 | shift |
| 60 | ;; |
| 61 | -k|--keyspace) |
| 62 | keyspace_name="$1" |
| 63 | ;; |
| 64 | -t|--tag) |
| 65 | tag_name="$1" |
| 66 | ;; |
| 67 | --default) |
| 68 | DEFAULT=YES |
| 69 | shift |
| 70 | ;; |
| 71 | *) |
| 72 | # unknown option |
| 73 | ;; |
| 74 | esac |
| 75 | done |
| 76 | |
| 77 | # Validate inputs |
Guillaume Lambert | 5f4af05 | 2021-03-09 21:52:32 +0100 | [diff] [blame] | 78 | if [ "$base_db_dir" = "" ] || [ "$ss_dir" = "" ] || [ "$keyspace_name" = "" ] |
Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 79 | then |
| 80 | echo "" |
| 81 | echo ">>>>>>>>>>Not all inputs provided, please check usage >>>>>>>>>>" |
| 82 | echo "" |
| 83 | print_usage |
| 84 | fi |
| 85 | |
| 86 | # Remove commit logs from current data dir |
| 87 | #/var/lib/cassandra/commitlog/CommitLog*.log |
| 88 | find $base_db_dir/../ -name "CommitLog*.log" -delete |
| 89 | |
| 90 | # Remove *.db from current data dir excluding skipped keyspaces |
| 91 | find $base_db_dir/$keyspace_name -name "*.db" -delete |
| 92 | |
| 93 | # Copy snapshots to data dir |
| 94 | echo "----------db files in snapshots--------------" |
| 95 | dirs_to_be_restored=`ls $ss_dir` |
| 96 | for i in ${dirs_to_be_restored} |
| 97 | do |
| 98 | src_path=$ss_dir/$i/snapshots/$tag_name |
| 99 | # Find the destination |
| 100 | table_name=$i |
| 101 | dest_table=$(find_target_table_name $base_db_dir $keyspace_name $table_name) |
| 102 | dest_path=$base_db_dir/$keyspace_name/$dest_table |
| 103 | # Create keyspace/table directory if not exists |
| 104 | #if [ ! -d "$dest_path" ]; then |
| 105 | # mkdir -p $dest_path |
| 106 | #fi |
| 107 | db_files=$(ls $src_path/*.db 2> /dev/null | wc -l) |
| 108 | if [ $db_files -ne 0 ] |
| 109 | then |
| 110 | cp $src_path/*.db $dest_path |
| 111 | if [ $? -ne 0 ] |
| 112 | then |
| 113 | echo "=====ERROR: Unable to restore $src_path/*.db to $dest_path=====" |
| 114 | exit 1 |
| 115 | fi |
| 116 | echo "=======check $dest_path ===============" |
| 117 | ls $dest_path |
| 118 | fi |
| 119 | done |