Saturday, March 9, 2013

script to get sha-256 hash of myql table descriptions

#!/bin/bash

# Generate SHA-256 hash database schema 
# and a hash of each individual table schema
# then we can see where changes have taken place.

usage() {
cat <

usage: $0 -d DNAME -h DBHOST

  -d  db name
  -h  db host

EOF
exit 1
}

while getopts "d:h:" OPTION; do
  case "$OPTION" in
    d) DB="$OPTARG" ;;
    h) DBHOST="$OPTARG" ;;
    \?) echo "Invalid Option: -$OPTARG" >&2
        usage
        exit 1 ;;
    *) usage
        exit 1 ;;
  esac
done

#enforce argument policy
[[ -z "$DB" ]] && usage;
[[ -z "$DBHOST" ]] && usage;

Q=`echo TRGtZ123Ec234REpKCg== | base64 -i -d -`

echo $DB_VERSION

#dump the schema and hash the whole thing
DBSCHEMA=`mysqldump -h$DBHOST --no-data -p$Q -uroot $DB`
DHASH=`echo $DBSCHEMA | openssl dgst -sha256`
echo "schemadump:"$DHASH

#get tables in the db
TABLES=`mysql --skip-column-names -h$DBHOST -p$Q -uroot $DB -e "SHOW TABLES;"`

#show the tables so we see what it truly is
echo $TABLES;

for i in $TABLES; do 

    TABLESCHEMA=`mysql -h$DBHOST -p$Q -uroot $DB -e "desc $i;"`
    THASH=`echo $TABLESCHEMA | openssl dgst -sha256`
    echo $i:$THASH

done

No comments: