Wednesday, November 19, 2014

Shell Script To Dump All MySQL Databases Every Hour To NAS Storage

+ Download this script
+ Modify settings according to your setup
+ Install cron job as follows to run script every hour
  1. # Backup database every 1 hr to /nas
  2. @hourly /root/scripts/db1hr.backup.sh >/dev/null 2>&1

Sample Shell Script To Dump All MySQL Databases

  1. #!/bin/bash
  2. # A simple shell script to backup all MySQL Server Database
  3. # Dump all MySQL database every hour from raid10 db disk to /nas/mysql
  4. # Each dump will be line as follows:
  5. # Directory: /nas/mysql/mm-dd-yyyy
  6. # File: mysql-DBNAME.04-25-2008-14:23:40.gz
  7. # Full path: /nas/mysql/mm-dd-yyyy/mysql-DBNAME.04-25-2008-14:23:40.gz
  8. # -------------------------------------------------------------------------
  9. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  10. # This script is licensed under GNU GPL version 2.0 or above
  11. # -------------------------------------------------------------------------
  12. # This script is part of nixCraft shell script collection (NSSC)
  13. # Visit http://bash.cyberciti.biz/ for more information.
  14. # -------------------------------------------------------------------------
  15. # Last updated: Jul-16-2009 - Fixed a small bug
  16. # - Make sure NAS really mounted on $NAS
  17. # -------------------------------------------------------------------------
  18. NOW=$(date +"%m-%d-%Y") # mm-dd-yyyy format
  19. FILE="" # used in a loop
  20. NASBASE="/nas" # NAS Mount Point
  21. BAK="${NAS}/mysql/${NOW}" # Path to backup dir on $NAS
  22.  
  23. ### Server Setup ###
  24. #* MySQL login user name *#
  25. MUSER="root"
  26.  
  27. #* MySQL login PASSWORD name *#
  28. MPASS="YOUR-PASSWORD"
  29.  
  30. #* MySQL login HOST name *#
  31. MHOST="127.0.0.1"
  32.  
  33. #* MySQL binaries *#
  34. MYSQL="$(which mysql)"
  35. MYSQLDUMP="$(which mysqldump)"
  36. GZIP="$(which gzip)"
  37.  
  38. # Make sure nas is really mounted
  39. mount | awk '{ print $3}' |grep -w $NASBASE >/dev/null
  40. if [ $? -ne 0 ]
  41. then
  42. echo "Error: NAS not mounted at $NASBASE, please mount NAS server to local directory and try again."
  43. exit 99
  44. fi
  45.  
  46. ### NAS MUST BE MOUNTED in Advance ###
  47. # assuming that /nas is mounted via /etc/fstab
  48. if [ ! -d $BAK ]; then
  49. mkdir -p $BAK
  50. else
  51. :
  52. fi
  53.  
  54. # get all database listing
  55. DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  56.  
  57. # start to dump database one by one
  58. for db in $DBS
  59. do
  60. FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz
  61. # gzip compression for each backup file
  62. $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
  63. done

Shell Script To Delete Files In The First Directory Which Are Similarly Named In The Second Directory

