Shell Scripts

1. Script to take backup of mysql,

##############Start script###################

#!/bin/bash
tdate=`date +%d-%b-%Y-%H-%M`
DIR=/backup/2hours_backup/mysql ##Mention Folder where backup should be done
DIR1=/backup/2hours_backup/ ## Mention Folder where final backup should be moved

[ ! $DIR ] && mkdir -p $DIR || :

/usr/bin/mysqldump -u root Databasename –lock-tables=false | gzip -c > $DIR/$tdate-backupfilename.sql.gz
/usr/bin/mysqldump -u root Databasename –lock-tables=false | gzip -c > $DIR/$tdate-backupfilename.sql.gz

tar -zcvf $DIR1/$tdate-2hoursdb_backup.tar.gz $DIR/*
rm -rf $DIR/*
echo “Message in Email 1300hrs and 1800hrs Backup Completed $tdate” | mail -s “Subject 1300hrs and 1800hrs DB Backup Successfully done” [email protected]

##################Script End#################

Save the Script in executive mode and cron the script as per your timing

 

2. Script to remove old files / Backup files

##############Start Script#######################

find /backup1/web_backup/*.tgz -mtime +06 -exec rm {} \;
find /backup1/etc_backup/*.tgz -mtime +06 -exec rm {} \;
find /backup1/weblocal_backup/*.tgz -mtime +03 -exec rm {} \;

##############Script End########################

Save the Script in executive mode and cron the script as per your timing

 

3.Script to backup code in tar format.

###############Start Script#####################

set -x
date=`date +%Y%m%d`
tar -czvf /backup1/etc_backup/etc.$date.tgz /etc
tar -czvf /backup1/web_backup/html.$date.tgz /home/html

echo “All Backup Completed” | mail -s “All Niti server Backup Successfully done $DATE” [email protected]

##############Script End#######################

Save the Script in executive mode and cron the script as per your timing

 

Send Logs attached in tar.gz format through scripts

################Start Script####################

cd /var/log/httpd/
tar -zcvf Filename_log.tar.gz log filename-error_log ## Tar command to execute
echo “Message in Mail” | mutt -a “/var/log/httpd/niticentral-error_log.tar.gz” -s “Subject” — [email protected] [email protected]
rm -rf niticentral-error_log.tar.gz ## Remove file after mail sent

###############Sctip End######################

Note: for this mail script you need to install MUTT application to send attachment,

yum installĀ mutt.x86_64

Save the Script in executive mode and cron the script as per your timing

 

How to restart the Service if load increases

Example: mysql service

##############Start Script#######################

#!/bin/sh
check=`cat /proc/loadavg | sed ‘s/\./ /’ | awk ‘{print $1}’`
if [ $check -gt 60 ]
then
{ /etc/init.d/mysql restart & date; } >> /var/log/restartmysql.log
fi

#############Script End#########################

Save the Script in executive mode and cron the script as per your timing

Comments are closed.