Database restore can very fast through below shell script which is more easier then enter manually all commands.
Note : Modify location of backup, email address, database name as per requirement in script.
During the execution of script it will ask the Database name to be restore & last logfile number/screenshot then after all the steps will be executing automatically through script.
#!/bin/sh
function errormail()
{
echo Script $0 >> $DB_LOG_FILE
echo "Created by: DBA Module">> $DB_LOG_FILE
echo "Created for: Server Name / IP">> $DB_LOG_FILE
echo ==== ended in error on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
echo -e "Dear Team,\n\nRestore has been in Error.\nKindly check attached file for more details.\n Regards,\nKapil Savaliya\n "| mutt -s "Error while running restore" -a $DB_LOG_FILE -- $MAILDBA
exit 1
}
##################SCRIPT FOR TAKE BACKUP###############################
function restoredb()
{
echo "Restore Started....">> $DB_LOG_FILE
echo -e "Dear Team,\n\nRestore has been Started on Server Name / IP.\n\nRegards,\nKapil Savaliya"| mutt -s "Restore Started" -c $MAILCC -- $MAILTO
echo ${thin_line} >> $DB_LOG_FILE
echo "Starting restoration.." >> $DB_LOG_FILE
echo ${thin_line} >> $DB_LOG_FILE
db2stop force;
if [ $? -eq 0 ]
then
echo ${thin_line}>> $DB_LOG_FILE
echo "Success STOP DATABASE....">> $DB_LOG_FILE
echo ${thin_line}>> $DB_LOG_FILE
else
echo "Error while shuting down the database">> $DB_LOG_FILE
errormail;
exit 1
fi
db2start;
db2 drop database <dbname>;
db2 "restore db <DBNAME> from <location> taken at $SCREENSHOT ON <location> into <dbname> redirect without prompting"
db2 RESTORE DATABASE <dbname> continue;
db2 get db config for <dbname> | grep -i "Path to log files";
cp <location>/LOGSTREAM0000/* <location>/LOGSTREAM0000/
db2 update db config for <dbname> using newlogpath <location>/LOGSTREAM0000/;
db2 get db cfg for <dbname>|grep -i rollforward;
db2 "rollforward db <dbname> to end of logs on dbpartitionnum (0)";
db2 "rollforward db <dbname> complete on dbpartitionnum (0)";
if [ $? -eq 0 ]
then
echo ${thin_line}>> $DB_LOG_FILE
echo "Successfully replica done....">> $DB_LOG_FILE
echo ${thin_line}>> $DB_LOG_FILE
else
echo "Error while replica the database">> $DB_LOG_FILE
errormail;
exit 1
fi
}
# ---------------------------------------------------------------------------
# Put output in <this file name>.out. Change as desired.
# Note: output directory requires write permission.
# ---------------------------------------------------------------------------
export day_of_week=`date +%d_%b_%y_%H`
DB_LOG_FILE=<location>/logfile/backup_log_${day_of_week}.txt
BACKUP_LOCATION=<location>/dbbackups
#Calling only first finction of Script, and all functions will be called from another
#function respected to success of failure of previous function.
echo Script $0 >> $DB_LOG_FILE
echo "Created by: DBA Module">> $DB_LOG_FILE
echo "Created for: Server Name / IP">> $DB_LOG_FILE
echo ==== started on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
############################################################################################
#----------------------------------SCRIPT CONTROL------------------------------------------#
printf "\nEnter Database Name for Restore : "
read -r ans1;
printf "\nEnter screenshot of backup database : "
read -r ans2;
function restore_start()
{
export thick_line="============================================================";
export thin_line="-------------------------------------------------------------";
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
echo ${thick_line}
echo "You are running script with user $CUSER"
echo ${thick_line}
export mday=`date +"%b-%d-%y"`
export DBBACKUP=$ans1
export SCREENSHOT=$ans2
export MAILTO=me@kapilsavaliya.com
export MAILCC=me@kapilsavaliya.com
export MAILDBA=me@kapilsavaliya.com
# -----------------------------------------------------------------
# Initialize the log file.
# -----------------------------------------------------------------
echo >> $DB_LOG_FILE
chmod 666 $DB_LOG_FILE
echo "Use tailf to see output file. Copy-Paste below command in new window.";
printf "\ntailf $DB_LOG_FILE\n";
echo ${thick_line} >> $DB_LOG_FILE
echo "You are running script with user $CUSER" >> $DB_LOG_FILE
echo ${thick_line} >> $DB_LOG_FILE
# ---------------------------------------------------------------------------
# You may want to delete the output file so that backup information does
# not accumulate. If not, delete the following lines.
# ---------------------------------------------------------------------------
if [ -f "$DB_LOG_FILE" ]
then
rm -f "$DB_LOG_FILE"
fi
#Calling only first finction of Script, and all functions will be called from another
#function respected to success of failure of previous function.
echo Script $0 >> $DB_LOG_FILE
echo "Created by: DBA Module">> $DB_LOG_FILE
echo "Created for: Server Name / IP">> $DB_LOG_FILE
echo ==== started on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
# ---------------------------------------------------------------------------
# Calling function.
# ---------------------------------------------------------------------------
#----------------------------------SCRIPT CONTROL------------------------------------------#
restoredb; #--Main Scipt Function
}
# ---------------------------------------------------------------------------
#All functions are listed in below script and
#called from another function if previous function successfully
#executed or else goes into error.
# ---------------------------------------------------------------------------
printf "\nDo you want to continue(Y/N) : "
read choice;
choice=`echo $choice | awk '{print toupper($0)}'`
case "$choice" in
"Y" | "YES")backup_start
;;
*)printf "\nExisting script\n"
exit 1;
;;
esac
####################################################################################################
#------------------------------End Of Script-------------------------------------------------------#
#End of Script.
#Created for: Server Name / IP
#Created by: DBA Module
####################################################################################################