Thursday, 28 February 2013

Linux For DBA

Useful Linux command




1) Find and Replace (Replace askapache with htaccess)
nice -n19 sh -c 'S=askapache && R=htaccess; find . -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e s%${S}%${R}% FF'


2) Disk Use in current Location
du -a --max-depth=1 | sort -n | cut -d/ -f2 | sed '$d' | while read i; do if [ -f $i ]; then du -h "$i"; else echo "$(du -h --max-depth=0 "$i")/"; fi; done


3) Do a search-and-replace in a file after making a backup
for file in <filename>; do cp $file{,.bak} && sed 's/old/new/g' $file.bak > $file; done


4) Delete all but the latest 5 files
ls -t | awk 'NR>5 {system("rm \"" $0 "\"")}'

5) Identify long lines in a file
awk 'length>72' file

6) Show directories in the PATH, one per line
echo src::${PATH} | awk 'BEGIN{pwd=ENVIRON["PWD"];RS=":";FS="\n"}!$1{$1=pwd}$1!~/^\//{$1=pwd"/"$1}{print $1}'

7) Internet access at Current Moment
lsof -P -i -n
netstat -lantp | grep -i stab | awk -F/ '{print $2}' | sort | uniq


watch --interval 0 'iptables -nvL | grep -v "0 0"'

8) Client Ip Enquiry
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

9) Remove Garbage
echo "http%3A%2F%2Fwww.google.com" | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e

10) Send Mail with attachment
tar cvzf - data1 data2 | uuencode data.tar.gz | mail -s 'data'

11) Email HTML content
mailx bar@foo.com -s "HTML Hello" -a "Content-Type: text/html" < body.htm

12) Send email with one or more binary attachments
echo "Body goes here" | mutt -s "A subject" -a /path/to/file.tar.gz recipient@example.com


du -h -s -x * | sort -g -b -r | less -F

13) Archive every file in /var/logs
find /var/logs -name * | xargs tar -jcpf logs_`date +%Y-%m-%e`.tar.bz2

14) Search for all files that begin with . and delete them.
find ~/Desktop/ \( -regex '.*/\..*' \) -print -exec rm -Rf {} \;

15) Copy all documents PDF in disk for your home directory
for i in `find / -name *.pdf`; do cp -v $i $HOME; done

16) simulates the DOS tree command that you might be missing on your Mac or Linux box
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

find . -name "*.sql" -print0 | wc -l --files0-from=-

17) Find file changed in last 7 days. Use +7 files modified before 7 days
find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
gzip weekly_incremental.tar

18) Date function

date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; date -d '16 hour'

date +%s

date +%Y-%m-%d

Curl and egrep Stuffs



Curl and egrep Stuffs
egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt


egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'

Check mail on Linux prompt:
curl -u <gmail username>:<password> --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"

curl -u <gmail username>:<password> --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title>.*<name>(.*)<\/name>.*$/$2 - $1/'

curl -u <gmail username>:<password> --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | perl -pe 's/^<title>(.*)<\/title>.*?<name>(.*?)<\/name>.*$/$2 - $1/'


cd /data1/app/oracle/diag/tnslsnr/domU-12-31-38-02-59-A1/listener/alert
find . -mtime -1 -type f | xargs cat * | grep '07-OCT-2009' | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'  | sort | uniq -c | sort -k1 | awk '{
                      switch($2){
                      case "10.211.X.X":
                      {print "node1(10.211.X.X) :=> "$1}
                  case "10.211.71.X":
                      {print "node2(10.211.71.X) :=> "$1}
                  case "10.252.107.X":
                      {print "node31(10.252.107.X) :=> "$1}
                  case "10.254.23.X":
                      {print "node4(10.254.23.X) :=> "$1}
                      default:
                          {print "OTHER $2 :=> "$1}
                  }
                      }'


egrep -o '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b' access.log | sort -u

