#!/bin/bash # # DB Revision Deletion Tool v3.1 # Author : Fir3net.com # Date : 13/5/10 # . /opt/CPshrd-R??/tmp/.CPprofile.sh ### CHECK OS IS LINUX ### OS=`uname` if [ "${OS}" != "Linux" ] ; then echo "${OS}" is not currently supported by dbdel\. exit 1 fi ### VARIABLES ### VERDB=$FWDIR/conf/db_versions/database/versioning_db.fws TOTALDBLIST=`grep _id $VERDB | sed 's/.*(\|)//g'` TOTALDB=`grep _id $VERDB | sed 's/.*(\|)//g' | wc -l | sed 's/^ *//g'` TOTALSIZE=`du -sh /opt/CPsuite-R65/fw1/conf/db_versions/repository/ | awk ' { print $1 } '` ### FUNCTIONS ### list() { awk ' BEGIN { print " ----------------------------------\n | ID Date |\n ----------------------------------" } /ver_id|date/{arr[i++]=gensub(/^[^(]*\(|\)|"/,"","g")} arr[1]{print " | " arr[1]" | " arr[0]" | " ;i=0;delete arr} END { print " ----------------------------------"} ' "${VERDB}" } delete() { if [ "${DELNUM}" -gt "${TOTALDB}" ] ; then echo Error : Unable to remove "${DELNUM}" DB Revision\(s\) as there are only "${TOTALDB}" DB Revision\(s\) remaining. exit 1 fi TOTALDBARRAY=$((${TOTALDB} - ${DELNUM})) VersionArray=($TOTALDBLIST) echo -n Are you sure you want to remove "${DELNUM}" from the current "${TOTALDB}" DB Revision\(s\) ? \[Y\/N\] ; read input case $input in y ) ;; Y ) ;; n ) exit 0 ;; N ) exit 0 ;; * ) exit 0 esac dbver -m delete `for ((j=0; j < ${#VersionArray[@]} - ${TOTALDBARRAY} ; j++)) ; do echo -n ${VersionArray[j]}, ; done` > /dev/null if [ $? == 0 ] ; then echo Successfully removed "${DELNUM}" DB Revision\(s\). else echo Error : Return Code $?. fi echo } delete_before() { TOTALBEFORE=`list | awk ' NR>3 { print $2 } ' | grep -B10000 ${DELBEFORE} | wc -l | sed 's/^ *//g'` if [ "${TOTALBEFORE}" -eq "0" ] ; then echo Error : Incorrect Database Revision ID supplied. exit 1 fi TOTALDBARRAY=$((${TOTALDB} - ${TOTALBEFORE})) VersionArray=($TOTALDBLIST) echo -n Are you sure you want to remove the Database Revision "${DELBEFORE}" and all Revisions before? \[Y\/N\] ; read input case $input in y ) ;; Y ) ;; n ) exit 0 ;; N ) exit 0 ;; * ) exit 0 esac dbver -m delete `for ((j=0; j < ${#VersionArray[@]} - ${TOTALDBARRAY} ; j++)) ; do echo -n ${VersionArray[j]}, ; done` > /dev/null if [ $? == 0 ] ; then echo Successfully removed "${TOTALBEFORE}" DB Revision\(s\). else echo Error : Return Code $? fi echo } usage() { cat << EOF usage: dbdel [-d number | -b id_number | -s | -c | -l ] List, count and remove multiple database revisions -d number of db revisions to remove -b remove this db revision id and all before -s size of all DB Revisions -c count DB Revisions -l list DB Revisions -? usage EOF } ### MAIN ### while getopts "d:b:slc?" OPTION do case $OPTION in d) DELNUM=$OPTARG delete exit ;; b) DELBEFORE=$OPTARG delete_before exit ;; s) echo Total size of all DB Revisions = "${TOTALSIZE}" echo exit ;; l) list exit ;; c) echo Total number of Database Revisions = "${TOTALDB}" echo exit ;; ?) usage exit ;; esac done usage exit 0