OSDN Git Service

Add copyright mentions, per Tom Lane.
[pg-rex/syncrep.git] / src / bin / initlocation / initlocation.sh
1 #!/bin/sh
2 #-------------------------------------------------------------------------
3 #
4 # initlocation.sh--
5 #     Create a secondary PostgreSQL database storage area.  
6
7 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 # Portions Copyright (c) 1994, Regents of the University of California
9 #
10 #
11 # IDENTIFICATION
12 #    $Header: /cvsroot/pgsql/src/bin/initlocation/Attic/initlocation.sh,v 1.11 2001/02/18 18:33:59 momjian Exp $
13 #
14 #-------------------------------------------------------------------------
15
16 exit_nicely(){
17     echo "$CMDNAME failed."
18     rm -rf "$PGALTDATA"
19     exit 1
20 }
21
22
23 CMDNAME=`basename $0`
24 EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`
25
26 if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
27 then
28     echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
29     echo "as the (unprivileged) user that will own the server process."
30     exit 1
31 fi
32
33 Location=
34
35 while [ "$#" -gt 0 ]
36 do
37     case "$1" in
38         # These options are not really necessary, but what the heck.
39         --location=*)
40             Location=`echo $1 | sed 's/^--pgdata=//'`
41             ;;
42         --location)
43             Location="$2"
44             shift;;
45         -D)
46             Location="$2"
47             shift;;
48
49         --help|-\?)
50             usage=t
51             break;;
52
53         -*)
54             echo "$CMDNAME: invalid option: $1" 1>&2
55             echo "Try '$CMDNAME --help' for more information." 1>&2
56             exit 1
57             ;;
58         *)
59             Location="$1"
60             ;;
61         esac
62         shift
63 done
64
65
66 if [ "$usage" ]; then
67         echo "$CMDNAME initializes an alternative filesystem location for database creation."
68         echo ""
69         echo "Usage:"
70         echo "  $CMDNAME LOCATION"
71         echo
72         echo "Please read the description of the CREATE DATABASE command for details."
73         echo
74         echo "Report bugs to <pgsql-bugs@postgresql.org>."
75         exit 0
76 fi
77
78
79 if [ -z "$Location" ]; then
80         echo "$CMDNAME: missing required argument LOCATION" 1>&2
81         echo "Try '$CMDNAME -?' for help." 1>&2
82         exit 1
83 fi
84
85
86 #
87 # Here's what's going on:
88 #
89 # You can call initlocation ENVAR (no dollar sign), then ENVAR will
90 # (a) be tested whether it is valid as a path, or
91 # (b) be resolved as an environment variable.
92 # The latter has been the traditional behaviour.
93 #
94 # You can call initlocation $ENVAR, which will of course be resolved
95 # by the shell, or initlocation some/path (containing at least one slash).
96 # Then you just take that path.
97 # This should appease users who are confused by the above behavour.
98 #
99
100 echo "$Location" | grep '/' >/dev/null 2>&1
101
102 if [ "$?" -ne 0 -a ! -d "$Location" ]; then
103     PGALTDATA=`printenv $Location 2> /dev/null`
104     if [ -z "$PGALTDATA" ]; then
105         echo "$CMDNAME: environment variable $Location not set" 1>&2
106         exit 1
107     fi
108     haveenv=t
109 else
110     PGALTDATA="$Location"
111     haveenv=f
112 fi
113
114 echo "The location will be initialized with username \"$EffectiveUser\"."
115 echo "This user will own all the files and must also own the server process."
116 echo
117
118 # -----------------------------------------------------------------------
119 # Create the data directory if necessary
120 # -----------------------------------------------------------------------
121
122 # don't want to leave anything lying around
123 trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
124
125 # umask must disallow access to group, other for files and dirs
126 umask 077
127
128 if [ ! -d $PGALTDATA ]; then
129         echo "Creating directory $PGALTDATA"
130         mkdir "$PGALTDATA"
131         if [ $? -ne 0 ]; then
132             echo "$CMDNAME: could not create $PGALTDATA" 1>&2
133             echo "Make sure $PGALTDATA is a valid path and that you have permission to access it." 1>&2
134             exit_nicely
135         fi
136 else
137         echo "Fixing permissions on pre-existing directory $PGALTDATA"
138         chmod go-rwx "$PGALTDATA" || exit_nicely
139 fi
140
141
142 if [ ! -d $PGALTDATA/base ]; then
143         echo "Creating directory $PGALTDATA/base"
144         mkdir "$PGALTDATA/base"
145         if [ $? -ne 0 ]; then
146             echo "$CMDNAME: could not create $PGALTDATA/base" 1>&2
147             echo "Make sure $PGALTDATA/base is a valid path and that you have permission to access it." 1>&2
148             exit_nicely
149         fi
150 else
151         echo "Fixing permissions on pre-existing directory $PGALTDATA/base"
152         chmod go-rwx "$PGALTDATA/base" || exit_nicely
153 fi
154
155 echo
156 echo "$CMDNAME is complete."
157 # We can only suggest them these commands if they used the environment
158 # variable notation. Otherwise they would be induced to use an absolute
159 # path, which the backend won't allow by default.
160 if [ "$haveenv" = "t" ]; then
161     echo "You can now create a database using"
162     echo "  CREATE DATABASE <name> WITH LOCATION = '$Location'"
163     echo "in SQL, or"
164     echo "  createdb <name> -D '$Location'"
165     echo "from the shell."
166 fi
167 echo
168
169 exit 0