OSDN Git Service

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