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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 echo "Usage:$0 <node> <backup-dir>"
26 if [ $node_arg -ne $node ]; then
27 echo "You need to run the script from the same node where the corrupted db is"
31 echo "Creating backup directory $backup_dir"
36 echo "Failed to create $backup_dir"
40 echo "Locking db service"
41 /opt/nokia/bin/hascli -l -o /$node/mariadb/mariadb
43 echo "Failed to lock /$node/mariadb/mariadb"
47 echo "Copying existing db files"
48 cp -r /var/lib/mysql $backup_dir
50 echo "Removing old db files"
53 echo "Recreating db directory"
55 chown mysql:mysql /var/lib/mysql
56 chmod 2755 /var/lib/mysql
58 echo "Installing the db"
59 /usr/bin/mysql_install_db --datadir=/var/lib/mysql --user=mysql
61 echo "db installation failed"
64 chown -R mysql:mysql /var/lib/mysql/
65 /usr/sbin/restorecon -R /var/lib/mysql
67 echo "Starting db in safe mode"
68 /usr/bin/mysqld_safe --wsrep-provider=none &
70 echo "Failed to start db in safe mode"
74 echo "Waiting for db to become up"
76 /bin/mysqladmin -h localhost -u root --password= ping | grep "mysqld is alive"
81 echo "DB is not yet up, waiting..."
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
90 mysql -h localhost -u root --password= < /tmp/restore.sql
92 echo "Failed to fix grants"
96 echo "Shutting down the db"
97 /usr/bin/mysqladmin -h localhost -u root shutdown
99 echo "Failed to shutdown the db"
103 if [ $rc -eq 0 ]; then
104 echo "DB files recovered successfully, starting db"
105 /opt/nokia/bin/hascli -u -o /$node/mariadb/mariadb