source: src-sh/pc-sysinstall/backend/functions-zfsrestore.sh @ 35000f6

releng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 35000f6 was 35000f6, checked in by Kris Moore <kris@…>, 16 months ago

Fix a bug pulling the correct LP props file from a remote host

  • Property mode set to 100755
File size: 5.7 KB
Line 
1#!/bin/sh
2#-
3# Copyright (c) 2013 iXsystems, Inc.  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26
27# Functions which runs commands on the system
28
29. ${BACKEND}/functions.sh
30. ${BACKEND}/functions-parse.sh
31
32do_zfs_restore()
33{
34  load_ssh_settings
35 
36  restore_zfs_from_remote
37
38  restore_stamp_grub
39
40  restore_umount_zfs
41}
42
43# Function to load & check SSH remote settings
44load_ssh_settings()
45{
46  get_value_from_cfg sshHost
47  SSHHOST="$VAL"
48  get_value_from_cfg sshPort
49  SSHPORT="$VAL"
50  get_value_from_cfg sshUser
51  SSHUSER="$VAL"
52  get_value_from_cfg sshKey
53  SSHKEY="$VAL"
54
55  if [ -z "$SSHHOST" ] ; then
56     exit_err "Missing sshHost!"
57  fi
58  if [ -z "$SSHUSER" ] ; then
59     exit_err "Missing sshUser!"
60  fi
61  if [ -z "$SSHPORT" ] ; then
62     SSHPORT="22"
63  fi
64  if [ -n "$SSHKEY" ] ; then
65     SSHKEY="-i $SSHKEY"
66  fi
67
68  # Now that the settings are loaded, lets test this connection
69  ssh -o NumberOfPasswordPrompts=0 $SSHKEY -p $SSHPORT ${SSHUSER}@${SSHHOST} "echo ''"
70  if [ $? -ne 0 ] ; then
71     exit_err "Failed SSH connection to ${SSHHOST}"
72  fi
73
74  export SSHKEY SSHPORT SSHUSER SSHHOST
75}
76
77restore_zfs_from_remote()
78{
79  get_value_from_cfg zfsProps
80  ZFSPROPS="$VAL"
81  if [ -z "$ZFSPROPS" ] ; then
82     exit_err "No zfsProps= specified!"
83  fi
84
85  ZPFILE="${TMPDIR}/.zfs-restore-props"
86
87  # First lets fetch the ZFS properties backup
88  echo "ssh -p ${SSHPORT} ${SSHKEY} ${SSHUSER}@${SSHHOST} \"cat '$ZFSPROPS'\" > ${ZPFILE}" > ${TMPDIR}/.fetchProp
89  sh ${TMPDIR}/.fetchProp
90  if [ $? -ne 0 ] ; then
91     rm ${TMPDIR}/.fetchProp
92     exit_err "Unable to get ZFS properties file!"
93  fi
94  rm ${TMPDIR}/.fetchProp
95
96  get_value_from_cfg zfsRemoteDataset
97  ZFSDATASET="$VAL"
98
99  lastSNAP=`ssh -p $SSHPORT ${SSHKEY} ${SSHUSER}@${SSHHOST} "zfs list -t snapshot -d 1 -H ${ZFSDATASET}" | tail -1 | awk '{$1=$1}1' OFS=" " | cut -d '@' -f 2 | cut -d ' ' -f 1`
100
101  echo "lastSnap: $lastSNAP"
102  sleep 5
103   
104  # Lets start pulling our ZFS replication
105  zSEND="ssh -p $SSHPORT ${SSHKEY} ${SSHUSER}@${SSHHOST} zfs send -Rv ${ZFSDATASET}@${lastSNAP}"
106  zRECV="zfs receive -evuF ${ZPOOLNAME}"
107  $zSEND | $zRECV >/dev/null 2>/dev/null
108  if [ $? -ne 0 ] ; then
109     exit_err "Failed ZFS send / receive"
110  fi
111
112  # OK, with all the ZFS datasets received, we can now rename
113  echo_log "Moving datasets to correct locations"
114  for i in `zfs list -d 2 $ZPOOLNAME | grep -v "^NAME" | awk '{print $1}'`
115  do
116    tDir=`basename ${i}`
117    if [ "$tDir" = "$i" ]; then continue ; fi
118
119    rc_halt "zfs rename -u ${i} ${ZPOOLNAME}/${tDir}"
120  done
121
122  # Lets create our placeholder snapshots on the zpool
123  echo_log "Cloning snapshots on zpool.."
124  hName=`basename $ZFSDATASET`
125  zfs list -d 1 -H -t snapshot ${ZPOOLNAME}/${hName} >${TMPDIR}/.zsnaps
126  while read line
127  do
128    zSnap="`echo $line | awk '{print $1}' | cut -d '@' -f 2`"
129    rc_halt "zfs snapshot ${ZPOOLNAME}@$zSnap"
130  done < ${TMPDIR}/.zsnaps
131  rm ${TMPDIR}/.zsnaps
132
133  # Delete the old hostname dataset
134  rc_halt "zfs destroy -r ${ZPOOLNAME}/${hName}"
135
136  # Lets export / import the pool
137  rc_halt "cp /boot/zfs/zpool.cache ${TMPDIR}/zpool.cache"
138  rc_halt "zpool export ${ZPOOLNAME}"
139  rc_halt "zpool import -R ${FSMNT} ${ZPOOLNAME}"
140
141  # Lets mount the default dataset
142  rc_halt "mount -t zfs ${ZPOOLNAME}/ROOT/default ${FSMNT}"
143
144  echo_log "Setting ZFS dataset properties.."
145  # Now lets read in our ZFS properties and reset them
146  while read zLine
147  do
148      dSet="`echo $zLine | awk '{print $1}' | cut -d '@' -f 2- | cut -d '/' -f 2-`"
149      prop="`echo $zLine | awk '{print $2}'`"
150      val="`echo $zLine | awk '{print $3}'`"
151      dChk="`echo $zLine | awk '{print $1}'`"
152
153      # Don't need to set empty props
154      if [ -z "$val" ] ; then continue ; fi
155
156      # We can skip setting mountpoint on BEs
157      echo $dSet | grep -q '^ROOT/'
158      if [ $? -eq 0 -a "$prop" = "mountpoint" ] ; then continue; fi
159
160      echo "$dChk" | grep -q '@'
161      if [ $? -eq 0 ] ; then
162        rc_halt "zfs set ${prop}=${val} ${ZPOOLNAME}@${dSet}"
163        continue
164      fi
165      echo "$dChk" | grep -q '/'
166      if [ $? -eq 0 ] ; then
167        rc_halt "zfs set ${prop}=${val} ${ZPOOLNAME}/${dSet}"
168        continue
169      fi
170      if [ "$dSet" = "$dChk" ] ; then
171        if [ "$prop" = "mountpoint" ] ; then continue; fi
172        rc_halt "zfs set ${prop}=${val} ${ZPOOLNAME}"
173        continue
174      fi
175  done < ${ZPFILE}
176
177  # Lastly, lets set bootfs
178  rc_halt "zpool set bootfs=${ZPOOLNAME}/ROOT/default ${ZPOOLNAME}"
179
180}
181
182restore_stamp_grub()
183{
184  rc_halt "cp ${TMPDIR}/zpool.cache /boot/zfs/zpool.cache"
185  setup_grub
186}
187
188restore_umount_zfs()
189{
190   umount_all_dir "${FSMNT}"
191}
Note: See TracBrowser for help on using the repository browser.