A shell script to compare files in two directory and delete files in the first directory which are similarly named in the second directory. Following script uses for loop to compare files in a each directory.
  1. #!/bin/bash
  2. # Write a shell script that accepts two directory names as arguments and
  3. # deletes those files in the first directory which are similarly named in
  4. # the second directory.
  5. SRC="$1"
  6. DST="$2"
  7. if [ $# -ne 2 ]
  8. then
  9. echo "$(basename $0) dir1 dir2"
  10. exit 1
  11. fi
  12.  
  13. if [ ! -d $SRC ]
  14. then
  15. echo "Directory $SRC does not exists!"
  16. exit 2
  17. fi
  18.  
  19.  
  20. if [ ! -d $DST ]
  21. then
  22. echo "Directory $DST does not exists!"
  23. exit 2
  24. fi
  25.  
  26. for f in $DST/*
  27. do
  28. #echo Processing $f
  29. if [ -f $f ]
  30. then
  31. tFile="$SRC/$(basename $f)"
  32. if [ -f $tFile ]
  33. then
  34. echo -n "Deleting $tFile..."
  35. /bin/rm $tFile
  36. [ $? -eq 0 ] && echo "done" || echo "failed"
  37.  
  38. fi
  39. fi
  40. done

Change password shell script

  1. #!/usr/local/bin/expect -f
  2. # Password change shell script, tested on Linux and FreeBSD
  3. # ----------------------------------
  4. # It need expect tool. If you are using Linux use following command
  5. # to install expect
  6. # apt-get install expect
  7. # FreeBSD user can use ports or following command:
  8. # pkg_add -r -v expect
  9. # ----------------------------------
  10. # If you are using linux change first line
  11. # From:
  12. #!/usr/local/bin/expect -f
  13. # To:
  14. #!/usr/bin/expect -f
  15. # -----------------------------------------------
  16. # Copyright (c) 2006 nixCraft project
  17. # This script is licensed under GNU GPL version 2.0 or above
  18. # -------------------------------------------------------------------------
  19. # This script is part of nixCraft shell script collection (NSSC)
  20. # Visit http://bash.cyberciti.biz/ for more information.
  21. # -------------------------------------------------------------------------
  22. # display usage
  23. if {$argc!=2} {
  24. send_user "usage: $argv0 username password \n"
  25. exit
  26. }
  27. # script must be run by root user
  28. set whoami [exec id -u]
  29. if {$whoami!=0} {
  30. send_user "You must be a root user to run this script\n"
  31. exit
  32. }
  33. #
  34. set timeout -1
  35. match_max 100000
  36. # stopre password
  37. set password [lindex $argv 1]
  38. # username
  39. set user [lindex $argv 0]
  40. # opem shell
  41. spawn $env(SHELL)
  42. # send passwd command
  43. send -- "passwd $user\r"
  44. expect "assword:"
  45. send "$password\r"
  46. expect "assword:"
  47. send "$password\r"
  48. send "\r"
  49. expect eof

Shell script for search for no password entries and lock all accounts

  1. #!/bin/bash
  2. # Shell script for search for no password entries and lock all accounts
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. # Set your email
  11. ADMINEMAIL="admin@somewhere.com"
  12.  
  13. ### Do not change anything below ###
  14. #LOG File
  15. LOG="/root/nopassword.lock.log"
  16. STATUS=0
  17. TMPFILE="/tmp/null.mail.$$"
  18.  
  19. echo "-------------------------------------------------------" >>$LOG
  20. echo "Host: $(hostname), Run date: $(date)" >> $LOG
  21. echo "-------------------------------------------------------" >>$LOG
  22.  
  23. # get all user names
  24. USERS="$(cut -d: -f 1 /etc/passwd)"
  25.  
  26. # display message
  27. echo "Searching for null password..."
  28. for u in $USERS
  29. do
  30. # find out if password is set or not (null password)
  31. passwd -S $u | grep -Ew "NP" >/dev/null
  32. if [ $? -eq 0 ]; then # if so
  33. echo "$u" >> $LOG
  34. passwd -l $u #lock account
  35. STATUS=1 #update status so that we can send an email
  36. fi
  37. done
  38. echo "========================================================" >>$LOG
  39. if [ $STATUS -eq 1 ]; then
  40. echo "Please see $LOG file and all account with no password are locked!" >$TMPFILE
  41. echo "-- $(basename $0) script" >>$TMPFILE
  42. mail -s "Account with no password found and locked" "$ADMINEMAIL" < $TMPFILE
  43. # rm -f $TMPFILE
  44. fi

Shell Script To Read IP Address ( Find Ip Address Script )

  1. #!/bin/sh
  2. # Shell script scripts to read ip address
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. # Get OS name
  11. OS=`uname`
  12. IO="" # store IP
  13. case $OS in
  14. Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
  15. FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
  16. SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
  17. *) IP="Unknown";;
  18. esac
  19. echo "$IP"

Shell Script To Monitor Services Such As Web / Http, Ssh, Mail Server

  1. #!/bin/bash
  2. # Shell script to monitor running services such as web/http, ssh, mail etc.
  3. # If service fails it will send an Email to ADMIN user
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # ----------------------------------------------------------------------
  11. # See URL for more info
  12. # http://www.cyberciti.biz/tips/processing-the-delimited-files-using-cut-and-awk.html
  13. # ---------------------------------------------------
  14. # Last updated: Jun - 15 - 2009.
  15. ports="22 53 80 25"
  16.  
  17. # service names as per above ports
  18. service="SSH DNS WEB MAIL"
  19.  
  20. #Email id to send alert
  21. ADMINEMAIL="admin@myispname.com"
  22.  
  23. #Bin paths, set them according to your Linux distro
  24. NETSTAT=/bin/netstat
  25. MAIL=/usr/bin/mail
  26. LOGGER=/usr/bin/logger
  27. ID=/usr/bin/id
  28.  
  29. # Red hat usr uncomment
  30. MAIL=/bin/mail
  31. LOGGER=/bin/logger
  32.  
  33.  
  34. #Counters, set defaults
  35. c=1
  36. status=""
  37. sendmail=0
  38.  
  39. # set the following to 1, if you want message in /var/log/messages via a SYSLOG
  40. logtosyslog=0
  41.  
  42. # Log file used to send an email
  43. LOG="/tmp/services.log.$$"
  44.  
  45. # log message to screen and a log file
  46. log(){
  47. echo "$@"
  48. echo "$@" >> $LOG
  49. }
  50.  
  51. # log message and stop script
  52. die(){
  53. echo "$@"
  54. exit 999
  55. }
  56.  
  57. # Make sure only root can run it
  58. is_root(){
  59. local id=$($ID -u)
  60. [ $id -ne 0 ] && die "You must be root to run $0."
  61. }
  62. # Look out for all bins and create a log file
  63. init_script(){
  64. [ ! -x $MAIL ] && die "$MAIL command not found."
  65. [ ! -x $NETSTAT ] && die "$NETSTAT command not found."
  66. [ ! -x $LOGGER ] && die "$LOGGER command not found."
  67. [ ! -x $ID ] && die "$ID command not found."
  68. is_root
  69. >$LOG
  70. }
  71.  
  72.  
  73. # check for all running services and shoot an email if service is not running
  74. chk_services(){
  75. log "-------------------------------------------------------------"
  76. log "Running services status @ $(hostname) [ $(date) ]"
  77. log "-------------------------------------------------------------"
  78.  
  79. # get open ports
  80. RPORTS=$($NETSTAT -tulpn -A inet | grep -vE '^Active|Proto' | grep 'LISTEN' | awk '{ print $4}' | cut -d: -f2 | sed '/^$/d' | sort -u)
  81.  
  82. # okay let us compare them
  83. for t in $ports
  84. do
  85. sname=$(echo $service | cut -d' ' -f$c)
  86. echo -en " $sname\t\t\t : "
  87. echo -en " $sname\t\t\t : " >> $LOG
  88. for r in $RPORTS
  89. do
  90. if [ "$r" == "$t" ]
  91. then
  92. status="YES"
  93. sendmail=1
  94. break
  95. fi
  96. done
  97. echo -n "$status"
  98. echo ""
  99. echo -n "$status" >>$LOG
  100. echo "" >>$LOG
  101. # Log to a syslog /var/log/messages?
  102. # This is useful if you have a dedicated syslog server
  103. [ $logtosyslog -eq 1 ] && $LOGGER "$sname service running : $status"
  104.  
  105. # Update counters for next round
  106. c=$( expr $c + 1 )
  107. status="NO"
  108. done
  109. log "-------------------------------------------------------------"
  110. log "This is an automatically generated $(uname) service status notification by $0 script."
  111.  
  112. if [ $sendmail -eq 1 ];
  113. then
  114. $MAIL -s "Service Down @ $(hostname)" $ADMINEMAIL < $LOG
  115. fi
  116. }
  117.  
  118. ### main ###
  119. init_script
  120. chk_services
  121.  
  122. ### remove a log file ###
  123. [ -f $LOG ] && /bin/rm -f $LOG