Category: Bash Shell


PostgreSQL DB Version Control with apgdiff

January 29th, 2012 — 9:05pm

apgdiff (Another PostgreSQL Diff Tool) is so easy to setup and use.

# you need at least Java 1.6

$ java -version

java version “1.6.0_29″
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-10M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)

# download binary and that’s it.

$ curl -O http://apgdiff.startnet.biz/download/apgdiff-2.3-bin.zip
$ unzip apgdiff-2.3-bin.zip

# create SQL dump files

$ pg_dump –host HOST_IP –port 5432 -U DB_USER -s -f new.sql ALPHA_DB
$ pg_dump –host HOST_IP –port 5432 -U USER -s -f old.sql PROD_DB

# generate upgrade statement

$ PATH_TO_APGDIFF=/tmp/apgdiff
$ java -jar $PATH_TO_APGDIFF/apgdiff-2.3.jar –ignore-start-with old.sql new.sql > upgrade.sql

Comment » | Bash Shell, mac, OS X, PostgreSQL

Install Python2.7.2, apache, wsgi on Ubuntu 10.04 LTS

September 27th, 2011 — 7:30pm

My initial attept failed miserably as you can see my post from here

After bunch of help from many intelligent people I successfully setup Python2.7.2, apache, wsgi for Ubunut 10.04 LTS

++++++++++++++++++++++++++++++
+ References:
+ 1. How to install 2.7 comment made by Alen here
+ 2. How to install wsgi
++++

# ==== Setup Ubuntu 10.04 on VMWare # on server:
# install basic stuff…
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install openssh-server # only if you want to use ssh
sudo apt-get install vim # only if you like vim….
sudo apt-get install libpq-dev
sudo apt-get install curl # only if you like over wget.. I am just so used to curl over wget since os-x (my dev machine) doesn’t have wget
sudo apt-get install build-essential

##### # Install Python 2.7.2 #####

sudo vim /etc/apt/sources.list

# add the following 2 lines at the bottom of the file:

deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main
deb-src http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main

# issue the following command:

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys DB82666C
sudo apt-get update
sudo apt-get install python2.7 python2.7-dev

# verify instllation

which python >>/usr/local/bin/python
python -V >> Python 2.7.2

# verify the linkage

ldd /usr/local/bin/python2.7

##### # Install Apache #####
# you need to install apache2-dev for wsgi installation

sudo apt-get install apache2 apache2-dev

# then add domain.

sudo vim /etc/apache2/conf.d/fqdn
# add: “ServerName localhost” without quotes

################################
# install wsgi
################################
# if wsgi is installed via apt-get then purge it:

sudo apt-get purge libapache2-mod-wsgi

mkdir tmp # or wherever you want to compile
cd tmp
curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz tar xzf mod_wsgi-3.3.tar.gz
rm mod_wsgi-3.3.tar.gz
cd mod_wsgi-3.3 ./configure –with-python=/usr/local/bin/python make sudo make install
# the load declaration for the module needs to go wsgi.load
sudo vim /etc/apache2/mods-available/wsgi.load
# then add the following line: “LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so” without quotes
# Then you have to activate the wsgi module with:
sudo a2enmod wsgi

# That’s it!

Comment » | apache, Bash Shell, Django, Linux, mod_wsgi, Python, Ubuntu

How to get IP address on your mac from Terminal

August 2nd, 2011 — 8:40pm

This will give you ethernet one

ifconfig en0 | grep ‘inet ‘ | cut -d ‘ ‘ -f2

This will give you airport one

ifconfig en1 | grep ‘inet ‘ | cut -d ‘ ‘ -f2

so if you are writing script to get your IP in either case you could:

MYIP=`ifconfig en0 | grep ‘inet ‘ | cut -d ‘ ‘ -f2`
if [$MYIP -eq '']; then
MYIP=`ifconfig en1 | grep ‘inet ‘ | cut -d ‘ ‘ -f2`
fi

MYIP=`ifconfig en0 | grep ‘inet ‘ | cut -d ‘ ‘ -f2`if [$MYIP -eq '']; then MYIP=`ifconfig en1 | grep ‘inet ‘ | cut -d ‘ ‘ -f2`fi

echo $MYIP

Comment » | Bash Shell, mac, OS X

Backup File Cleanup task with Shell Script