if ($2 == "10.211.X.X") {printf "%-30s:=>%-10d node1 \n",$2,$1}
                      else if ($2 == "10.211.X.X") {printf "%-30s:=>%-10d Node2 \n",$2,$1}
                      else if ($2 == "10.252.X.X") {printf "%-30s:=>%-10d Node3 \n",$2,$1}
                      else if ($2 == "10.254.X.X") {printf "%-30s:=>%-10d Node4 \n",$2,$1}
                      else {printf "%-30s:=>%-10d Other \n",$2,$1}
                      }'

Oracle-11g Installation Using response file



cat pkgs

binutils
compat-libstdc++
elfutils-libelf
elfutils-libelf-devel
glibc
glibc-common
glibc-devel
gcc
gcc-c++
libaio-devel
libaio
libgcc
libstdc++
libstdc++-devel
make
sysstat
unixODBC
unixODBC-devel

for pkg in `cat pkgs` ; do rpm -q $pkg --queryformat '%{NAME} %{VERSION} %{ARCH}\n' ; done

--- invonment
--- Airtel 64 bit server

package compat-libstdc++ is not installed
rpm -ivh compat-libstdc++-33-3.2.3-61.x86_64.rpm

package elfutils-libelf-devel is not installed
rpm -ivh elfutils-libelf-devel-static-0.125-3.el5.x86_64.rpm elfutils-libelf-devel-0.125-3.el5.x86_64.rpm

package glibc-devel is not installed
rpm -ivh kernel-headers-2.6.18-92.el5.x86_64.rpm glibc-headers-2.5-24.x86_64.rpm glibc-devel-2.5-24.x86_64.rpm

package gcc is not installed
rpm -ivh libgomp-4.1.2-42.el5.x86_64.rpm gcc-4.1.2-42.el5.x86_64.rpm

package gcc-c++ is not installed
rpm -ivh libstdc++-devel-4.1.2-42.el5.x86_64.rpm gcc-c++-4.1.2-42.el5.x86_64.rpm

package libaio-devel is not installed
rpm -ivh libaio-devel-0.3.106-3.2.x86_64.rpm

--package libstdc++-devel is not installed
package sysstat is not installed
rpm -ivh sysstat-7.0.2-1.el5.x86_64.rpm

package unixODBC is not installed
package unixODBC-devel is not installed
rpm -ivh unixODBC-devel-2.2.11-7.1.x86_64.rpm unixODBC-2.2.11-7.1.x86_64.rpm


-- 64bit
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 5368709120
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586

/etc/security/limits.conf file:

# Oracle Specific
oracle              soft    nproc   2047
oracle              hard    nproc   16384
oracle              soft    nofile  1024
oracle              hard    nofile  65536

/etc/pam.d/login
# Oracle Specific
session    required     pam_limits.so


/etc/selinux/config
SELINUX=disabled

groupadd oinstall
groupadd dba
groupadd oper
groupadd asmadmin

useradd -g oinstall -G dba,oper,asmadmin -c "Oracle user for Airtel DB 64-bit" oracle
passwd oracle

AirOra

mkdir -p /opt/app/oracle/product/11.2.0/db_1
chown -R oracle:oinstall /opt
chmod -R 775 /opt

env|grep ORA

TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
ORACLE_SID=AIRDB
ORACLE_BASE=/opt/app/oracle
ORACLE_HOME=/opt/app/oracle/product/11.2.0/db_1
PATH=$ORACLE_HOME/bin:$PATH; export PATH
export ORACLE_SID
export ORACLE_BASE
export ORACLE_HOME

if [ $USER = "oracle" ]; then
  if [ $SHELL = "/bin/ksh" ]; then
    ulimit -p 16384
    ulimit -n 65536
  else
    ulimit -u 16384 -n 65536
  fi
fi

3)##########################################################################
ACTUALLY NEED TO SET in response file

