Database backup can take through below shell script which is more easier then enter manually command.
Note : Modify location of backup & email address as per requirement in script.
During the execution of script it will ask the Database name which user want to take backup & mode of the backup as ONLINE / OFFLINE 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 IP / Name">> $DB_LOG_FILE
echo ==== ended in error on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
echo -e "Dear Team,\n\nBackup has been in Error.\nKindly check attached file for more details.\n Regards,\nKapil Savaliya\n "| mutt -s "Error while running backup" -a $DB_LOG_FILE -- $MAILDBA
exit 1
}
##################SCRIPT FOR TAKE BACKUP###############################
function backupdb()
{
echo "Backup Started....">> $DB_LOG_FILE
echo -e "Dear Team,\n\nBackup has been Started on Server IP / Name.\n\nRegards,\nKapil Savaliya"| mutt -s "Backup Started" -c $MAILCC -- $MAILTO
echo ${thin_line} >> $DB_LOG_FILE
echo "Starting backup.." >> $DB_LOG_FILE
echo ${thin_line} >> $DB_LOG_FILE
if [ $BACKUP_TYPE -eq 1 ]
then
db2 connect $DBBACKUP;
db2 backup database $DBBACKUP online to $BACKUP_LOCATION INCLUDE LOGS;
elif [ $BACKUP_TYPE -eq 2 ]
then
db2 connect $DBBACKUP;
db2 backup database $DBBACKUP to $BACKUP_LOCATION;
else
printf "\nInvalid option for backup\n"
exit 1;
fi
if [ $? -eq 0 ]
then
echo ${thin_line}>> $DB_LOG_FILE
echo "Successfully backup done....">> $DB_LOG_FILE
echo ${thin_line}>> $DB_LOG_FILE
else
echo "Error while backup 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 path>/backup_log_${day_of_week}.txt
BACKUP_LOCATION=<location path>/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 IP / Name">> $DB_LOG_FILE
echo ==== started on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
############################################################################################
#----------------------------------SCRIPT CONTROL------------------------------------------#
printf "\nEnter Database Name for Backup : "
read -r ans1;
printf "\nDo you online/offline backup - 1 for online | 2 for offline : "
read -r ans2;
function backup_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 BACKUP_TYPE=$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 IP / Name">> $DB_LOG_FILE
echo ==== started on `date` ==== >> $DB_LOG_FILE
echo >> $DB_LOG_FILE
# ---------------------------------------------------------------------------
# Calling function.
# ---------------------------------------------------------------------------
#----------------------------------SCRIPT CONTROL------------------------------------------#
backupdb; #--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 IP / Name
#Created by: DBA Module
####################################################################################################