-
Notifications
You must be signed in to change notification settings - Fork 390
/
cass-db-restore.sh
executable file
·118 lines (108 loc) · 2.65 KB
/
cass-db-restore.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Script to restore Cassandra db data [snapshot taken by nodetool command as follows]
# nodetool -h localhost -p 7199 snapshot
# The snapshot is created in <data_directory_location>/<keyspace_name>/snapshots/<snapshot_name>.
# Initialize variables
ss_dir=""
base_db_dir=""
ss_name=""
ss="snapshots"
declare -a dirs_to_be_restored=( )
me=`basename $0`
function print_usage()
{
echo "NAME"
echo " Script to restore Cassandra datbase from snapshot"
echo "SYNOPSIS"
echo " $me [--help|-h] [--base_db_dir|-b] [--snapshot_dir|-s] [--snapshot_name|-n]"
echo " MUST OPTIONS: base_db_dir, snapshot_dir, snapshot_name"
echo "DESCRIPTION"
echo " --base_db_dir, -b"
echo " Location of running Cassandra database"
echo " --snapshot_dir, -s"
echo " Snapshot location of Cassandra database"
echo " --snapshot_name, -n"
echo " Snapshot name"
echo "EXAMPLE"
echo " $me -b /var/lib/cassandra/data -s /root/data.ss -n 1403068337967"
exit
}
if [ $# -eq 0 ]
then
print_usage
fi
while [[ $# > 0 ]]
do
key="$1"
shift
case $key in
-h|--help)
print_usage
;;
-b|--base_db_dir)
base_db_dir="$1"
shift
;;
-s|--snapshot_dir)
ss_dir="$1"
shift
;;
-n|--snapshot_name)
ss_name="$1"
;;
--default)
DEFAULT=YES
shift
;;
*)
# unknown option
;;
esac
done
# Validate inputs
if [ "$base_db_dir" == "" ] || [ "$ss_dir" == "" ] || [ "$ss_name" == "" ]
then
echo ""
echo ">>>>>>>>>>Not all inputs provided, please check usage >>>>>>>>>>"
echo ""
print_usage
fi
ss_check="`find $ss_dir -name $ss_name` "
if [ "$ss_check" == " " ]
then
echo "Snapshot not found, please check !!"
exit
else
echo "Snapshot available...continuing.."
fi
data_dirs="`find $ss_dir -name $ss` "
# Get directories with snapshots
# expected format ss_dir/keyspace_dir/column_family_dir/snapshots/*.db
# reverse output dirs & cut 3 fields of interest
for i in $data_dirs
do
#x="`echo $i | cut -d "/" -f 4- | sed 's/snapshots$//'`"
x="`echo $i | rev | cut -d '/' -f -3 | rev | sed 's/snapshots$//'`"
dirs_to_be_restored+=("$x")
done
# Print directories with snapshots without full path
echo "----------------dirs to be restored------------"
for i in ${dirs_to_be_restored[@]}
do
echo $i
done
# Remove commit logs from current data dir
#/var/lib/cassandra/commitlog/CommitLog*.log
find $base_db_dir/../ -name "CommitLog*.log" -delete
# Remove *.db from current data dir
find $base_db_dir -name "*.db" -delete
# Copy snapshots to data dir
echo "----------db files in snapshots--------------"
for i in ${dirs_to_be_restored[@]}
do
src_path=$ss_dir/$i/$ss/$ss_name/
dest_path=$base_db_dir/$i/
cp $src_path/*.db $dest_path
echo "=======check $dest_path ==============="
ls $dest_path
done