| 1 |
#!/bin/bash |
|---|
| 2 |
# Copyright (c) 2007 OmniTI, Inc. All rights reserved. |
|---|
| 3 |
# This is released for use under the same license as PostgreSQL itself. |
|---|
| 4 |
|
|---|
| 5 |
# What you need: Solaris 10, a working postgres PITR slave with data in a |
|---|
| 6 |
# ZFS filesystem |
|---|
| 7 |
|
|---|
| 8 |
# You need a zone that is configured identically to the zone running |
|---|
| 9 |
# the postgres PITR slave. |
|---|
| 10 |
# |
|---|
| 11 |
# We have our globalzone accessing postgres data in: |
|---|
| 12 |
# /data/postgres/82 @ store2/postgres/82 on ZFS |
|---|
| 13 |
# with xlogs in /data/postgres/82_xlogs on a different ZFS mount. |
|---|
| 14 |
# All our incoming (master) WAL logs are in /data/postgres/82_walarchives |
|---|
| 15 |
# It is important to note that by copying the contents of the /data/postgres/82 |
|---|
| 16 |
# filesystem, we _do not_ get xlogs or WALs. |
|---|
| 17 |
|
|---|
| 18 |
# The zone has a dataset added called "pool/zonename" (store2/clonedb) here. |
|---|
| 19 |
# We will clone the PITR slave's data into the zones dataset, mount it up |
|---|
| 20 |
# and let it spin... There are a few other steps, but we'll take care of that |
|---|
| 21 |
# too. |
|---|
| 22 |
|
|---|
| 23 |
ZONE=clonedb |
|---|
| 24 |
SRCDATA=store2/postgres/82 |
|---|
| 25 |
DSTDATA=store2/clonedb/82 |
|---|
| 26 |
|
|---|
| 27 |
DATAMOUNT=/data/postgres/82 |
|---|
| 28 |
WALS=/data/postgres/82_walarchives |
|---|
| 29 |
|
|---|
| 30 |
ZONEDO="zlogin $ZONE" |
|---|
| 31 |
ZONEPATH=`zonecfg -z clonedb info zonepath | awk '{print $2;}'` |
|---|
| 32 |
|
|---|
| 33 |
WAL_NEEDED=`ls -rt $WALS | tail -1` |
|---|
| 34 |
|
|---|
| 35 |
log() { |
|---|
| 36 |
NOW=`date` |
|---|
| 37 |
echo "[$NOW] $*" |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
# Stop postgres |
|---|
| 41 |
log "Stopping postgres in $ZONE" |
|---|
| 42 |
$ZONEDO svcadm disable -s postgres |
|---|
| 43 |
# Drop the old copy |
|---|
| 44 |
log "Dropping clone and base snapshot" |
|---|
| 45 |
$ZONEDO zfs destroy $DSTDATA |
|---|
| 46 |
zfs destroy $SRCDATA@clonebase |
|---|
| 47 |
|
|---|
| 48 |
# Snap the source. |
|---|
| 49 |
log "Snapshot $SRCDATA" |
|---|
| 50 |
zfs snapshot $SRCDATA@clonebase |
|---|
| 51 |
|
|---|
| 52 |
# Clone the data |
|---|
| 53 |
log "Clone to $DSTDATA" |
|---|
| 54 |
zfs clone $SRCDATA@clonebase $DSTDATA |
|---|
| 55 |
|
|---|
| 56 |
log "Mount $DSTDATA at $DATAMOUNT in $ZONE" |
|---|
| 57 |
$ZONEDO zfs mount $DSTDATA |
|---|
| 58 |
$ZONEDO zfs set mountpoint=$DATAMOUNT $DSTDATA |
|---|
| 59 |
log "Copy last WAL [$WAL_NEEDED]" |
|---|
| 60 |
cp -p $WALS/$WAL_NEEDED $ZONEPATH/root/$WALS/$WAL_NEEDED |
|---|
| 61 |
$ZONEDO touch $DATAMOUNT/failover |
|---|
| 62 |
log "Make it active [induce failover]" |
|---|
| 63 |
$ZONEDO find $DATAMOUNT/pg_log/. -name postgres\*log -exec rm {} \\\; |
|---|
| 64 |
log "Start postgres in $ZONE" |
|---|
| 65 |
$ZONEDO svcadm enable postgres |
|---|
| 66 |
|
|---|
| 67 |
sleep 1 |
|---|
| 68 |
TRIES=200 |
|---|
| 69 |
while [ $TRIES -gt 0 ] |
|---|
| 70 |
do |
|---|
| 71 |
STATUS=`echo "select 'database system is up';" | \ |
|---|
| 72 |
$ZONEDO psql postgres postgres 2>&1 | \ |
|---|
| 73 |
grep 'database system is' | |
|---|
| 74 |
sed -e 's/^ *//; s/ *$//;'` |
|---|
| 75 |
if [ -z "$STATUS" ]; then |
|---|
| 76 |
log "Error: $STATUS" |
|---|
| 77 |
exit |
|---|
| 78 |
elif [ "$STATUS" = "database system is up" ]; then |
|---|
| 79 |
log "System up" |
|---|
| 80 |
exit |
|---|
| 81 |
fi |
|---|
| 82 |
TRIES=$(($TRIES - 1)) |
|---|
| 83 |
done |
|---|
| 84 |
|
|---|
| 85 |
log "Timeout waiting for system to come up, please investigate" |
|---|