Tuesday, October 13, 2020

Mysql : Script to restore backup using MEB.

 Edit once you have copied to Vi editor...

******************************************************************

#!/bin/ksh


#########################################################################

# PROGRAM NAME: mysql-meb-restore_auto.sh 

#

# USAGE: Mysql- Restoring databases from MEB backup. 

#

##########################################################################


#

COMMAND=$0

PGM=${COMMAND##*/}

DATE=$(date "+%Y%m%d%H")

hostname=`hostname -s`

MAILTO=USER_SIVA@ABC.com

PWD=/opt/siva/scripts/restore  # put script dir path if you want to run from "crontab"

repdir=$PWD

LOG=$repdir/dblogs

echo $LOG


## *********************** Below list of changes are req:


conf_file=/etc/my.cnf

BACKUP_USER=TEST_USER

BACKUP_PASS=`cat /opt/.orapasswd/.backupusr`       # to hide password

datadir=/opt/data/mysql   #### make sure this is correct.

bkup_dir_a=/backup/test/TESTDB/meb/FULL/      # Backup location hardcoded:

innodb_log_file_v=1610612736                  # change innodb_log_file_size as  in the backup file 

Logfile=mysql_restore_auto_"$hostname"_$DATE.log

log_ret=15                                    # retention for logfiles.

echo  "started $DATE" >>$LOG/$Logfile


## Disable below lines if $bkup_dir is hardcoded:   below lines will automatically select last night backup.


p_date=$(date --date='-1 day' '+%Y-%m-%d')

bk_dir="$p_date*"

bkup_dir_f=`find $bkup_dir_a -type d -name "$bk_dir"`

bkup_dir=$bkup_dir_f

echo $bkup_dir


#bkup_dir=/backup/test/TESTDB/meb/FULL/2020-07-20_23-00-08   # disable above line and enable this line if you want to mention backup directroy.

########## Changes are done:


#**** Check if Backup directory exists:


if [ ! -d "$bkup_dir" ]; then

echo "backup directory $bkup_dir doesn't exists" | mailx -s "Mysql-Restore Status: $hostname - Failed" $MAILTO

exit;

else

echo "$bkup_dir exists" 

fi


###


## ***************** run Active file:


if [ -f $LOG/bkp_active_file ]

 then

   echo "previous mysql restore/backup is still running. confirm before running again" | mailx -s "Mysql-Restore Status: $hostname - Failed" $MAILTO

   exit 0

fi


### checking Logs Directory:

if [ ! -d $LOG ]; then

mkdir  $LOG

else

/usr/bin/find $LOG -type f -mtime +$log_ret -exec rm -f {} \;

fi


####


## ************************************************* Prep Steps before Start of Restore:


touch $LOG/bkp_active_file


####### Checking MySQL Instance status:


echo "Step 1: checking MySQL Instance status:"


inst_up=`ps -ef| grep mysqld.pid |wc -l`

if [ "$inst_up" -gt 1 ]; then

echo "Mysql instance is up. Shutting down now."

sudo systemctl stop mysqld


RETCODE=$?

print  "RETCODE:  $RETCODE"

if [ "$RETCODE" -ne 0 ]; then

   print  "echo MySQL Instance did not shutdown cleanly. please check" | mailx -s "Mysql-Restore Status: $hostname - Failed" $MAILTO

rm $LOG/bkp_active_file

exit 0;

fi


else

echo "Mysql instance is not up"

fi


#### 


### Prep step for Mysql Restore:


echo "Step 2: cleaning the $datadir for restore:"

cd $datadir

sudo /usr/bin/rm -r $datadir/* 

sudo /usr/bin/rm -r /data/log/*


####


## ************************************************* Start Of MySQL Restore:


echo "Step 3: MySQL restore started:"


/opt/mysql/meb-4.1/bin/mysqlbackup --defaults-file=/etc/my.cnf --port=1234 --host=localhost --user=$BACKUP_USER --password=$BACKUP_PASS --datadir=/data/data/mysql --innodb_log_file_size=$innodb_log_file_v --with-timestamp --backup-dir=$bkup_dir --messages-logdir=$LOG copy-back-and-apply-log >>$LOG/$Logfile 2>&1


####


## *********************** Check Any errors in the logfile:


echo "Step 4: Checking errors in Restore:"


error_chk=$(cat $LOG/$Logfile | awk 'BEGIN{buf=""} /[0-9]:[0-9][0-9]:[0-9]/{buf=$0} /WARNING/{print buf,$0}' | wc -l)


if [[ $error_chk -le 1 ]]

  then

     echo "Mysql Restore completed successfully - ignorable warning(1) - $error_chk " |tee -a $LOG/$Logfile

   else

    echo "Mysql Restore  was unsuccessful - has more errors than required(1). - error count - $error_chk" |tee -a $LOG/$Logfile

    echo "Mysql Restore  was unsuccessful - has more errors than required(1). - error count - $error_chk . please check" | mailx -s "Mysql-Restore Status: Completed with errors" $MAILTO


fi


####


## ********************** Remove run active file:


if [ -f $LOG/bkp_active_file ]

 then

rm $LOG/bkp_active_file

echo " removed run-active file" |tee -a $LOG/$Logfile

fi


## ************************************************* Post steps after MySQL Restore: 


echo "Step 5: Changing $datadir permissions:"

sudo chown -R mysql.mysql $datadir              # enable this if you are using different owner than mysql.


echo "Step 6: Starting MySQL Instance:"

sudo systemctl start mysqld


RETCODE=$?

print  "RETCODE:  $RETCODE"

if [ "$RETCODE" -ne 0 ]; then

   print  "MySQL Instance did not start cleanly. please check" | mailx -s "Mysql-Restore Status: $hostname - Failed" $MAILTO

exit 0;

fi


## *********************** Final MySQL status:


inst_up=`ps -ef| grep mysqld.pid |wc -l`

if [ "$inst_up" -gt 1 ]; then

echo "Mysql-Restore Status: All steps are done" |tee -a $LOG/$Logfile

echo "Mysql instance is up - After-Restore - Final-step Done " | mailx -s "Mysql-Restore Status: $hostname - completed" $MAILTO

else

echo "Mysql instance is not up - After-Restore - Final-step failed" | mailx -s "Mysql-Restore Status: $hostname -failed " $MAILTO 

fi


####



## All steps Done: End


No comments:

Post a Comment

PostgreSql: Useful Commands-

 1)   ************************* Users ***************************  -- List of users with roles assigned: SELECT usename AS role_name,   CASE...