Tikejhya: Ashish Nepal

Knowledgebase

Category: Scripts (Page 1 of 3)

AWS Automated Snapshot

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

#!/bin/bash

# Volumelist => volume_id:someidentifiername:retaintion_period
volume_list='

vol-01010101:ashishnepal.com:3
vol-01010102:tikejhya.com:3
vol-01010103:tikejhyaroot.com:3
vol-01010104:somerandombox.com:3

'

snapshot_volumes() {
for volume in $volume_list; do

volume_id=$(echo "$volume" | cut -d':' -f1)
host_name=$(echo "$volume" | cut -d':' -f2)
retention_days=$(echo "$volume" | cut -d':' -f3)
retention_date_in_seconds=$(date +%s --date "$retention_days days ago")
snapshot_description=$(date +%Y-%m-%d)

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

snapshot_id=$(aws ec2 create-snapshot --output=text --description $host_name-$snapshot_description --volume-id $volume_id --query SnapshotId)
aws ec2 create-tags --resource $snapshot_id --tags Key=CreatedBy,Value=AutomatedBackup

done
}

cleanup_snapshots() {
for volume in $volume_list; do

volume_id=$(echo "$volume" | cut -d':' -f1)
host_name=$(echo "$volume" | cut -d':' -f2)
retention_days=$(echo "$volume" | cut -d':' -f3)
retention_date_in_seconds=$(date +%s --date "$retention_days days ago")

snapshot_list=$(aws ec2 describe-snapshots --output=text --filters "Name=volume-id,Values=$volume_id" "Name=tag:CreatedBy,Values=AutomatedBackup" --query Snapshots[].SnapshotId)
for snapshot in $snapshot_list; do

snapshot_date=$(aws ec2 describe-snapshots --output=text --snapshot-ids $snapshot --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}')
snapshot_date_in_seconds=$(date "--date=$snapshot_date" +%s)
snapshot_description=$(aws ec2 describe-snapshots --snapshot-id $snapshot --query Snapshots[].Description)

if (( $snapshot_date_in_seconds <= $retention_date_in_seconds )); then aws ec2 delete-snapshot --snapshot-id $snapshot echo "aws ec2 delete-snapshot --snapshot-id $snapshot" else echo "Nothing to delete $snapshot" fi done done } cleanup_snapshots snapshot_volumes

Bash script to recursively add a file folder inside directory

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

This is just a sample script which will allow you to create directories inside directory for depth of 3 folders, with a file in each folder in 3rd directory.

ie.
1/2/3
3/4/3 …. and so on.

#!/bin/bash
cd dir
for ((i=0;i<=9;i+=1)); do mkdir -p $i cd $i; touch index.html for ((j=0;j<=9;j+=1)); do mkdir -p $j cd $j; touch index.html for ((k=0;k<=9;k+=1)); do mkdir -p $k; touch index.html done cd ../ done cd ../ done

Taking off one level off, which creats only 2 level

#!/bin/bash
cd dir
for ((i=0;i<=9;i+=1)); do mkdir -p $i cd $i; touch index.html for ((j=0;j<=9;j+=1)); do mkdir -p $j; touch index.html done cd ../ done

creating zabbix parameters (quick & easy)

#!/bin/bash

LOGFILE=`find /application/log/*/ -name “*-20??-??-??” | grep -i ‘[a-zA-Z]-[0-9]’`

for i in $LOGFILE; do

LOGNAME=`basename $i`
KEY=`basename $i | sed ‘s/………..$//g’`
LOGFULLNAME=`echo “${i}” | sed ‘s/………..$//g’`

echo “UserParameter=app.${KEY},wc -l ${LOGFULLNAME}-`date +%Y-%m-%d` | awk ‘{print $1}'”

KEYTONAME=`echo “$KEY” | sed ‘s/_/ /g’`

HOSTID=’10080’
KEY=bidsys.${KEY}
NAME=$KEYTONAME
DELAY=120

