Linux Admin Blog

System Administration, Linux, Solaris, Backup, Networking, Security, Mysql, Script, Tips & Tricks

Archive for the ‘Scripts’ Category

Read File using Shell Script

Posted by sanjaydalal4u on May 7, 2009

#!/bin/bash
FILENAME=”filename.txt”
exec 0< $FILENAME
while read LINE
do
echo $LINE
done

 

#!/bin/bash

FILENAME=”filename.txt”

exec 0< $FILENAME

while read LINE

do

     echo $LINE

done

Posted in HowTo, Scripts, Tips & Tricks | Leave a Comment »

Shell Script to take Backup Files & Directory

Posted by sanjaydalal4u on May 7, 2009

Step 1 : create a file /path/to/backupfilelist.txt which contains the files & directory which you want to take bacup

Step 2 : Write a script backup_files_and_directory.sh

#!/bin/sh

#Script for take a backup of list of files and directory in one compress file

 

#file format which contains backup files & directory

FILENAME=”`date +%d-%m-%Y`.tar.gz”

#This file contains the files & directory which we are going to take backup

BACKUPFILELIST=”/path/to/backupfilelist.txt”

#Directory where backup kept

BACKUPDIRECTORY=”/root/filebackup”

TAR=`which tar`

 

#create backup directory if not exist

if [ ! -d $BACKUPDIRECTORY ]; then

mkdir -p /root/filebackup

fi

#check backupfilelist.txt is available or not

if [ -f $BACKUPFILELIST ]; then

        BACKUPFILE=`cat $BACKUPFILELIST | grep -E -v “^#”`

else

        echo “File not Exist…Please create Backup file first”

        exit 1

fi

#check for the backupfilelist.txt null or not

if [ $BACKUPFILELIST = "" ]; then

        echo “Backup file list is empty..Please add some files or directory to take backup”

        exit 2

fi

#take a bakcup

$TAR -zcvf $BACKUPDIRECTORY/$FILENAME $BACKUPFILE > /dev/null

Step 3 : make script executable 

#chmod +x backup_file_and_directory.sh

Step 4 : execute file

#./backup_file_and_directory.sh

Posted in Backup, HowTo, Scripts | Leave a Comment »

Mysql database Backup Script

Posted by sanjaydalal4u on May 6, 2009

#!/bin/bash

MYSQL=`which mysql`             #mysql version

MYSQLDUMP=`which mysqldump`     #mysqldump file

HOSTNAME=`hostname`             #hostname of backup server

USERNAME=”username”                 #mysql server user

PASSWORD=”password”            #mysql user password

LOCALSERVER=”localhost”         #mysql local server name

BACKUPDIRECTORY=/root/mysqldbbackup     #backup directory where backup is stored

NOW=”$(date +”%d-%m-%Y”)”               #Backup file format

DATATBASELIST=”"                        #Mysql database list

IGNOREDATABASE=”exampledb”                  #Ignore database not to be include in backup

GZIP=`which gzip`                       #Gzip to compress the backup file

FILENAME=”"                             #Database backup file names

#store the database list in DATABASELIST

DATABASELIST=`$MYSQL -u $USERNAME -h $LOCALSERVER -p$PASSWORD -Bse ’show databases’`

#Fetch database on by one and take a backup

for db in $DATABASELIST

do

        skipdatabase=-1

        if [ "$IGNOREDATABASE" != "" ]; then

                for i in $IGNOREDATABASE        #check the ignore database list

                do

                        if [ "$db" = "$i" ]; then

                                skipdatabase=1        #it will skip this database and move to second database list

                        fi

                done

        fi

        if [ "$skipdatabase" = "-1" ]; then

                FILE=”$BACKUPDIRECTORY/$db.$HOSTNAME.$NOW.gz” #Backup file format & Backup directory

                #ALL in one command to take backup & compress the backup file

 $MYSQLDUMP -u $USERNAME -h $LOCALSERVER -p$PASSWORD $db | $GZIP -9 > $FILE

 

Posted in Backup, HowTo, Mysql, Scripts | Leave a Comment »

check SSL certificate expiration

Posted by sanjaydalal4u on April 30, 2009

Usage: ./check_expiration www.verisign.com

[-] Certificate for www.verisign.com has not expired yet: May 8 23:59:59 2009 GMT

 

#!/bin/bash 

dates=$(echo “GET /” |openssl s_client -connect “$1:443″ 2> /dev/null |openssl x509 -noout -dates) 

# For STARTTLS over e.g. smtp, replace the offending field by: 

# openssl s_client -connect “$1:25″ -starttls smtp if [ -z "$dates" ]; then    

 echo “[!] Invalid IP, not SSL or no cert found”     

exit 2 

fi 

not_after=$(echo $dates|cut -d ‘=’ -f 3) 

now_epoch=$(date +%s) 

not_after_epoch=$(date +%s -d “$not_after”) 

if [ $now_epoch -gt $not_after_epoch ]; then    

echo “[!] Certificate for $1 has expired: $not_after”     

exit 1 

else     

echo “[-] Certificate 

for $1 has not expired yet: $not_after” fi


After Completing the script change the permission of the script

#chm0d +x check_expiration.bash

Posted in Scripts, Tips & Tricks | Leave a Comment »

Shell script to backup MySql database

Posted by sanjaydalal4u on April 29, 2009

#!/bin/bash

# Shell script to backup MySql database

 

USERNAME=”mysql user name”    

PASSWORD=”PASSWORD”

HOSTNAME=”localhost” 

 

MYSQL=”$(which mysql)”

MYSQLDUMP=”$(which mysqldump)”

CHOWN=”$(which chown)”

