New resilver-step2.ksh
  1 #!/usr/bin/ksh
  2 #
  3 # CDDL HEADER START
  4 #
  5 # The contents of this file are subject to the terms of the
  6 # Common Development and Distribution License (the License).
  7 # You may not use this file except in compliance with the License.
  8 #
  9 # You can obtain a copy of the license at usr/src/CDDL.txt
 10 # or http://www.opensolaris.org/os/licensing.
 11 # See the License for the specific language governing permissions
 12 # and limitations under the License.
 13 #
 14 # When distributing Covered Code, include this CDDL HEADER in each
 15 # file and include the License file at usr/src/CDDL.txt.
 16 # If applicable, add the following below this CDDL HEADER, with the
 17 # fields enclosed by brackets [] replaced with your own identifying
 18 # information: Portions Copyright [yyyy] [name of copyright owner]
 19 #
 20 # CDDL HEADER END
 21 #
 22 
 23 #
 24 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 25 # Use is subject to license terms.
 26 #
 27 
 28 # ident   "%Z%%M% %I%     %E% SMI"
 29 
 30 # Script to copy the current remaining WAL logs from the standby after the resilver-step1 
 31 # script has run.
 32 # It assumes that PostgreSQL is configured with an appropriate archive command and 
 33 # with a recovery.conf file.
 34 #
 35 # The resilver-step2 script has to be called as the postgres user and has some assuptions 
 36 # about the PostgreSQL configuration:
 37 #
 38 # 1.    The file postgresql.conf is linked to an other directoy than PGDATA
 39 #       example: postgresql.conf -> ../conf/postgresql.conf.
 40 # 2.    The file recovery.conf/recovery.done is linked to an other directoy than PGDATA
 41 #       example: recovery.conf -> ../conf/recovery.conf.
 42 # 3.    Every other configration file in PGDATA which has to differ between
 43 #       the designated primary and the designte standby is linked to an other directory
 44 #       than PGDATA.
 45 # 4.    The Postgres users on the primary and on the standby are identical and trust
 46 #       each other on a ssh login without password request.
 47 # 5.    It assumes that PostgreSQL is configured with an appropriate archive command and
 48 #       with an appropriate recovery.conf/done file.
 49 # 6.    It assumes that the PostgreSQL databases on both nodes, the designated standby and 
 50 #       the designated primaries are shut down, and all recovery.conf commands are renamed 
 51 #       to recovery.done.
 52 #
 53 # Example for the designated primary:
 54 # Archive command in postgresql.conf:
 55 # archive_command = '/usr/local/bin/rsync -arv %p standby:/pgs/82_walarchives/%f </dev/null'
 56 # Contents of recovery.conf/done:
 57 # restore_command = 'cp /pgs/82_walarchives/%f %p'
 58 #
 59 # Example for the designated secondary:
 60 #
 61 # Archive command postgresql.conf:
 62 # archive_command = '/usr/local/bin/rsync -arv %p primary:/pgs/82_walarchives/%f </dev/null'
 63 # Contents of recovery.conf/done:
 64 # restore_command = '/pgs/postgres-8.2.5/bin/pg_standby -k 10 -t /pgs/data/failover /pgs/82_walarchives %f %p'
 65 #
 66 
 67 # You have to customize the following variables:
 68 
 69 # SOURCE_DATA Is the PGDATA directry of the current node, for normal use it would be the
 70 #             designated standby node.
 71 # Example:    SOURCE_DATA=/pgs/data
 72 #
 73 # TARGET_DATA Is the PGDATA of the target node, for normal use it would be the
 74 #             designated primary node.
 75 # Example:    TARGET_DATA=/pgs/data
 76 #
 77 # TARGET      Is the name of the targe tnode. for normal usage it would be the name of the
 78 #             designated primary node.
 79 # Example:    TARGET=primary-host
 80 #
 81 # PGS_BASE    Is the Postgres Base directory, where the Postgres binaries are located
 82 # Example:    PGS_BASE=/pgs/postgres-8.2.5 for a custom build PostgreSQL
 83 # Example:    PGS_BASE=/usr/postgres/82 for a PostgreSQL delivered with Solaris 10 8/07
 84 #
 85 # PRI_GRP     Resource group which contains the cluster resource of the designated primary.
 86 # Example:    PRI_GRP=primary-rg
 87 #
 88 # STDBY_GRP   Resource group which contains the cluster resource of the designated standby.
 89 # Example:    STDBY_GRP=standby-rg
 90 #
 91 # STDBY_RS    Resource name which of the designated standby resource. This name should be unique on your standby.
 92 #             The resilver-step1 script generated the file under /var/tmp/${STDBY_RS}-resilver resilver-step2 
 93 #             requires this file.
 94 # Example:    STDBY_RS=standby-rs
 95 
 96 # ROLECHG_GRP The resource Group which contains the rolechanger resource.
 97 # Example:    ROLECHG_GRP=rolechg-rg
 98 #
 99 # PRI_NODE    The nodename of the designated primary host/zone