echo “INSERT INTO items (‘hostid’, ‘name’, ‘key_’, ‘delay’) VALUES (‘$HOSTID’, ‘$NAME’, ‘$KEY’, ‘$DELAY’);”

done

Bash: linebreak after result

Bash: linebreak after result

;echo

Log & Lockfile bash script

####### Bash Loging Function
function logOutput()
{
logMessage=$1
logTime=`date`
logFile=/var/log/program.log
echo “$logMessage Completed $logTime” >> $logFile
}

# Bash Lockfile
LOCKFILE=”/var/script/program.lock”

if [ -e “${LOCKFILE}” ]; then
echo “Already running.”
exit 99

else

echo $! > “${LOCKFILE}”
chmod 644 “${LOCKFILE}”

###################### Do Whatever you want ##########################
ls -al /home # lol
logOutput ‘oh whatever completed.’

### Ok i dont need lock now.
### Release Lock
rm -f “${LOCKFILE}”

fi

Script (Reading log file)

#!/bin/bash

# As my requirement was to get data written in right order weekly/daily basis
# It was more required to get data in right order while dumping into output fle
# So running ls -lrt would give extra field and awked date,user, owner and so on.

FILES=`ls -lrt /var/log/tikejhya/Email-* | awk ‘{ print $9 }’`

# Looping through all of the files found above.
for lf in $FILES;do

# Getting only the basename i.e. filename and sedding filename from . toget first half and removing
# consistent patern.
FILEHEAD=`basename $lf | cut -d”.” -f1 | sed -e ‘s/^……//g’`

# To make more readable inserting blank line followed by date
echo “” >> /home/tikejhya/email_report.log
echo “=========================================================” >> /home/tikejhya/email_report.log
echo “DATE: ‘$FILEHEAD’ ” >> /home/tikejhya/email_report.log

# cat $lf | awk ‘{ print $2,$3,$4,$10 } match($0,”SUBJECT”) {print substr($0,RSTART+0,1000)}’
# This means match subject and print anything after that, 1000 means 1000 character from there just to make it all.

# sed -e s/”}]”/”n&”/g => inserting a newline after }]

cat $lf | awk ‘{ print $2,$3,$4,$10 } match($0,”SUBJECT”) {print substr($0,RSTART+0,1000)}’ | cut -d””:”” -f1,2,3,4 | cut -d “,” -f1 | tr -d ‘n’ | sed -e s/”}]”/”n&”/g | sed ‘s/SUBJECT/ &/g’ | cut -d””:”” -f3 | sed -e’s/.$//g’ | sort | uniq -c | sort >> /home/ashish.nepal/touchpoint_count

done

Alert (General Script)

This script checks recent upload from one of my server which naming convention is pretty much standard ending with today’s date.

This script can be used as tip to use loop read (content inside some directory), Email notification.

#!/bin/bash
#### Directory variable####
LOG_DIR='/data/1/ /data/2/ /data/3/


##### Email notification if failure##########
RCPT="tikejhya@ashishnepal.com"


##### Loop starts here############

for i in $LOG_DIR;do
        file=`ls -lrt $i | grep -i `date +%Y-%m-%d --date='1 day ago'`_23 | tail -5 | wc -l`

        if [ $file -ne 0 ];then

        echo "File Were uploaded $i" >> /data/logs/upload_success



                else

                                echo "Alert: Files are Not uploaded for $i" >> /data/logs/uploads_error
                                echo "Files not uploaded to $i for `date +%Y-%m-%d --date='1 day ago'`" | mail -s "ATTENTION" $RCPT

fi
done
exit 0

Upgrade php Script

#!/bin/bash

#######################################################################
# This script upgrades php into newer version with simply passing     #
# version as an argument i.e. sh upgrade_php.sh PHP-5.3.17 (if        #
# i want to upgrade to given version.)                                #
#                                                                     #
# This script usage repo of uk.php.net which might not work if there  #
# is change with layout of repository, you might simply want to change#
# URL on REPO on line 35                                              #
#                                                                     #
# NOTE: So far this script only identifies HTTPD/LIGHTTPD, which      #
# has to be changed according to your need on line 45 (i.e. grep )    #
#######################################################################

