dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / svc / configd / restore_repository.sh
blobfa3b5e2e4fa4faf0e5f89d147efc957d16cff638
1 #!/sbin/sh
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License, Version 1.0 only
7 # (the "License"). You may not use this file except in compliance
8 # with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
21 # CDDL HEADER END
24 # Copyright 2012 Nexenta Sysytems, Inc. All rights reserved.
25 # Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 # Use is subject to license terms.
29 PATH=/sbin:/usr/bin:/usr/sbin
30 LC_ALL=C
31 export PATH LC_ALL
33 . /lib/svc/share/smf_include.sh
34 . /lib/svc/share/fs_include.sh
36 usage()
38 echo "usage: $0 [-r rootdir]" >&2
39 echo "
40 See http://illumos.org/msg/SMF-8000-MY for more information on the use of
41 this script."
42 exit 2;
45 repositorydir=etc/svc
46 repository=repository
48 myroot=/
49 while getopts r: opt; do
50 case "$opt" in
51 r) myroot=$OPTARG
52 if [ ! -d $myroot ]; then
53 echo "$myroot: not a directory" >&2
54 exit 1
56 # validate directory and make sure it ends in '/'.
57 case "$myroot" in
58 //*) echo "$myroot: must begin with a single /" >&2
59 usage;;
60 /) echo "$myroot: alternate root cannot be /" >&2
61 usage;;
63 /*/) ;; # ends with /
64 /*) myroot="$myroot/";; # add final /
66 *) echo "$myroot: must be a full path" >&2
67 usage;;
68 esac;;
69 ?) usage;;
70 esac
71 done
73 if [ $OPTIND -le $# ]; then
74 # getopts(1) didn't slurp up everything.
75 usage
79 # Note that the below test is carefully constructed to fail *open*; if
80 # anything goes wrong, it will drive onward.
82 if [ -x /usr/bin/id -a -x /usr/bin/grep ] &&
83 /usr/bin/id 2>/dev/null | /usr/bin/grep -v '^[^=]*=0(' >/dev/null 2>&1; then
84 echo "$0: may only be invoked by root" >&2
85 exit 2
88 echo >&2 "
89 See http://illumos.org/msg/SMF-8000-MY for more information on the use of
90 this script to restore backup copies of the smf(5) repository.
92 If there are any problems which need human intervention, this script will
93 give instructions and then exit back to your shell."
95 if [ "$myroot" = "/" ]; then
96 system="system"
97 [ "`/sbin/zonename`" != global ] && system="zone"
98 echo >&2 "
99 Note that upon full completion of this script, the $system will be rebooted
100 using reboot(1M), which will interrupt any active services.
104 # check that the filesystem is as expected
105 cd "$myroot" || exit 1
106 cd "$myroot$repositorydir" || exit 1
108 nouser=false
109 rootro=false
111 # check to make sure /usr is mounted
112 if [ ! -x /usr/bin/pgrep ]; then
113 nouser=true
116 if [ ! -w "$myroot" ]; then
117 rootro=true
120 if [ "$nouser" = true -o "$rootro" = true ]; then
121 if [ "$nouser" = true -a "$rootro" = true ]; then
122 echo "The / filesystem is mounted read-only, and the /usr" >&2
123 echo "filesystem has not yet been mounted." >&2
124 elif [ "$nouser" = true ]; then
125 echo "The /usr filesystem has not yet been mounted." >&2
126 else
127 echo "The / filesystem is mounted read-only." >&2
130 echo >&2 "
131 This must be rectified before $0 can continue.
133 To properly mount / and /usr, run:
134 /lib/svc/method/fs-root
135 then
136 /lib/svc/method/fs-usr
138 After those have completed successfully, re-run:
139 $0 $*
141 to continue.
143 exit 1
146 # at this point, we know / is mounted read-write, and /usr is mounted.
147 oldreps="`
148 /bin/ls -1rt $repository-*-[0-9]*[0-9] | \
149 /bin/sed -e '/[^A-Za-z0-9_,.-]/d' -e 's/^'$repository'-//'
152 if [ -z "$oldreps" ]; then
153 cat >&2 <<EOF
154 There are no available backups of $myroot$repositorydir/$repository.db.
155 The only available repository is "-seed-". Note that restoring the seed
156 will lose all customizations, including those made by the system during
157 the installation and/or upgrade process.
160 prompt="Enter -seed- to restore from the seed, or -quit- to exit: \c"
161 default=
162 else
163 cat >&2 <<EOF
164 The following backups of $myroot$repositorydir/$repository.db exist, from
165 oldest to newest:
167 $oldreps
169 The backups are named based on their type and the time what they were taken.
170 Backups beginning with "boot" are made before the first change is made to
171 the repository after system boot. Backups beginning with "manifest_import"
172 are made after svc:/system/manifest-import:default finishes its processing.
173 The time of backup is given in YYYYMMDD_HHMMSS format.
175 Please enter either a specific backup repository from the above list to
176 restore it, or one of the following choices:
178 CHOICE ACTION
179 ---------------- ----------------------------------------------
180 boot restore the most recent post-boot backup
181 manifest_import restore the most recent manifest_import backup
182 -seed- restore the initial starting repository (All
183 customizations will be lost, including those
184 made by the install/upgrade process.)
185 -quit- cancel script and quit
188 prompt="Enter response [boot]: \c"
189 default="boot"
192 cont=false
193 while [ $cont = false ]; do
194 echo "$prompt"
196 read x || exit 1
197 [ -z "$x" ] && x="$default"
199 case "$x" in
200 -seed-)
201 if [ $myroot != / -o "`/sbin/zonename`" = global ]; then
202 file="$myroot"lib/svc/seed/global.db
203 else
204 file="$myroot"lib/svc/seed/nonglobal.db
205 fi;;
206 -quit-)
207 echo "Exiting."
208 exit 0;;
210 file="$x";;
211 */*)
212 file="$myroot$x";;
214 file="$myroot$repositorydir/repository-$x";;
215 *) file= ;;
216 esac
218 if [ -f $file ]; then
219 if [ -r $file ]; then
220 checkresults="`echo PRAGMA integrity_check\; | \
221 /lib/svc/bin/sqlite $file >&1 | grep -v '^ok$'`"
223 if [ -n "$checkresults" ]; then
224 echo "$file: integrity check failed:" >&2
225 echo "$checkresults" >&2
226 echo
227 else
228 cont=true
230 else
231 echo "$file: not readable"
233 elif [ -n "$file" ]; then
234 echo "$file: not found"
236 done
238 errors="$myroot"etc/svc/volatile/db_errors
239 repo="$myroot$repositorydir/$repository.db"
240 new="$repo"_old_"`date +%Y''%m''%d'_'%H''%M''%S`"
242 steps=
243 if [ "$myroot" = / ]; then
244 steps="$steps
245 svc.startd(1M) and svc.configd(1M) will be quiesced, if running."
248 if [ -r $repo ]; then
249 steps="$steps
250 $repo
251 -- renamed --> $new"
253 if [ -r $errors ]; then
254 steps="$steps
255 $errors
256 -- copied --> ${new}_errors"
259 cat >&2 <<EOF
261 After confirmation, the following steps will be taken:
262 $steps
263 $file
264 -- copied --> $repo
267 if [ "$myroot" = / ]; then
268 echo "and the system will be rebooted with reboot(1M)."
271 echo
272 cont=false
273 while [ $cont = false ]; do
274 echo "Proceed [yes/no]? \c"
275 read x || x=n
277 case "$x" in
278 [Yy]|[Yy][Ee][Ss])
279 cont=true;;
280 [Nn]|[Nn][Oo])
281 echo; echo "Exiting..."
282 exit 0;
283 esac;
284 done
286 umask 077 # we want files to be root-readable only.
288 startd_msg=
289 if [ "$myroot" = / ]; then
290 zone="`zonename`"
291 startd="`pgrep -z "$zone" -f svc.startd`"
293 echo
294 echo "Quiescing svc.startd(1M) and svc.configd(1M): \c"
295 if [ -n "$startd" ]; then
296 pstop $startd
297 startd_msg=\
298 "To start svc.start(1M) running, do: /usr/bin/prun $startd"
300 pkill -z "$zone" -f svc.configd
302 sleep 1 # yes, this is hack
304 echo "done."
307 if [ -r "$repo" ]; then
308 echo "$repo"
309 echo " -- renamed --> $new"
310 if mv $repo $new; then
312 else
313 echo "Failed. $startd_msg"
314 exit 1;
318 if [ -r $errors ]; then
319 echo "$errors"
320 echo " -- copied --> ${new}_errors"
321 if cp -p $errors ${new}_errors; then
323 else
324 mv -f $new $repo
325 echo "Failed. $startd_msg"
326 exit 1;
330 echo "$file"
331 echo " -- copied --> $repo"
333 if cp $file $repo.new.$$ && mv $repo.new.$$ $repo; then
335 else
336 rm -f $repo.new.$$ ${new}_errors
337 mv -f $new $repo
338 echo "Failed. $startd_msg"
339 exit 1;
342 echo
343 echo "The backup repository has been successfully restored."
344 echo
346 if [ "$myroot" = / ]; then
347 echo "Rebooting in 5 seconds."
348 sleep 5
349 reboot