source: src-sh/lpreserver/lpreserver @ 8f6c088

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

Remove the mirror functionality for now, will need to be re-examined
as a viable option for local backup, replication may still be a better
method

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