function chkInput
                {
                if [ -z "$1" ];then
                displayUsage
                exit 0;
                fi
                }

function displayUsage
                {
                echo "Please enter php version you want to upgrade for"
                echo "If you want to upgrade php version 5.3.17 please write PHP-5.3.17"
                echo "Usage: sh upgrade_php.sh PHP-5.x.x"
                }



chkInput $1

REPO=`echo http://uk.php.net/get/$1.tar.gz/from/this/mirror | sed -e's/PHP/php/g'`


CUR_VERSION=`php --version | head -1 | cut -d' ' -f1,2 | sed -e 's/PHP /php-/g'`
CONFIGURE=`php -i | grep -i configure | awk '{for (i=1; i<=NF-3; i++) $i = $(i+3); NF-=3; print}'`
TARFILE=`echo $REPO | cut -d"/" -f5`
VERSION=`echo $REPO | cut -d"/" -f5 | sed -e's/.......$//g'`
SRC="/usr/local/src"
LOCALREPO="/usr/local/src/$VERSION/"

WEBSERVER=`netstat -tupan | grep -i 80 | grep -i "httpd" | awk '{ print $7}' | cut -d'/' -f2 | sort | uniq`




         if [ "$CUR_VERSION" = "$VERSION" ]; then
         echo 'Old Version: '$CUR_VERSION' Is same as Target Version: '$VERSION''


         else
                echo " Preparing PHP Upgrade from $CUR_VERSION to $VERSION "

                        read -t 5 -p "Press 'Q' to abort within 5 seconds..:" confirm

                                if [ "$confirm" != 'Q' ];then

                                                `cd $SRC && wget $REPO && tar -zxvf $TARFILE && cd $VERSION && $CONFIGURE && make && make install && service $WEBSERVER restart`

                                else
                                                echo "User Abort,exiting..."
                                fi
        fi

###### Standard PHP Config

./configure  --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5 --with-apxs2 --with-openssl --with-kerberos --with-zlib --with-bz2 --enable-calendar --with-curl --enable-dba --with-db4 --with-gdbm --enable-ftp --with-gd --with-jpeg-dir=/usr/lib64 --with-png-dir=/usr/lib64 --enable-gd-native-ttf --with-gettext --with-mhash --with-imap --with-imap-ssl --with-ldap --with-ldap-sasl --enable-mbstring --with-mcrypt --with-mysql --enable-soap --enable-sockets --enable-sqlite-utf8 --enable-zip --with-libdir=lib64 --with-iconv --with-xmlrpc --with-mime-magic --enable-pdo --with-gd-ttf --with-freetype-dir=/usr/include/freetype2/ --enable-intl --enable-bcmath

Bash Readline

Bash Readline

#!/bin/bash
filename=$1

echo “Log Start $ts” >> sentMessageLog.txt
exec 9<$filename while read -u9 line do echo " $line" #send im echo $line >> sentMessageLog.txt
done
echo “” > $filename
echo “Log End for $ts” >> sentMessageLog.txt

linefeed / Breakline with Bash script (Readline)

linefeed / Breakline with Bash script (Readline)

#!/bin/bash

DISPLAY=localhost:0.0
export DISPLAY

#!/bin/bash
let i=1
messageString=""
skypeUser=$1
for var in "$@"
        do
                if [ "$i" -gt "1" ];then
# Added var+messagestring but after 1st string of character
                        messageString="$messageString $var"
                fi
                let i=$i+1
# Add $var into $messagestring to make final Message
        done
echo "Sending Message : $messageString to User : $skypeUser" >> skypelog.txt

python /path/to/script.pl "$skypeUser" "`printf "$messageString"`"
# printf "$messageString"` Helps any incoming line break n

Page 1 of 3

Powered by WordPress & Theme by Anders Norén