CHMOD=”$(which chmod)”

GZIP=”$(which gzip)”

 

DESTINATION=”/directory1″    # Directoy to take backup

 

# Main directory where backup will be stored

BACKUPSTORE=”$DEST/mysql”

HOST=”$(hostname)”                                  # Get hostname

NOW=”$(date +”%d-%m-%Y”)”                  # Get data in dd-mm-yyyy format

FILE=”"                                                           # File to store current backup file

DATABASELIST=”"                                     # Store list of databases

IGNOREDATABASE=”temp”                   # DO NOT BACKUP THESE DATABASES

 

[ ! -d $BACKUPSTORE ] && mkdir -p $ BACKUPSTORE || :

 

$CHOWN 0.0 -R $DESTINATION

$CHMOD 0600 $DESTINATION

 

# Get all database list

DATABSELIST=”$($MYSQL -u $USERNAME -h $HOSTNAME -p$PASSWORD -Bse ’show databases’)”

 

for db in $DATABASELIST

do

    skipdb=-1

    if [ "$IGNOREDATABASE" != "" ];

    then

            for i in $IGNOREDATABASE

            do

                [ "$db" == "$i" ] && skipdb=1 || :

            done

    fi

 

    if [ "$skipdb" == "-1" ] ; then

            FILE=”$BACKUPSTORE/$db.$HOST.$NOW.gz”

            $MYSQLDUMP -u $USERNAME -h $HOSTNAME -p$PASSWORD $db | $GZIP -9 > $FILE

    fi

done

Posted in Mysql, Scripts | Leave a Comment »

Mysql Slave Server(Replication) Status Check Script

Posted by sanjaydalal4u on April 29, 2009

Below script is returning the Mysql Slave server Status.

If Replication  is running then it will not show any message but if replication is stop then it will send the email notification for status.

—————————

#!/usr/bin/env bash

repeat_alert_interval=15 # minutes

lock_file=/tmp/slave_alert.lck

active=yes

 

## Check if alert is already sent ## 

 

function check_alert_lock () {

    if [ -f $lock_file ] ; then

        current_file=`find $lock_file -cmin -$repeat_alert_interval`

        if [ -n "$current_file" ] ; then

            # echo “Current lock file found”

            return 1

        else

            # echo “Expired lock file found”

            return 2 

        fi

    else

    return 0

    fi

}

 

## Find the location of the mysql.sock file ##

 

function check_for_socket () {

        if [ -z $socket ] ; then

                if [ -S /var/lib/mysql/mysql.sock ] ; then

                        socket=/var/lib/mysql/mysql.sock

                elif [ -S /tmp/mysql.sock ] ; then

                        socket=/tmp/mysql.sock

                else

                        ps_socket=`netstat -ln | egrep “mysql(d)?\.sock” | awk ‘{ print $9 }’`

                        if [ "$ps_socket" ] ; then

                        socket=$ps_socket

                        fi

                fi

        fi

        if [ -S "$socket" ] ; then

                echo UP > /dev/null

        else

                echo “No valid socket file “$socket” found!”

                echo “mysqld is not running or it is installed in a custom location”

                echo “Please set the $socket variable at the top of this script.”

                exit 1

        fi

}

 

 

check_for_socket

 

Slave_IO_Running=`mysql -u username -p’password’ -h Slavehostip -Bse “show slave status\G” | grep Slave_IO_Running | awk ‘{ print $2 }’`

Slave_SQL_Running=`mysql -u username -p’password’ -h Slavehostip -Bse “show slave status\G” | grep Slave_SQL_Running | awk ‘{ print $2 }’`

Last_error=`mysql -u username -p’password’ -h Slavehostip -Bse “show slave status\G” | grep Last_error | awk -F \: ‘{ print $2 }’`

 

 

if [ -z $Slave_IO_Running -o -z $Slave_SQL_Running ] ; then

        echo “Replication is not configured or you do not have the required access to MySQL” | mail -s “Mysql Replication Status ” username@domain.com

        exit

fi

 

if [ $Slave_IO_Running == 'Yes' ] && [ $Slave_SQL_Running == 'Yes' ] ; then 

    if [ -f $lock_file ] ; then

        rm $lock_file

        echo “Replication slave is running”

        echo “Removed Alert Lock”

    fi

    exit 0

elif [ $Slave_SQL_Running == 'No' ] ; then

    if [ $active == 'yes' ] ; then

        check_alert_lock

        if [ $? = 1 ] ; then

            ## Current Lock ##

            echo “up” > /dev/null

        else

            ## Stale/No Lock ##

             touch $lock_file

            echo “SQL thread not running on server `hostname -s`!” | mail -s “This is subject” mail_ID@somedomain.com

            echo “Last Error:” $Last_error

        fi

    fi

    exit 1

elif [ $Slave_IO_Running == 'No' ] ; then

        if [ $active == 'yes' ] ; then

                check_alert_lock

                if [ $? = 1 ] ; then

                        ## Current Lock ##

            echo “up” > /dev/null

                else

                        ## Stale/No Lock ##

                        touch $lock_file

                        echo “LOG IO thread not running on server `hostname -s`!”

                        echo “Last Error:” $Last_error

                fi

    fi

    exit 1

else 

        if [ $active == 'yes' ] ; then

                check_alert_lock

                if [ $? = 1 ] ; then

                        ## Current Lock ##

            echo “up” > /dev/null

                else

                        ## Stale/No Lock ##

                        touch $lock_file

            echo “Unexpected Error!”

            echo “Check Your permissions!”

                fi

        fi

    exit 2

fi

Posted in Mysql, Scripts | 2 Comments »