Tampilkan postingan dengan label Unix. Tampilkan semua postingan
Tampilkan postingan dengan label Unix. Tampilkan semua postingan

Selasa, 03 Februari 2015

Setting up PostgreSQL on Solaris



Setting up PostgreSQL on Solaris
PostgreSQL is bundled with Solaris 10 and is available from the primary OpenSolaris IPS repository.
To check if PostgreSQL is installed in your Solaris instance you can use the following command:

$ svcs "*postgres*"
STATE          STIME    FMRI
disabled       Feb_16   svc:/application/database/postgresql:version_81
disabled       16:11:25 svc:/application/database/postgresql:version_82

Install Required Packages
If you don't see any PosgreSQL instance in your Solaris box then proceed and install the following packages (the list may actually change over time):
  • SUNWpostgr
  • SUNWpostgr-contrib
  • SUNWpostgr-devel
  • SUNWpostgr-docs
  • SUNWpostgr-jdbc
  • SUNWpostgr-libs
  • SUNWpostgr-pl
  • SUNWpostgr-server
  • SUNWpostgr-server-data
  • SUNWpostgr-tcl

Check if PostgreSQL SMF Services are Configured
After installation, SMF services should be listed by (the output may depend on the actual PostgreSQL version you installed):

$ svcs "*postgres*"
STATE          STIME    FMRI
disabled       Feb_16   svc:/application/database/postgresql:version_81
disabled       16:11:25 svc:/application/database/postgresql:version_82

On Solaris, PostgreSQL is managed by the SMF framework. If you're curious you can check the service manifest at /var/svc/manifest/application/database/postgresql.xml and the service methods at /lib/svc/method/postgresql. Many important parameters are stored in the service configuration file (postgresql.xml): if you want to change some parameters (such as PostgreSQL data directory) you must use svccfg to edit the service configuration.

PostgreSQL and RBAC
PostgreSQL on Solaris uses RBAC to give users permissions over the database instance. When you install Solaris' PostgreSQL packages an RBAC role is setup for you:

[/etc/passwd]
postgres:x:90:90:PostgreSQL Reserved UID:/:/usr/bin/pfksh
This user is setup as an RBAC role in /etc/user_attr file:

[/etc/user_attr]
postgres::::type=role;profiles=Postgres Administration,All

Permission for the Postgres Administration profiles are setup in the /etc/security/exec_attr file:

[/etc/security/exec_attr]
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/initdb:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/ipcclean:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/pg_controldata:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/pg_ctl:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/pg_resetxlog:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/postgres:uid=postgres
Postgres Administration:solaris:cmd:::/usr/postgres/8.2/bin/postmaster:uid=postgres

Starting PostgreSQL
You can start PostgreSQL using the following SMF command from an account with the appropriate privileges:

$ su - postgres
$ svcadm enable svc:/application/database/postgresql:version_82

Initial Configuration
By default, PostgreSQL is configured to trust all of the local users. That's not a good practice because all your local users may connect to PostgreSQL as a superuser. The first to do is setting up a password for the postgres user:

$ psql -U postgres
postgres=# alter user postgres with password 'your-password';
Exit psql with the \q command and change the /var/postgres/8.2/data/pg_hba.conf file to set an appropriate authentication method and change the following line:

[/var/postgres/8.2/data/pg_hba.conf]
local all all trust

with, for example:

[/var/postgres/8.2/data/pg_hba.conf]
local all all md5

Next time you connect, PostgreSQL will be asking you for the user password. Now, let's refresh the PostgreSQL service so that PostgreSQL will receive a SIGHUP signal an re-read the pg_hba.conf file:

$ svcadm refresh svc:/application/database/postgresql:version_82

Done!
You're now running a PostgreSQL instance on your Solaris box ready to be given to your database administrator, ready for production use.

Jumat, 12 Juli 2013

10 Example of find command in Unix and Linux

