source: src-sh/lpreserver/lpreserver @ 59f367e

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 59f367e was 59f367e, checked in by Kris Moore <kris@…>, 11 months ago

Add bunch of new functionality to lpreserver

Replace "mirror" with "zpool" sub-cmd, which will allow you to
attach / detach / online / offline / list various zpool drives

  • Important notes *

When attaching a new disk to a zpool, the drive will
be wiped and re-formatted with a 'freebsd-zfs' file-system of the same
size as the first-disk in th zpool array.

If the first disk has a swap partition it will be created also,
but not activated, since advanced users can handle that as the see fit.

Last but not least, GRUB will be stamped on the new disk, making it
bootable should another drive in the array go bad.

  • Property mode set to 100755
File size: 10.4 KB
Line 
1#!/bin/sh
2# Command-line interaction script for life-preserver
3# Author: Kris Moore
4# License: BSD
5# "Life-Preserver" Copyright 2013 PC-BSD Software (iXsystems)
6######################################################################
7
8# Source external functions
9. /usr/local/share/pcbsd/scripts/functions.sh
10
11require_root() 
12{
13  UTEST="`whoami`"
14  if [ "$UTEST" != "root" ] ; then
15     exit_err "This command must be run as root!"
16  fi
17}
18
19# Source our functions
20PROGDIR="/usr/local/share/lpreserver"
21
22# Source our local functions
23. ${PROGDIR}/backend/functions.sh
24
25title()
26{
27  echo "Life-Preserver
28---------------------------------"
29};
30
31
32# Function to display help information
33help_main()
34{
35  title
36
37cat<<__EOF__
38Available commands
39
40Type in help <command> for information and usage about that command
41
42       help - This help file or the help for the specified command
43
44   cronsnap - Schedule snapshot creation via cron
45        get - Get list of lpreserver options
46   listcron - Listing of scheduled snapshots
47   listsnap - List snapshots of a zpool/dataset
48     mksnap - Create a ZFS snapshot of a zpool/dataset
49  replicate - Enable / Disable ZFS replication to a remote system
50 revertsnap - Revert zpool/dataset to a snapshot
51     rmsnap - Remove a snapshot
52        set - Set lpreserver options
53     status - List datasets, along with last snapshot / replication date
54      zpool - Mange a zpool by attaching / detaching disks
55__EOF__
56};
57
58help_zpool()
59{
60 title
61 echo "Help zpool
62
63Life-Preserver zpool sub-system
64
65The 'zpool' sub-command allows you to attach / detach drives to a zpool,
66as well as get status and more.
67
68When a drive is first attached to a zpool, it will be re-formatted with
69a new gpart layout and made bootable with GRUB. For this reason the disk
70must be the same size, or larger, than the first array disk SWAP + ZFS
71partition size.
72
73Available Flags:
74        list - List zpool disks
75      attach - Attach a new zpool disk
76      detach - Remove a zpool disk
77     offline - Mark a disk as offline
78      online - Mark a disk as being online again
79
80Attach Options:
81
82        attach <zpool> <new disk>
83
84        Example 1:
85
86        attach tank1 /dev/da0
87
88        Attach the disk /dev/da0 to tank1 pool. The disk will be
89        auto-partioned with a SWAP / ZFS partition and made bootable.
90
91Detach Options:
92
93        detach <zpool> <disk>
94       
95        Detach a disk from the zpool
96
97        Use 'zpool list' for a list of available disks
98
99Offline Options:
100
101        offline <zpool> <disk> 
102       
103        Mark a disk in the zpool array as 'offline', should be when you
104        want to unplug a device, and anticipate plugging it back in later.
105
106Online Options:
107
108        online <zpool> <disk>   
109       
110        Mark an offline disk in the array as online again. The disk will begin
111        to resilver again automatically.
112
113List Options:
114
115        list <zpool>
116       
117        Lists the connected disks of a zpool
118
119Usage:
120
121  lpreserver zpool <subcmd> <flags>
122
123Example:
124
125  lpreserver zpool list tank1
126"
127};
128
129help_replicate()
130{
131 title
132 echo "Help replicate
133
134Life-Preserver replication sub-system
135
136Replication uses ZFS to send your snapshots to a remote system which also has
137a ZFS pool, such as FreeNAS or another PC-BSD system. The <hostdataset> must
138also be scheduled for snapshots for replication to occur.
139
140On the remote system, you will need to have created the <remotedataset> beforehand.
141
142If you plan to replicate to a non-root user, please set the following permissions
143on the remote dataset:
144
145# zfs allow -u <user> create,receive,mount,userprop,destroy,send,hold <remotedataset>
146
147Available Flags:
148        list - List replication targets
149         add - Add a new replication target
150      remove - Remove a replication target
151
152Add Options:
153
154        add <remotehost> <user> <port> <hostdataset> <remotedataset> <time>
155
156        Example 1:
157
158        add freenas.8343 backupuser 22 tank1/usr/home/kris tankbackup/backups sync
159
160        Will schedule replication of tank1/usr/home/kris to tankbackup/backups with each snapshot
161
162        Example 2:
163
164        add freenas.8343 backupuser 22 tank1 tankbackup/backups 22
165
166        Will schedule replication of tank1 to tankbackup/backups at 10PM, notated in 24hour time
167
168Remove Options:
169
170        remove <dataset>
171       
172        Remove a replication task indicated by <dataset>
173        Use 'replicate list' for a list of IDs.
174
175Usage:
176
177  lpreserver replicate <subcmd> <flags>
178
179Example:
180
181  lpreserver replicate
182"
183};
184
185
186help_set()
187{
188 title
189 echo "Help set
190
191Set Life-Preserver options
192
193Usage:
194
195  lpreserver set
196
197Config options
198
199     duwarn - Set to a disk percentage [0-99] at which to warn of low disk space
200
201      email - Set the e-mail address to receive notifications
202                This will require that the \"mail\" command is setup for outgoing mail
203
204  emailopts - (ALL/WARN/ERROR)
205                  ALL - Send mail for every snapshot, plus warnings and errors
206                 WARN - Send mail for warnings and errors (Default)
207                ERROR - Send mail for errors only
208
209  recursive - (ON/OFF)
210                   ON - Will create / destroy snapshots recursively, for all children of
211                        a target zpool/dataset. (Default)
212                  OFF - Will only create / destroy snapshots on the specified dataset. 
213
214Example:
215
216  lpreserver set email "kris@example.org,admin@example.org"
217
218  lpreserver set emailopts ALL
219"
220};
221
222help_get()
223{
224 title
225 echo "Help get
226
227Listing of Life-Preserver options set
228
229Usage:
230
231  lpreserver get
232
233Example:
234
235  lpreserver get
236"
237};
238
239help_listcron()
240{
241 title
242 echo "Help listcron
243
244List scheduled cron snapshots
245
246Usage:
247
248  lpreserver listcron
249"
250};
251
252help_status()
253{
254 title
255 echo "Help status
256
257List monitored dataset's last snapshot and replication
258
259Usage:
260
261  lpreserver status
262
263"
264};
265
266
267help_listsnap()
268{
269 title
270 echo "Help listsnap
271
272List ZFS snapshots of a dataset
273
274Usage:
275
276  lpreserver listsnap <dataset>
277
278Example:
279
280  lpreserver listsnap tank0/usr/home/kris
281"
282};
283
284help_cronsnap()
285{
286 title
287 echo "Help cronsnap
288
289Schedule a ZFS snapshot
290
291Usage:
292
293  For a listing of all scheduled snapshots
294
295  # lpreserver listcron
296
297  or
298
299  To start / stop snapshot scheduling
300
301  # lpreserver cronsnap <dataset> <action> <frequency> <numToKeep>
302 
303  action = start / stop
304  frequency = daily@XX / hourly / 30min / 10min / 5min
305                    ^^ Hour to execute
306  numToKeep = Number of snapshots to keep total
307
308Example:
309
310  lpreserver cronsnap tank1/usr/home/kris start daily@22 10
311
312  or
313
314  lpreserver cronsnap tank1/usr/home/kris stop
315"
316};
317
318help_revertsnap()
319{
320 title
321 echo "Help revertsnap
322
323Revert ZFS to a snapshot of a dataset
324
325Usage:
326
327  lpreserver revertsnap <dataset> <SNAP>
328
329Example:
330
331  lpreserver revertsnap tank1/usr/home/kris 20130729-140510
332"
333};
334
335help_rmsnap()
336{
337 title
338 echo "Help rmsnap
339
340Remove ZFS snapshot of a dataset
341
342Usage:
343
344  lpreserver rmsnap <dataset> <SNAP>
345
346Example:
347
348  lpreserver rmsnap tank1/usr/home/kris 20130729-140510
349
350NOTE:
351By default snapshots are recursive and will remove snapshots
352of all children datasets automatically. To change this use:
353
354# lpreserver set recursive off
355"
356};
357
358
359help_mksnap()
360{
361 title
362 echo "Help mksnap
363
364Create a new ZFS snapshot of a dataset and optionally replicate it immediately
365
366Usage:
367
368  lpreserver mksnap --replicate <dataset> <snapshotname>
369
370Example:
371
372  lpreserver mksnap tank1/usr/home/kris mysnapshot
373
374  or
375
376  lpreserver mksnap --replicate tank1/usr/home/kris mysnapshot
377
378NOTE:
379By default snapshots are recursive and will create snapshots
380of all children datasets automatically. To change this use:
381
382# lpreserver set recursive off
383"
384};
385
386# Check if we need to enable the zfs monitor
387enable_watcher
388
389# Check what the user wants to do
390case "$1" in
391
392   help) case "$2" in
393           get) help_get ;;
394           set) help_set ;;
395           mksnap) help_mksnap ;;
396           listcron) help_listcron ;;
397           listsnap) help_listsnap ;;
398           revertsnap) help_revertsnap ;;
399           zpool) help_zpol ;;
400           replicate) help_replicate ;;
401           cronsnap) help_cronsnap ;;
402           rmsnap) help_rmsnap ;;
403           status) help_status ;;
404           *) help_main ;;
405         esac  ;;
406
407    get) require_root
408         # Display our options 
409         title
410         echo "      Recursive mode: $RECURMODE"
411         echo "E-mail notifications: $EMAILMODE"
412         echo "    E-mail addresses: $EMAILADDY"
413         echo "  Disk space warn at: ${DUWARN}%"
414         exit 0
415         ;;
416
417    set) require_root
418         OPT="${2}" 
419
420         case $OPT in
421        emailopts) case $3 in
422                  ALL|WARN|ERROR) echo "E-Mail mode set to: $3" 
423                                  echo "$3" > ${DBDIR}/emaillevel ;;
424                     *) exit_err "Unknown option" ;;
425                   esac
426                   ;;
427            email) echo "Setting email notification to: $3" 
428                   echo "$3" > ${DBDIR}/emails ;;
429           duwarn) if [ ! $(is_num "$3") ] ; then exit_err "Invalid number, must be 0-99"; fi
430                   echo "Setting disk warning capacity to: ${3}%" 
431                   echo "$3" > ${DBDIR}/duwarn ;;
432        recursive) case $3 in
433                  OFF|Off|off)  echo "Recursive mode disabled" 
434                                touch ${DBDIR}/recursive-off ;;
435                     ON|On|on)  echo "Recursive mode enabled" 
436                                rm ${DBDIR}/recursive-off 2>/dev/null ;;
437                     *) exit_err "Unknown option" ;;
438                   esac
439                   ;;
440                *) exit_err "Invalid option!" ;;
441           esac
442         ;;
443
444   mksnap) require_root
445         if [ "$2" = "--replicate" ] ; then
446           REP="YES"
447           DATASET="$3"
448           SNAPNAME="$4"
449         else
450           REP="NO"
451           DATASET="$2"
452           SNAPNAME="$3"
453         fi
454         ${PROGDIR}/backend/zfsmksnap.sh "${DATASET}" "$SNAPNAME" "$REP"
455         ;;
456
457 listcron) require_root
458         ${PROGDIR}/backend/zfslistcron.sh
459         ;;
460
461 listsnap) require_root
462         DATASET="$2"
463         ${PROGDIR}/backend/zfslistsnap.sh "${DATASET}"
464         ;;
465
466 cronsnap) require_root
467         DATASET="$2"
468         ${PROGDIR}/backend/zfscronsnap.sh "${DATASET}" "$3" "$4" "$5"
469         ;;
470
471revertsnap) require_root
472         DATASET="$2"
473         SNAP="$3"
474         ${PROGDIR}/backend/zfsrevertsnap.sh "${DATASET}" "$SNAP"
475         ;;
476
477    rmsnap) require_root
478         DATASET="$2"
479         SNAP="$3"
480         ${PROGDIR}/backend/zfsrmsnap.sh "${DATASET}" "${SNAP}"
481         ;;
482    status) listStatus
483         ;;
484 replicate) require_root
485            shift
486            case ${1} in
487                add) add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
488                list) list_rep_task ;;
489                remove) cat ${REPCONF} | grep -q "^${2}:" 
490                        if [ $? -eq 0 ] ; then
491                           rem_rep_task "$2"
492                           echo "Removed replication task for $2"
493                        else
494                           exit_err "No such replication task for dataset $2"
495                        fi
496                        ;;
497                *) help_replicate ;;
498            esac
499         ;;
500    zpool) require_root
501            shift
502            case ${1} in
503                attach) add_zpool_disk "$2" "$3" ;;
504                detach) rem_zpool_disk "$2" "$3" ;;
505                online) online_zpool_disk "$2" "$3" ;;
506               offline) offline_zpool_disk "$2" "$3" ;;
507                list) list_zpool_disks "$2" ;;
508                *) help_zpool ;;
509            esac
510         ;;
511   *) help_main ;;
512esac
513
514exit 0
Note: See TracBrowser for help on using the repository browser.