January 17th, 2011 — 9:53am
How to check whether backup file newer than 3 days ago exist and if so delete older than 3 days via Shell Script?
#!/bin/bash
system_admin_email=sysadmin@email.com;
# check whether backup newer than 3 days ago exist…
filesOnBackup=`find ${network_volume} -name “*backup.gz” -type f -mtime -3`
echo ${filesOnBackup}  >> $logfile
if [ "${filesOnBackup}" != "" ]; then
echo “Deleting old backup files…”  >> $logfile
if [ -d ${backup_dir} ]; then
find ${backup_dir} -name “*backup.gz” -type f -mtime +3 -delete;
echo “done”;  >> $logfile
fi
else
echo “backup doesn’t exist on BU volume”  >> $logfile
SUBJECT=”backup file doesn’t exist on BU volume”
TO=”${system_admin_email}”
echo $SUBJECT | mail -s “$SUBJECT” ${system_admin_email}
fi

Comment » | Bash Shell, System Admin

strace for Mac OS X

August 13th, 2010 — 11:16pm

In Leopard it’s called dtrace, but it’s simpler if you just call dtruss

Here is the manual:

NAME
dtruss – process syscall details. Uses DTrace.
-
SYNOPSIS
dtruss [-acdeflhoLs] [-t syscall] { -p PID | -n name | command }
-
DESCRIPTION
dtruss prints details on process system calls. It is like a DTrace ver-
sion of truss, and has been designed to be less intrusive than truss.
Of particular interest is the elapsed times and on cpu times, which can
identify  both  system calls that are slow to complete, and those which
are consuming CPU cycles.
Since  this  uses  DTrace,  only  the  root  user  or  users  with  the
dtrace_kernel privilege can run this command.
-
OPTIONS
-a     print all details
-b bufsize
dynamic  variable  buffer  size.  Increase  this  if  you notice
dynamic  variable  drop  errors.  The  default  is  ”4m”  for  4
megabytes per CPU.
-c     print system call counts
-d     print relative timestamps, us
-e     print elapsed times, us
-f     follow children as they are forked
-l     force printing of pid/lwpid per line
-L     don’t print pid/lwpid per line
-n name
examine processes with this name
-o     print on-cpu times, us
-s     print stack backtraces
-p PID examine this PID
-t syscall
examine this syscall only
-
EXAMPLES
run and examine the “df -h” command
# dtruss df -h
examine PID 1871
# dtruss -p 1871
examine all processes called “tar”
# dtruss -n tar
run test.sh and follow children
# dtruss -f test.sh
run the “date” command and print elapsed and on cpu times,
# dtruss -eo date
-
FIELDS
PID/LWPID
Process ID / Lightweight Process ID
RELATIVE
relative  timestamps  to  the start of the thread, us (microsec-
onds)
ELAPSD elapsed time for this system call, us
CPU    on-cpu time for this system call, us
SYSCALL(args)
system call name, with arguments (some may be evaluated)
-
DOCUMENTATION
See the DTraceToolkit for further documentation under the  Docs  direc-
tory. The DTraceToolkit docs may include full worked examples with ver-
bose descriptions explaining the output.
-
EXIT
dtruss will run forever until Ctrl-C is hit, or if a command  was  exe-
cuted dtruss will finish when the command ends.
-
AUTHOR
Brendan Gregg [Sydney, Australia]
-
SEE ALSO
procsystime(1M), dtrace(1M), truss(1)

Comment » | Bash Shell

PostgreSQL: Dump Schema Only or Triggers Only

July 17th, 2010 — 2:59pm

I needed to extract triggers only, sequence/index only etc when we migrated database.
I received advice from postgresql General List and thought I should post exact step/script of how to accomplish this.

Open Terminal:

1. dump schema only

pg_dump -h localhost -p 5432 –username UserName –schema-only –format c –file path_to_backup.backup –schema ‘public’ db_name

2. create list of item that you want to extract
2.1. TRIGGERS only

pg_restore –list “path_to_backup.backup” | awk ‘/[0-9]*; [0-9]* [0-9]* (TRIGGER)/ { print }’ > “path_to_triggers.list”

2.2. SEQUENCE, INDEX

pg_restore –list “path_to_backup.backup” | awk ‘/[0-9]*; [0-9]* [0-9]* (SEQUENCE|INDEX)/ { print }’ > “path_to_seq.list”

3. Create SQL script from the list
3.1. TRIGGERS only

pg_restore -L path_to_triggers.list -f dest_path_triggers.sql path_to_backup.backup

3.2. SEQUENCE, INDEX

pg_restore -L path_to_seq.list -f dest_path_seq.sql path_to_backup.backup

Comment » | Bash Shell, PostgreSQL

Back to top