#################SCRIPT FOR AUTOMATIC INVALID TABLE BACKUP###########
#!/usr/bin/bash
##rm pcount.log
##rm qcount.log
t=`date +"%Y%m%d%H%M%S"`
p="invalids$t"
pcount="pcount.$t"
qcount="qcount.$t"
a=`grep -o '[0-9]\+' pcount.log|sed -n 1p`
b=`grep -o '[0-9]\+' qcount.log|sed -n 1p`
sqlplus -s "/ as sysdba" << EOF
create table $p as select * from dba_objects where status like 'INVALID';
spool pcount.log
select count (*) from $p;
exit;
EOF
sqlplus -s "/ as sysdba" << EOF
spool qcount.log
select count(*) from dba_objects where status like 'INVALID';
exit;
EOF
if [ $a == $b ]
then
echo "#########****INVALID TABLE BACKUP HAS BEEN TAKEN SUCESSFULLY****#######"
echo "INVALID-BACKUP-TABLENAME: $p"
echo "INVALID-BACKUP-TABLECOUNT: $a"
echo "ACUTAL-INVALID-TABLE-COUNT: $b"
else
sqlplus -s "/ as sysdba" << EOF
spool drp.txt
drop table $p;
spool off;
exit;
EOF
echo "There is an issue with taking automated backup so please check and take the invalid count manually"
fi