source: src-sh/lpreserver/lpreserver @ 5d99101

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 5d99101 was 5d99101, checked in by Kris Moore <kris@…>, 8 months ago

Revert "Remove the mirror functionality for now, will need to be re-examined"

This reverts commit 8f6c08823fc3203016251461ceffae7c402a975b.

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