FROM_LOCATION="/home/oracle/oracle/database/stage/products.xml"
ORACLE_BASE="/opt/app/oracle"
ORACLE_HOME="/opt/app/oracle/product/11.2.0/db_1"
ORACLE_HOME_NAME="Ora11gR1HOME1"
SHOW_SPLASH_SCREEN=FALSE
s_globalDBName="AIRDB"
s_dbSid="AIRDB"
s_superAdminSamePasswd="airorcl"
s_superAdminSamePasswdAgain="airorcl"
n_configurationOption=3

Below are optional
RESPONSEFILE_VERSION=2.2.1.0.0
FROM_LOCATION="/home/oracle/oracle/database/stage/products.xml"
ORACLE_BASE="/opt/app/oracle"
ORACLE_HOME="/opt/app/oracle/product/11.2.0/db_1"
ORACLE_HOME_NAME="Ora11gR1HOME1"
TOPLEVEL_COMPONENT={"oracle.server","11.1.0.6.0"}
DEINSTALL_LIST={"oracle.server","11.1.0.6.0"}
SHOW_SPLASH_SCREEN=FALSE
SHOW_WELCOME_PAGE=false
SHOW_COMPONENT_LOCATIONS_PAGE=false
SHOW_CUSTOM_TREE_PAGE=false
SHOW_SUMMARY_PAGE=true
SHOW_INSTALL_PROGRESS_PAGE=true
SHOW_REQUIRED_CONFIG_TOOL_PAGE=true
SHOW_CONFIG_TOOL_PAGE=true
SHOW_RELEASE_NOTES=true
SHOW_ROOTSH_CONFIRMATION=true
SHOW_END_SESSION_PAGE=true
SHOW_EXIT_CONFIRMATION=true
NEXT_SESSION=false
NEXT_SESSION_ON_FAIL=false
SHOW_DEINSTALL_CONFIRMATION=true
SHOW_DEINSTALL_PROGRESS=true
ACCEPT_LICENSE_AGREEMENT=true
COMPONENT_LANGUAGES={"en"}
SELECTED_LANGUAGES={"en"}
INSTALL_TYPE="EE"
s_nameForDBAGrp="dba"
s_nameForOPERGrp="dba"
n_dbType=1
s_globalDBName="AIRDB"
s_dbSid="AIRDB"
b_loadExampleSchemas=true
b_disableSecureConfig=false
n_configTabSelected=0
n_dbCharSetOption=3
b_useDBControl=true
b_receiveEmailNotification=false
s_dlgEMSMTPServer="_NOT_AVAILABLE_"
s_dlgEMEmailAddress="_NOT_AVAILABLE_"
s_dlgEMCentralAgentSelected="N/A"
n_dbStorageType=1
s_rawDeviceMapFileLocation="N/A"
b_enableAutoBackup=false
b_useFileSystemForRecovery=true
s_dlgRBORecoveryLocation="N/A"
s_dlgRBOUsername="N/A"
s_dlgRBOPassword="N/A"
b_useSamePassword=true
s_superAdminPasswdType=S
s_superAdminSamePasswd="airorcl"
s_superAdminSamePasswdAgain="airorcl"
sl_superAdminPasswds={}
sl_superAdminPasswdsAgain={}
n_performUpgrade=0
n_upgradeDB=0
s_dbOHSelectedForUpgrade=""
s_dbSIDSelectedForUpgrade=""
n_upgradeASM=0
s_dbSelectedUsesASM="No"
sl_upgradableSIDBInstances={}
n_dbSelection=0
b_stateOfUpgradeASMCheckbox=false
n_configurationOption=3
s_ASMSYSPassword=""
s_ASMSYSPasswordAgain=""
s_dbRetChar="WE8ISO8859P1"
SELECTED_LANGUAGES={"en"}
s_dlgASMCfgDiskGroupName="DATA"
s_dlgASMCfgDiskDiscoveryString=""
n_dlgASMCfgRedundancySelected=2
b_centrallyManageASMInstance=true
sl_dlgASMDskGrpSelectedGroup={" "," "," "," "}
sl_dlgASMCfgSelectableDisks={}
sl_dlgASMCfgDiskSelections={}
s_dlgASMCfgRedundancyValue=""
s_dlgASMCfgExistingFreeSpace="0"
s_dlgASMCfgNewDisksSize=""
s_dlgASMCfgAdditionalSpaceNeeded="-100 MB"


