blob: 7f271d262f4899c5e0d53ada228494a3455b42da [file] [log] [blame]
Marat Salakhutdinov92154fb2020-07-30 11:05:32 -04001#!/bin/sh
Akansha Dua7b6e1982019-09-04 13:36:12 +00002
3# Initialize variables
4ss_dir=""
5base_db_dir=""
6ss_name=""
7ss="snapshots"
8me=`basename $0`
9
Guillaume Lambert785bc382021-03-09 21:41:30 +010010find_target_table_name ()
Akansha Dua7b6e1982019-09-04 13:36:12 +000011{
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 Lambert785bc382021-03-09 21:41:30 +010021print_usage ()
Akansha Dua7b6e1982019-09-04 13:36:12 +000022{
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}
39if [ $# -eq 0 ]
40then
41 print_usage
42fi
43
Guillaume Lambertba6e50f2021-04-26 21:46:56 +020044while [ $# -gt 0 ]
Akansha Dua7b6e1982019-09-04 13:36:12 +000045do
46key="$1"
47shift
48
49case $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 ;;
74esac
75done
76
77# Validate inputs
Guillaume Lambert5f4af052021-03-09 21:52:32 +010078if [ "$base_db_dir" = "" ] || [ "$ss_dir" = "" ] || [ "$keyspace_name" = "" ]
Akansha Dua7b6e1982019-09-04 13:36:12 +000079then
80 echo ""
81 echo ">>>>>>>>>>Not all inputs provided, please check usage >>>>>>>>>>"
82 echo ""
83 print_usage
84fi
85
86# Remove commit logs from current data dir
87#/var/lib/cassandra/commitlog/CommitLog*.log
88find $base_db_dir/../ -name "CommitLog*.log" -delete
89
90# Remove *.db from current data dir excluding skipped keyspaces
91find $base_db_dir/$keyspace_name -name "*.db" -delete
92
93# Copy snapshots to data dir
94echo "----------db files in snapshots--------------"
95dirs_to_be_restored=`ls $ss_dir`
96for i in ${dirs_to_be_restored}
97do
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
119done