find command is one of the versatile command in UNIX and Linux  and I used it a lot in my day to day work. I believe having knowledge of find command in UNIX and understanding of its different usage will increase your productivity a lot in UNIX. If your works involve lots of searching stuff on Linux machine or if you are a java or C++ programmer and your code resides in UNIX, find command can greatly help you to look for any word inside your source file in the absence of an IDE, It is the alternative way of searching things in UNIX. grep is another Linux command which provides similar functionality like find but in my opinion later is much more powerful than grep in UNIX. Like any  other command strength of find is lies in its various options, which is worth learning, but to be frank hard to remember. If you can even able to remember all options mentioned in this article, you will be taking more advantage of find, than average developers and Linux users. If you love to read books,  you can also take a look at  A Practical Guide to Linux Commands, Editors, and Shell Programming (2nd Edition) By Mark G. Sobell, a great book and must read for any system adminstrator, security guy or developers, who works in UNIX based environment. It not only teaches about find and grep, but also several useful commands, which probably gone unnoticed by many of us

By the way, I have been sharing my experience on Unix and Linux command and its different options, usage and example and this article is in continuation of my earlier post like How to convert IP address to hostname in Linux . If you are new here you may find those tips useful for your day 2 day development and support work.




10 tips on find command in UNIX
Here I am listing down some of the way I use find in Unix or Linux box regularly, I hope this would help some one who is new in UNIX find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.

How to run  last executed find command in Unix – Example 1
!find will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact "!" can be used with any command to invoke previous run of that command.
k0p0nkkk@xserver ~/java : !find
find . -name "*.java"     --last find command executed
./OnlineStockTrading.java
./StockTrading.java

How to find files which has been modified less than one day, minute or hour  in Unix:
unix find command tutorialfind -mtime is used to search files based upon modification time. This is infact my favorite find command tips while looking out some production issues just to check which files have been modified recently, could be likely cause of  issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change. Along with –mtime, there are two more options related to time, find -atime which denote last accessed time of file and find –ctime denotes last changed time. + sign is used to search for greater than, - sign is used to search for less than and without sign is used for exact. For example find –mtime -1 will search all files which has been modified
k0p0nkkk@xserver ~/java : find . -mtime 1  (find all the files modified exact 1 day)
k0p0nkkk@xserver ~/java : find . -mtime -1 (find all the files modified less than 1 day)
.
./StockTrading.java
k0p0nkkk@xserver ~/java : find . -mtime +1 (find all the files modified more than 1 day)
./.vimrc
./OnlineStockTrading.java
./StockTrading.java~

In this example since we have only modified StockTrading.java some time back it has shown on find –mtime -1, rest of files are not touched today so they are appearing as modified more than 1 day while there is no file which has been modified exactly one day.

How to find all the files and directories which holds the 777 permission in Unix box – Exmaple 3
find –perm option is used to find files based upon permissions. You can used find –perm 444 to get all files which allows read to owner, group and others. If you are not sure how  those 777 and 444 numbers comes up, see my post on file and directory permission in Unix and some chmod examples to change permissions in Unix.
k0p0nkkk@xserver:~/java $ find . -perm 644
./.vimrc
./OnlineStockTrading.java
I use this find command example to find out all the executable files, you can also modify it to find all the read only files or files having write permission etc by changing permissions e.g. to find all read only files in current directory : find . –perm 555  Here "." or period denotes current directory. You can replace it with any directory you want.

Example 4 – Case insensitive search using find in Unix
How to do case insensitive search using find command in Unix? Use option “-i" with name, by default find searches are case sensitive. This option of find is extremely helpful while looking for errors and exceptions in log file.
find . –iname "error" –print ( -i is for ignore )

On a different note find and grep command is also a favorite topic during Unix interview and interview often asked questions during interviews on both system admin and application developer jobs.

UNIX find command and xargs Example

Now we will see some UNIX find command example combined with xargs command, xargs can be used to do whatever witch each file found by find command for example we can delete that file, list content of that file or can apply any comment on that file.

Example 5 -  How to delete temporary files using find command in Unix?
In order to delete files you can use either –delete option of find command or use xargs in combination. Its better to create house keeping script for such task which can perform cleanup on periodic basis.
find . -name "*.tmp" -print | xargs rm –f
Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below , also its worth considering use of -print0 to avoid problems with white space in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.

Example 6 -  How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word "MemoryCache". we can also leave -print option in all cases because its default for UNIX find command as pointed out by Ben in comments. You can further sort result of find command using Sort command in unix.
find . –name "*.txt" –print | xargs grep "Exception"

Example 7 -  Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows. –type option can be used to specifiy search for only file, link or directory and maxdepth specifies how deep find has to search.
find . -maxdepth 1 -type f -newer first_file
Another way of doing it is below:
find . -type f -cmin 15 -prune

Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)