[oracle@TEST11g data1]$ ./runInstaller -silent -responseFile /home/oracle/oracle/database/resfile_11g.res

Setup in progress (Wed May 19 20:00:05 IST 2010)
...................................                             100% Done.
Setup successful

End of install phases.(Wed May 19 20:00:15 IST 2010)
WARNING:A new inventory has been created in this session. However, it has not yet been registered as the central inventory of this system.
To register the new inventory please run the script '/opt/app/oraInventory/orainstRoot.sh' with root privileges.
If you do not register the inventory, you may not be able to update or patch the products you installed.
The following configuration scripts need to be executed as the "root" user.
#!/bin/sh
#Root script to run
/opt/app/oraInventory/orainstRoot.sh
/opt/app/oracle/product/11.2.0/db_1/root.sh
To execute the configuration scripts:
    1. Open a terminal window
    2. Log in as "root"
    3. Run the scripts

The installation of Oracle Database 11g was successful.
Please check '/opt/app/oraInventory/logs/silentInstall2010-05-19_07-46-04PM.log' for more details.

Error
[oracle@DB bin]$ ./sqlplus
./sqlplus: error while loading shared libraries: /opt/app/oracle/product/11.2.0/db_1/lib/libnnz11.so: cannot restore segment prot after reloc: Permission denied


resolution
[root@DB oracle]# setenforce 0




[oracle@TEST11g dbs]$ orapwd file=airdbPWD password=airorcl entries=5
---this is wrong gives
ERROR:
ORA-01031: insufficient privileges
It should be
[oracle@TEST11g dbs]$ orapwd file=orapwairdb password=airorcl entries=5





mkdir -p /data1/app/oracle/admin/airdb /data3/app/oracle/oradata/airdb /data1/app/oracle/oradata/airdb /data3/app/oracle/flash_recovery_area /data3/app/oracle/arch/airdb /data1/app/oracle/admin/airdb/adump

chown -R oracle.oinstall /data1 /data2 /data3

Configure init.ora file

#Instance dependence Parameter
#airdb.__db_cache_size=2885681152
#airdb.__java_pool_size=67108864
#airdb.__large_pool_size=67108864
#airdb.__oracle_base='/data1/app/oracle'#ORACLE_BASE set from environment
#airdb.__pga_aggregate_target=2617245696
#airdb.__sga_target=3825205248
#airdb.__shared_io_pool_size=0
#airdb.__shared_pool_size=738197504
#airdb.__streams_pool_size=0
memory_target=5g
memory_max_target=8g
*.audit_file_dest='/data1/app/oracle/admin/airdb/adump'
*.audit_trail='db'
*.compatible='11.1.0.0.0'
*.control_files='/data1/app/oracle/oradata/airdb/control01.ctl','/data2/app/oracle/oradata/airdb/control02.ctl','/data3/app/oracle/oradata/airdb/control03.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='airdb'
*.db_recovery_file_dest='/data3/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=214748364800
*.diagnostic_dest='/data1/app/oracle'
*.job_queue_processes=1000
#*.log_archive_dest_1='LOCATION=/data3/app/oracle/arch/airdb'
#*.log_archive_dest_state_1='enable'
#*.log_archive_format='airdb_%t_%s_%r.arc'
#*.memory_target=6442450944
*.open_cursors=800
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
#*.sga_target=5368709120
*.undo_tablespace='UNDOTBS1'