No RSS feeds have been linked to this section.

Entries in bash script (1)

Friday
Jun012012

A simple script

As I discussed in the last post there is a virtual requirement for scripting ability in a large scale environment. The following is a simple script that could be used deploy scripts to pool of server.

 

--------------------------------------------------

#!/bin/bash

script=path/to/script/file.sql

for i in {1..100}
do
  (
  for dc in 'chi' 'la' 'sf'
  do
    for db in `mysql -u dba -h mysql${i}-${dc} -e "SHOW DATABASES"`
 do
      if [[ -z `mysql -u dba -s -rB -h  mysql${i}-${dc} $db  < $script` >> output/mysql.success ]]
      then echo p-cs${i}-${dc} $db >> output/mysql.broken;
      fi
    done;
  done;
  )&
done;

This isn't a tutorial on bash programming, but I want to point out a couple of things here that are relevant.

 

1) A reasonable naming scheme for your servers is important. Don't call them Gandolf, Bilbo and Frodo as cool as those names are. Once your pool of servers grows past six this just doesn't scale and much it more difficult to programmatically access in a loop. There are numerous naming strategies available but you should probably at least include what server type it is (dev, testing or production) and what data center it is located in. EVEN if you are only in one data center right now there is always the chance for growth down the road and redundancy (ie multiple data centers) is important.

2) "Parallelize" if possible. In this case I forked off the loop so that the script is executed against each server at the same time instead of serially. The & sign is used to do this in a bash script.

3) This may be obvious, but you should always write some type of output to a file. Log errors AND successes.

There is much more but I don't want to make this post to long. Start solving your small problems now programmatically so that you gain experience and build a bag of "tricks" to use down the road.