Day: November 28, 2007

MySQL full DB dump -shell script

MySQL full DB dump -shell script

#!/bin/bash
#Shell script to backup MySQL database
#To set permission to user which can take  backup of all the DB in
#MySQL server. plzsee query below
#mysql> grant SELECT, LOCK TABLES  on  *.*  to
#’backup_user’@’localhost’ identified  by  ‘pwd_of_user’;
#
#(c)arunbagul@indiangnu.org
###############################################################
#DB details
user_name=”user”
user_pwd=”password”
myhost=”localhost”

# Linux bin paths, change this if it can’t be autodetected via which command
mysql_cmd=”$(which mysql)”
dump_cmd=”$(which mysqldump)”
CHOWN=”$(which chown)”
CHMOD=”$(which chmod)”
zip_cmd=”$(which gzip)”

# Backup Dest directory, change this if you have someother location
dest_dir=”/var/mysql-backup/

# Get hostname
host_name=”$(hostname)”

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

# File to store current backup file
FILE=””
# Store list of databases
DB_list=””

# DO NOT BACKUP these databases
Ignore_db=”test”

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

# Only root can access it!
$CHOWN 0.0 -R $dest_dir
$CHMOD 0600 $dest_dir

# Get all database list first
DB_list=”$($mysql_cmd -u$user_name -h $myhost -p$user_pwd   -Bse ‘show
databases’)”

#send db list to file and clear previous  content
echo “” > $mail_file
echo “” > $database_file

for db in $DB_list
do
skipdb=-1
#send DB list to file
echo $db >> $database_file
echo $db
if [ “$Ignore_db” != “” ];
then
for i in $Ignore_db
do
[ “$db” == “$i” ] && skipdb=1 || :
done
fi

if [ “$skipdb” == “-1″ ] ; then
FILE=”$dest_dir/$db.$host_name.$NOW.gz”
my_file=”$dest_dir/${db}_${host_name}.sql”
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir 🙂
# $dump_cmd -u $user_name -h $myhost -p$user_pwd $db | $zip_cmd -9 > $FILE
# DB dump start here
########################################
$dump_cmd -u $user_name -h $myhost -p$user_pwd $db > $my_file
if [ $? -eq 0 ]; then
echo $db >> $mail_file
fi
########################################
fi
done

#end

python script for sending mail.

python script for sending mail.

#!/usr/bin/python
#This  scritp  will send mail  in both HTML and Text format
#@arunbagul@indiangu.org
#@GPL
import smtplib
import os
import sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

#guess = sys.argv[1]
fromaddr = “arunbagul@indiangnu.org”
toaddrs = “toaddress@indiangnu.org”

#———————————–
file_name = “/home/arunbagul/mail-body.txt”
file = open(file_name)
msg = file.read();
#———————————-
#msg = MIMEMultipart()
#msg[‘From’] = fromaddr
#msg[‘To’] = COMMASPACE.join(toaddrs)
#msg[‘To’] = toaddrs
#msg[‘Date’] = formatdate(localtime=True)
#msg[‘Subject’] = “This is my Sub”

server = smtplib.SMTP(‘192.168.1.5’)
#server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
#server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()

Load Balancing – shell script

Load Balancing – shell script

#!/bin/bash
echo “——————————————-”
echo “”
echo -e “Wel come to Load Balancing………….”
echo -e “@arunbagul@indiangnu.org”
echo “”
echo “——————————————-”
#################################
#**root@proxy# cat  /etc/iproute2/rt_tables
#
# reserved values
#
#**255     local
#**254     main
#**253     default
#**0       unspec
#Entry manually added
#please  add this two line each internet connections
#for example two connections
#**251    sify3
#**252    tata
#################################

#specify Details
#Local LAN
LAN_IF=eth0
LAN_IP=192.168.1.68
LAN_NWK=192.168.1.0/24

#INET1 (internet connection )
INET1_IF=eth1
INET1_IP=10.10.172.230
INET1_NWK=10.10.172.0/24
INET1_GW=10.10.172.X

#INET2 (internet connection )
INET2_IF=eth2
INET2_IP=208.259.169.X
INET2_NWK=208.259.169.X/30
INET2_GW=208.259.169.X

#################################
case “$1” in