100 # Example:    PRI_NODE=primary-host:primary-zone
101 #
102 # RSYNC       Absolute path to the RSYNC command including the necessary options.
103 # Example:    RSYNC="/usr/local/bin/rsync -rav"
104 #
105 # SSH_PASSPHRASE        True or false wether or not your ssh key is secured by a passphrase.
106 # Example:              SSH_PASSPHRASE=false
107 
108 
109 #####  Customize the following variables ##########
110 
111 SOURCE_DATA=
112 TARGET_DATA=
113 TARGET=
114 PGS_BASE=
115 PRI_GRP=
116 STDBY_GRP=
117 STDBY_RS=
118 ROLECHG_GRP=
119 PRI_NODE=
120 RSYNC=
121 SSH_PASSPHRASE=false
122 
123 #####  End of customizations  ##########
124 
125 # generic variables
126 
127 TR=/usr/xpg4/bin/tr
128 SSH_AGENT=/usr/bin/ssh-agent
129 SSH_ADD=/usr/bin/ssh-add
130 SSH=/usr/bin/ssh
131 RM=/usr/bin/rm
132 ECHO=/usr/bin/echo
133 MV=/usr/bin/mv
134 
135 # Check if the varables are set
136 
137 custom_ok=0
138 if [ -z "${SOURCE_DATA}" ]
139 then
140         ${ECHO} "ERROR: customize the SOURCE_DATA variable"
141         custom_ok=1
142 fi
143 
144 if [ -z "${TARGET_DATA}" ]
145 then
146         ${ECHO} "ERROR: customize the TARGET_DATA variable"
147         custom_ok=1
148 fi
149 
150 if [ -z "${TARGET}" ]
151 then
152         ${ECHO} "ERROR: customize the TARGET variable"
153         custom_ok=1
154 fi
155 
156 if [ -z "${PGS_BASE}" ]
157 then
158         ${ECHO} "ERROR: customize the PGS_BASE variable"
159         custom_ok=1
160 fi
161 
162 if [ -z "${RSYNC}" ]
163 then
164         ${ECHO} "ERROR: customize the RSYNC variable"
165         custom_ok=1
166 fi
167 
168 if [ -d /etc/cluster ]
169 then
170         if [ -z "${PRI_GRP}" ]
171         then
172                 ${ECHO} "ERROR: customize the PRI_GRP variable"
173                 custom_ok=1
174         fi
175         
176         if [ -z "${STDBY_GRP}" ]
177         then
178                 ${ECHO} "ERROR: customize the STDBY_GRP variable"
179                 custom_ok=1
180         fi
181 
182         if [ -z "${STDBY_RS}" ]
183         then
184                 ${ECHO} "ERROR: customize the STDBY_RS variable"
185                 custom_ok=1
186         fi
187         
188         if [ -z "${ROLECHG_GRP}" ]
189         then
190                 ${ECHO} "ERROR: customize the ROLECHG_GRP variable"
191                 custom_ok=1
192         fi
193         
194         if [ -z "${PRI_NODE}" ]
195         then
196                 ${ECHO} "ERROR: customize the PRI_NODE variable"
197                 custom_ok=1
198         fi
199 fi
200 
201 if [ ${custom_ok} -ne 0 ]
202 then
203         exit 1
204 fi
205 
206 if [ ! -f /var/tmp/${STDBY_RS}-resilver ]
207 then
208         ${ECHO} "ERROR: run resilver-step1 first"
209         exit 1
210 fi
211 
212 # start the ssh-agent and decrypt the private key depending on the SSH_PASSPHRASE variable
213 
214 SSH_PASSPHRASE=`${ECHO} ${SSH_PASSPHRASE}|${TR} "[:upper:]" "[:lower:]"`
215 
216 if [ "${SSH_PASSPHRASE}" == "true" ]
217 then
218         eval `${SSH_AGENT} -s` >/dev/null 2>&1
219         ${SSH_ADD}
220         if [ ${?} -ne 0 ]
221         then
222                 exit 1
223         fi
224 fi
225 
226 # Configure the source to start in recovery mode.
227 
228 ${MV} ${SOURCE_DATA}/recovery.done ${SOURCE_DATA}/recovery.conf 
229 
230 # Sync the currrent wal logs.
231 
232 ${RSYNC} ${SOURCE_DATA}/pg_xlog/* ${TARGET}:${TARGET_DATA}/pg_xlog
233 if [ ${?} -ne 0 ]
234 then
235         ${ECHO} ERROR: Problems at the rsync, these need to be fixed.
236         exit 1
237 fi
238 
239 # reconfigure the target to come up in recovery mode
240 
241 ${SSH} ${TARGET} ${MV} ${TARGET_DATA}/recovery.done ${SOURCE_DATA}/recovery.conf
242 
243 
244 if [ -d /etc/cluster ]
245 then
246         ${ECHO} 
247         ${ECHO} "Step 1 As PostgreSQL user on the primary node/zone, start the target database manually "
248         ${ECHO} "       and check the logs that it gets to the ready state."
249         ${ECHO} "Step 2 As PostgreSQL user on the primary node/zone, shutdown the target database manually" 
250         ${ECHO} "       to allow the cluster to take control."
251         ${ECHO} 
252         ${ECHO} "Example:"
253         ${ECHO} "${PGS_BASE}/bin/pg_ctl -D ${TARGET_DATA} start"
254         ${ECHO} "${PGS_BASE}/bin/pg_ctl -D ${TARGET_DATA} stop -m fast"
255         ${ECHO} 
256         ${ECHO} "Step 3 Start your cluster resource groups now"
257         ${ECHO} "Example: In the global zone as root call:"
258         ${ECHO} 
259         ${ECHO} "clrg online ${PRI_GRP}"
260         ${ECHO} "clrg online ${STDBY_GRP}"
261         ${ECHO} "clrg online -n ${PRI_NODE} ${ROLECHG_GRP}"
262 else
263         ${ECHO} 
264         ${ECHO} "Step 1 As PostgreSQL user on the primary node/zone, start the target database manually "
265         ${ECHO} "       and check the logs that it gets to the ready state."
266         ${ECHO} 
267         ${ECHO} "Example:"
268         ${ECHO} "${PGS_BASE}/bin/pg_ctl -D ${TARGET_DATA} start"
269         ${ECHO} 
270         ${ECHO} "Step 2 As PostgreSQL user on the standby node/zone, start the database manually "
271         ${ECHO} "       and check the logs that it starts in recovery mode state."
272         ${ECHO} 
273         ${ECHO} "Example:"
274         ${ECHO} "${PGS_BASE}/bin/pg_ctl -D ${TARGET_DATA} start"
275 fi
276 
277 # stop the ssh-agent depending on the SSH_PASSPHRASE variable
278 
279 if [ "${SSH_PASSPHRASE}" == "true" ]
280 then
281         ${SSH_AGENT} -k >/dev/null 2>&1
282 fi
283 
284 # remove the /var/tmp/${STDBY_RS}-resilver file
285 ${RM} /var/tmp/${STDBY_RS}-resilver 2>/dev/null