Example 8 – How to find files based on size in Unix and Linux
Following find example shows how you can use find –size option to find files based upon certain size. This will find all files in current directory and sub-directory, greater than some size using find command in Unix:
find . -size +1000c -exec ls -l {} \;
Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as in below example of find:
find . -size +10000c -size -50000c -print

This find example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:

Example 9 – How to find files some days older and above certain size
We can combine –mtime and –size to find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. This example of find command will find which are more than 10 days old and size greater than 50K.
find . -mtime +10 -size +50000c -exec ls -l {} \;

10) You can use "awk" in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:
find . -type l -print | xargs ls -ld | awk '{print $10}'


"." says starts from current directory and include all sub directory and  "-type l" says list all links.

Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.

Tip: 
$* :    $* is one of the special bash parameter which is used to expands positional parameters from position one.
if you give double quotes and expansion is done within double quotes, it only expands to a single word and corresponding value of each parameter will be separated by the first letter of the IFS environment variable defined in bash. Do let me know how do you find these find examples .

How to use find command on file names with space in Unix:

I have received lot of comments from my readers on not mentioning about find -print0 and xargs -0 on find examples, so I thought to include this as well. When we don't specify any expression after find command the default option is -print which prints the name of each found files followed by \n or newline.since we mostly pipe output of find command to xargs -print could cause problem if file name itself contain new line or any form of white space. To resolve this issue instead of -print use -print0. Difference between find -print and find -print0 is, print0 display file name on the stdout followed by a "NUL" character and then you can use xargs -0 command to process file names with null character. let's see UNIX find command example with file name having space in them:
k0p0nkkk@xserver:~/test find . -name "*equity*" -print
./cash equity trading ./equity~
You see here "cash equity trading" has space in there name
k0p0nkkk@xserver:~/test find . -name "*equity*" -print | xargs ls -l
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
Now if we pass this to xargs, xargs treat them as three separate files.
k0p0nkkk@xserver:~/test find . -name "*equity*" -print0 | xargs ls

xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?

ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
Now to solve this we have used find command with -print0 which appends NUL character on file name but without xargs -0, xargs will not able to handle those inputs.
k0p0nkkk@xserver:~/test find . -name "*equity*" -print0 | xargs -0 ls -l
-rw-r--r-- 1 stock_trading cash_domain trading 0 Jul 21 09:54 ./cash equity trading
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
Now you can see with find -print0| xargs -0 it looks good

In conclusion always use find -print0 along with xargs -0 if you see slightest possibilities of file names containing space in UNIX or Linux.

Important point about find command in Unix and Linux:

Here are some of the important and interesting things to know about powerful find command, most of these points are contributed by various people in comments and big thanks to all of them for sharing there knowledge, you should definitely check out comments to know more about find command :
1.  find –print and find is same as –print is a default option of find command.
2.  find –print0 should be used to avoid any issue with white space in file name or path while forwarding output to xargs, also use xargs -0 along with find –print0.
3. find has an option called –delete which can be used in place of  -exec rm {} \;

Kamis, 11 Juli 2013

Solaris Flash Archives

Flash images or flar image is very useful in situations where you need cloning/imaging or crashed server recovery. The flarcreate command creates a flash archive. A flash archive can be created on a system that is running a UFS root file system or a ZFS root file system. A flash archive of a ZFS root pool contains the entire pool hierarchy except for the swap and dump volumes and any excluded datasets. The swap and dump volumes are created when the flash archive is installed.


NOTE: By default, the flarcreate command ignores items that are located in "swap" partitions.

Let's see how we can work with flar image creation.

Create the archive:


For UFS:

# flarcreate -n "Solaris 10 10/09 build" -S -c -x /var/tmp/ /var/tmp/S10-1009.ufs.archive.sun4u-`date +'%Y%m%d%H%M'`

For ZFS:

# flarcreate -n "Solaris 10 10/09 build" -S -c /var/tmp/S10-1009.zfs.archive.sun4u-`date +'%Y%m%d%H%M'`


Where -


The "-n Solaris 10 10/09 build" implants a name into the FLAR image. The name should be something unique and meaningful to better identify it as the FLAR image for the system.

The "-x /var/tmp/" option causes the /var/tmp/ directory and its contents to be excluded from the FLAR image since it will not be needed in the FLAR image.

