Initial commit
[ta/monitoring.git] / src / recover-db-files.sh
1 #! /bin/bash
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 if [ $# -ne 2 ]; then
18         echo "Usage:$0 <node> <backup-dir>"
19         exit 1
20 fi
21
22 node_arg=$1
23 backup_dir=$2
24 node=$(hostname)
25
26 if [ $node_arg -ne $node ]; then
27     echo "You need to run the script from the same node where the corrupted db is"
28     exit 1
29 fi
30
31 echo "Creating backup directory $backup_dir"
32
33 mkdir -p $backup_dir
34
35 if [ $? -ne 0 ]; then
36         echo "Failed to create $backup_dir"
37         exit 1
38 fi
39
40 echo "Locking db service"
41 /opt/nokia/bin/hascli -l -o /$node/mariadb/mariadb
42 if [ $? -ne 0 ]; then
43     echo "Failed to lock /$node/mariadb/mariadb"
44     exit 1
45 fi
46
47 echo "Copying existing db files"
48 cp -r /var/lib/mysql $backup_dir
49
50 echo "Removing old db files"
51 rm -rf /var/lib/mysql
52
53 echo "Recreating db directory"
54 mkdir /var/lib/mysql
55 chown mysql:mysql /var/lib/mysql
56 chmod 2755 /var/lib/mysql
57
58 echo "Installing the db"
59 /usr/bin/mysql_install_db --datadir=/var/lib/mysql --user=mysql
60 if [ $? -ne 0 ]; then
61     echo "db installation failed"
62     exit 1
63 fi
64 chown -R mysql:mysql /var/lib/mysql/
65 /usr/sbin/restorecon -R /var/lib/mysql
66
67 echo "Starting db in safe mode"
68 /usr/bin/mysqld_safe --wsrep-provider=none &
69 if [ $? -ne 0 ]; then
70     echo "Failed to start db in safe mode"
71     exit 1
72 fi
73
74 echo "Waiting for db to become up"
75 while [ 1 ]; do
76     /bin/mysqladmin -h localhost -u root --password= ping | grep "mysqld is alive"
77     if [ $? -eq 0 ]; then
78         echo "DB is now up"
79         break
80     fi
81     echo "DB is not yet up, waiting..."
82     sleep 2
83 done
84
85 echo "Fix the passwords/grants"
86 root_password=$(sudo grep password /root/.my.cnf | cut -d'=' -f2)
87 echo "grant all on *.* to root@localhost identified by \"$root_password\";" >/tmp/restore.sql
88 echo "set password for 'root'@'localhost' = password(\"$root_password\");" >>/tmp/restore.sql
89 rc=0
90 mysql -h localhost -u root --password= < /tmp/restore.sql
91 if [ $? -ne 0 ]; then
92     echo "Failed to fix grants"
93     rc=1
94 fi
95
96 echo "Shutting down the db"
97 /usr/bin/mysqladmin -h localhost -u root shutdown
98 if [ $? -ne 0 ]; then
99     echo "Failed to shutdown the db"
100     rc=1
101 fi
102
103 if [ $rc -eq 0 ]; then
104     echo "DB files recovered successfully, starting db"
105     /opt/nokia/bin/hascli -u -o /$node/mariadb/mariadb
106 fi
107
108 exit $rc