Category: System Admin


PostgreSQL Utility for EnterpriseDB Installation

January 30th, 2012 — 10:00pm

PostgreSQL Installer provided by EnterpriseDB is so handy and make your life very easy for OS X Desktop installation.

Maybe I just don’t know the handy utility but I wrote a shell script to control/display various tasks hence I wanted to share this with you.

The utility contains: start | stop | restart | stopfast | stopnow | status | vac_status | show_connections | version | restore backup


NODE_NAME=`uname -n`
NODE_NAME=${NODE_NAME%%\.*local}
NODE_NUMBER=${NODE_NAME##*SQL-}
PGBIN=/Library/PostgreSQL/9.1/bin
PGDATA=/Library/PostgreSQL/9.1/data
pidof(){
# usage: `pidof`
ps -ef | grep postgres | awk '{if ($3==1)print $2}'
}
start(){
pid=`pidof`
if [ "$pid" == "" ] ; then
echo "Starting postgres..."
su - postgres -c "${PGBIN}/pg_ctl -w start -D ${PGDATA} -l ${PGDATA}/pg_log/startup.log"
else
status
fi
}
stop(){
pid=`pidof`
if [ "$pid" != "" ] ; then
echo "Stopping postgres..."
su - postgres -c "${PGBIN}/pg_ctl stop -m smart -w -D ${PGDATA}"
else
echo "Postgres is not running"
fi
}
stopfast(){
pid=`pidof`
if [ "$pid" != "" ] ; then
echo "Stopping postgres..."
su - postgres -c "${PGBIN}/pg_ctl stop -m fast -w -D ${PGDATA}"
else
echo "Postgres is not running"
fi
}
stopnow(){
pid=`pidof`
if [ "$pid" != "" ] ; then
echo "Stopping postgres..."
su - postgres -c "${PGBIN}/pg_ctl stop -m immediate -w -D ${PGDATA}"
else
echo "Postgres is not running"
fi
}
status(){
pid=`pidof`
if [ "$pid" != "" ] ; then
echo "postgres is running"
echo `ps -ef | grep postgres | grep -v grep | awk '{if ($3==1) print "Postgres: " $8 ", Data Directory: " $10}'`
else
echo "postgres is not running"
fi
}
vac_status(){
su - postgres -c "${PGBIN}/psql -c \"select relname, last_autovacuum, last_autoanalyze from pg_stat_user_tables where last_autovacuum IS NOT NULL order by last_autovacuum desc\""
}
show_connections(){
su - postgres -c "${PGBIN}/psql -c \"SELECT datname, usename, procpid, client_addr, waiting, query_start, current_query FROM pg_stat_activity;\""
}
version(){
su - postgres -c "${PGBIN}/psql -c \"SELECT version()\""
}
restore_bak(){
echo "backup file path : "
read BACKUPFILEPATH
echo "database name : "
read DBNMAE
chmod -R 777 $BACKUPFILEPATH
cat ${BACKUPFILEPATH} | gunzip | ${PGBIN}/psql -U postgres ${DBNMAE}
${PGBIN}/psql -d ${DBNMAE} -U postgres < /users.sql
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
stopfast)
stopfast
;;
stopnow)
stopnow
;;
vac_status)
vac_status
;;
show_connections)
show_connections
;;
restore_bak)
restore_bak
;;
version)
version
;;
*)
echo "Usage: $0 { start | stop | restart | stopfast | stopnow | status | vac_status | show_connections | version | restore_bak }"
exit 1
esac
exit 0

Comment » | mac, OS X, PostgreSQL, System Admin

Open SSH (Port 22) on Fedora 15

August 31st, 2011 — 6:13pm

So Fedora 15 with GNOME 3 is very beautiful. I really like it. Mac’s spotlight like search is +1.

I had a bit hard time accessing my macbook to Fedora 15 via SSH so I thought I should share this with you.

1. Modify sshd_conf file (/etc/ssh/sshd_config): Find Line “Port 22″ and uncomment the line

2. Restart sshd service by issuing 

$ service sshd stop$ service sshd restart

3. Let’s check to see if sshd is running

$ service sshd status# or you could$ ps aux|grep sshd

3. Okay, let’s test it. Go to client (my case is my macbook) and issued $ ssh username@ip_address Connect then I got “Connection Refused” Error. So looks like I need to mess with firewall (iptables).

3.1 Edit file /etc/sysconfig/iptables and add the following line. Note that this should go above the first instance of -A INPUT otherwise other rule might block you as rule will will be read from top down and stops as soon as it meets the rule.

# to open ssh
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
3.2. Save and close the file and restart iptables as follows:
$ service iptables restart
4. Then try this again: $ ssh username@ip_address Hopefully you are connected….

 

Comment » | Fedora, Linux, System Admin

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

Back to top