start)
#Start Load Balancing……..
echo -e “Starting Load Balancing……..”
##################################################################
#step(1)
#Flush out table tata  & sify3 if any routing is their.
/sbin/ip route flush table tata
/sbin/ip route flush table sify3
#################################
echo -e “Creating Kernel routing table::- tata(251)”
# 202.189.249.204/30 is IP n/w of ISP1 with
#gateway is 202.189.249.205 & IP assign
#to eth3  is 202.189.249.206 which is source(src)
/sbin/ip route add $INET2_NWK dev $INET2_IF src $INET2_IP proto kernel table tata
#Add entry for internel n/w
/sbin/ip route add $LAN_NWK dev $LAN_IF proto kernel src $LAN_IP table tata
#Now add default gateway to ISP-I in table tata
/sbin/ip route add default via $INET2_GW table tata
##################################################################
#step(2)
echo -e “Creating Kernel routing table::- sify3(253)”
# 10.10.127.0/24 is IP n/w of ISP2 with
# gateway is 10.10.127.1 & IP assign
#to eth1  is 10.10.127.222 which is source(src)
/sbin/ip route add $INET1_NWK  dev $INET1_IF  src $INET1_IP proto kernel table sify3
#Add entry for internel n/w
/sbin/ip route add $LAN_NWK dev $LAN_IF proto kernel src $LAN_IP table sify3
#Now add default gateway to ISP-II in table sify3
/sbin/ip route add default  via $INET1_GW  table sify3
##################################################################
#step(3)
#set up the routing rules.
#These actually choose what routing table to route with.
/sbin/ip rule  add from $INET2_IP table tata
/sbin/ip rule  add from $INET1_IP table sify3
##################################################################
#step(4)
#Delete default route path form kernel ‘main’ routing table.
/sbin/route del default
if [ $(echo $?) = 0 ] ; then
echo -e “Default gateway removed from ‘main’ table”
fi
/sbin/route del default
echo “Done………”
##################################################################
#step(5)
#now set up the default route to be a multipath route.
#In the default kernel this will balance routes over the two providers.
#/sbin/ip route add default equalize nexthop via $INET1_IP $dev $INET1_IF weight 1 nexthop via $INET2_IP dev $INET2_IF weight 1
/sbin/ip route add default  nexthop  via $INET1_IP dev $INET1_IF weight 1 nexthop via $INET2_IP dev $INET2_IF  weight 1
echo -e ” Thank you….”
##################################################################
;;
stop)
#Stoping Load Balancing….
echo -e “Stoping Load Balancing….”
#Seperating routing decision…
/sbin/ip rule del from $INET1_IP table tata
/sbin/ip rule del from $INET1_IP  table sify3
#Flush out table tata,sify3 and main if any routing is their.
/sbin/ip route flush table tata
/sbin/ip route flush table sify3
/sbin/ip route flush table main
#update kernel ‘main table’————–
#add entry for ‘tata’ connection
/sbin/ip route add $INET2_NWK  dev $INET2_IF  src $INET2_IP  proto kernel  scope link  table main
#add entry for ‘sify’ connection
/sbin/ip route add $INET1_NWK  dev eth1  src $INET1_IP proto kernel  scope link  table main
#Add entry for internel n/w
/sbin/ip route add $LAN_NWK  dev $LAN_IF proto kernel scope link src 192.168.0.3 table main
#Now add default gateway to ISP-I in table main
/sbin/ip route add default via $INET1_GW table main
;;
status)
#Checkng Load Balancing status….
echo -e “Checking Load Bal status…….”
#Check load balancing is successful or not————-
/sbin/ip route show | grep “nexthop” > /dev/null
if [ $(echo $?) = 0 ] ; then
echo -e “Load Balancing is SUCCESSSFUL”
else
echo -e  “Load Balancing FAILED”
echo -e “Load-balancing:: Please try again…”
fi
;;
table)
#Kernel routing table
#################################
echo -e “Kernel routing table ‘tata’ is…..”
/sbin/ip route show table tata
echo -e “Kernel routing table ‘sify3’ is…..”
/sbin/ip route show table sify3
#Now finally check kernel routing with default route as multiple route
echo -e “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”
echo -e “Kernel ‘main’ routing table is…(plz check here)”
/sbin/ip route show table  main
echo -e “………..”
/sbin/route -n
echo -e “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”
echo -e “!!please run this cmd when ever you will restart n/w service or PC.”
echo -e ” And make sure that both connection are running”
;;

*)
echo “Usage:/sbin/load-balancing {start|stop|status|table}”
exit 1
;;
esac
#################################
#exit 0
#END