-S option causes to skip the disk space check and do not write archive size data to the archive. Without -S, flarcreate builds a compressed archive in memory before writing the archive to disk, to determine the size of the archive. The result of the use of -S is a significant decrease in the time it takes to create an archive.

-c Tells flar to compress the archive as it's writing it.


E.g. -


# time flarcreate -n "Solaris 10 10/09 build" -S -c /var/tmp/S10-1009.zfs.archive.sun4u-`date '+%m-%d-%y'`


Full Flash
Checking integrity...
Integrity OK.
Running precreation scripts...
Precreation scripts done.
Creating the archive...
Archive creation complete.
Running postcreation scripts...
Postcreation scripts done.


Running pre-exit scripts...
Pre-exit scripts done.


real 19m58.57s
user 13m42.99s
sys 1m55.48s



# ls -l /var/tmp/S10-1009.zfs.archive.sun4u*
-rw-r--r-- 1 root root 5339709933 Oct 14 04:54 /var/tmp/S10-1009.zfs.archive.sun4u-10-14-10


# flar info /var/tmp/S10-1009.zfs.archive.sun4u-10-14-10
archive_id=2f27a01690ce4fcaf398e638fcdcb66e
files_archived_method=cpio
creation_date=20101014093417
creation_master=XXXXXX
content_name=Solaris 10 10/09 build
creation_node=XXXXXXXX
creation_hardware_class=sun4u
creation_platform=SUNW,Sun-Fire-V240
creation_processor=sparc
creation_release=5.10
creation_os_name=SunOS
creation_os_version=Generic_142900-09
rootpool=rpool
bootfs=rpool/ROOT/s10s_u8wos_08a_Pre-patch
snapname=zflash.101014.04.10
files_compressed_method=compress
content_architectures=sun4c,sun4d,sun4m,sun4u,sun4s,sun4us
type=FULL


Also we can have a small shell script to create flar image -


#!/bin/sh
echo
echo Enter image name, i.e. Solaris build e.g. S10-1009.ufs.archive.sun4v
read ANS
echo "Image Name: ${ANS}" > /etc/image_catalog
echo "Image Created on: `date`" >> /etc/image_catalog
echo "Image Created by: `/usr/ucb/whoami` on `hostname`" >> /etc/image_catalog

#
# Clean up wtmpx so that new machine won't have last logins
#
cat /dev/null > /var/adm/wtmpx
#
# Now create flar, excluding -x /var/tmp/
#
flarcreate -n ${ANS} -c -a `/usr/ucb/whoami` -x /var/tmp/ /var/tmp/${ANS}_`date +'%Y%m%d%H%M'`

Rabu, 29 Mei 2013

Change Shell To Bash

For those of us that are stuck working in Solaris environments with either csh or ksh as the default shell, there is hope. Some of these environments have the chsh command to change shells, but for those that don't, here is how to change your shell to bash. First of all, make sure it is available on your system. It's typically found in /bin/bash. If it's there, you can edit your ~/.profile to make it run the new shell when you login.
IMPORTANT: Before doing this, open two separate shells. Keeping one open and unaltered is a good way to recover if something goes wrong in this process. If you break something and can't log in, you may piss off your system admin when you have to call him to fix it for you. Leave one of these open shells alone during this process.
In one of your open shells, add the following two lines to the bottom of your ~/.profile.
export SHELL=/bin/bash
exec $SHELL
You can now log out of that shell and back in again to see if it worked. If so, all went well and you can close the other shell. If not, use your "safety" shell to repair your ~/.profile so that you can log in again.

Selasa, 21 Mei 2013

How to check an open port on Solaris

One of The Solaris operating system command is "netstat" , this tools function is to show the status of network activity, including open ports. When used alone, the "netstat -an" command will show all the open ports on the system. The "-a" option shows the state of all the ports. The "-n" option shows the network addresses as numbers rather than symbols. Combined with the "grep" command, you can see the status of a specific port.(use --help for complete detail )

Here is the example how to use it :
  • ·         Type the command "netstat -an" to obtain a list of all the open ports.  
  • ·         Type the command "netstat -an | grep 8080" to determine whether port "8080" is open.
  • ·         You can replace "8080" with any specific port that you are looking for
  • ·         Type the command "exit" to close the terminal session.