source: src-sh/lpreserver/lpreserver @ a3560cd

releng/10.0.1releng/10.0.2
Last change on this file since a3560cd was a3560cd, checked in by Kris Moore <kris@…>, 5 months ago

Add hourly/30min/10min options to life-preserver replication frequency

  • Property mode set to 100755
File size: 11.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  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 - Manage 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
149         add - Add a new replication target
150        init - Initialize the remote side again
151        list - List replication targets
152      remove - Remove a replication target
153
154Add Options:
155
156        add <remotehost> <user> <port> <hostdataset> <remotedataset> <time>
157
158        <time> = XX/sync/hour/30min/10min
159
160        Example 1:
161
162        add freenas.8343 backupuser 22 tank1/usr/home/kris tankbackup/backups sync
163
164        Will schedule replication of tank1/usr/home/kris to tankbackup/backups with each snapshot
165
166        Example 2:
167
168        add freenas.8343 backupuser 22 tank1 tankbackup/backups 22
169
170        Will schedule replication of tank1 to tankbackup/backups at 10PM, notated in 24hour time
171
172
173Init Options:
174
175        init <localdataset/zpool>
176
177        Will re-init the remote side of the replication. This can be useful
178        when your replication gets stuck. Doing this option will remove
179        all the data on the remote side, and require a full re-sync again.
180
181Remove Options:
182
183        remove <dataset>
184       
185        Remove a replication task indicated by <dataset>
186        Use 'replicate list' for a list of IDs.
187
188Usage:
189
190  lpreserver replicate <subcmd> <flags>
191
192Example:
193
194  lpreserver replicate
195"
196};
197
198
199help_set()
200{
201 title
202 echo "Help set
203
204Set Life-Preserver options
205
206Usage:
207
208  lpreserver set
209
210Config options
211
212     duwarn - Set to a disk percentage [0-99] at which to warn of low disk space
213
214      email - Set the e-mail address to receive notifications
215                This will require that the \"mail\" command is setup for outgoing mail
216
217  emailopts - (ALL/WARN/ERROR)
218                  ALL - Send mail for every snapshot, plus warnings and errors
219                 WARN - Send mail for warnings and errors (Default)
220                ERROR - Send mail for errors only
221
222  recursive - (ON/OFF)
223                   ON - Will create / destroy snapshots recursively, for all children of
224                        a target zpool/dataset. (Default)
225                  OFF - Will only create / destroy snapshots on the specified dataset. 
226
227Example:
228
229  lpreserver set email "kris@example.org,admin@example.org"
230
231  lpreserver set emailopts ALL
232"
233};
234
235help_get()
236{
237 title
238 echo "Help get
239
240Listing of Life-Preserver options set
241
242Usage:
243
244  lpreserver get
245
246Example:
247
248  lpreserver get
249"
250};
251
252help_listcron()
253{
254 title
255 echo "Help listcron
256
257List scheduled cron snapshots
258
259Usage:
260
261  lpreserver listcron
262"
263};
264
265help_status()
266{
267 title
268 echo "Help status
269
270List monitored dataset's last snapshot and replication
271
272Usage:
273
274  lpreserver status
275
276"
277};
278
279
280help_listsnap()
281{
282 title
283 echo "Help listsnap
284
285List ZFS snapshots of a dataset
286
287Usage:
288
289  lpreserver listsnap <dataset>
290
291Example:
292
293  lpreserver listsnap tank0/usr/home/kris
294"
295};
296
297help_cronsnap()
298{
299 title
300 echo "Help cronsnap
301
302Schedule a ZFS snapshot
303
304Usage:
305
306  For a listing of all scheduled snapshots
307
308  # lpreserver listcron
309
310  or
311
312  To start / stop snapshot scheduling
313
314  # lpreserver cronsnap <dataset> <action> <frequency> <numToKeep>
315 
316  action = start / stop
317  frequency = auto / daily@XX / hourly / 30min / 10min / 5min
318                           ^^ Hour to execute
319  numToKeep = Number of snapshots to keep total
320
321  NOTE: When frequency is set to "auto" the following will take place:
322    * Snapshots will be created every 5 minutes and kept for an hour.
323    * A hourly snapshot will be kept for a day.
324    * A daily snapshot will be kept for a month.
325    * A Monthly snapshot will be kept for a year.
326    * The life-preserver daemon will also keep track of the zpool disk space,
327      if the capacity falls below 75%, the oldest snapshot will be auto-pruned.
328
329Example:
330
331  lpreserver cronsnap tank1/usr/home/kris start daily@22 10
332
333  or
334
335  lpreserver cronsnap tank1/usr/home/kris stop
336"
337};
338
339help_revertsnap()
340{
341 title
342 echo "Help revertsnap
343
344Revert ZFS to a snapshot of a dataset
345
346Usage:
347
348  lpreserver revertsnap <dataset> <SNAP>
349
350Example:
351
352  lpreserver revertsnap tank1/usr/home/kris 20130729-140510
353"
354};
355
356help_rmsnap()
357{
358 title
359 echo "Help rmsnap
360
361Remove ZFS snapshot of a dataset
362
363Usage:
364
365  lpreserver rmsnap <dataset> <SNAP>
366
367Example:
368
369  lpreserver rmsnap tank1/usr/home/kris 20130729-140510
370
371NOTE:
372By default snapshots are recursive and will remove snapshots
373of all children datasets automatically. To change this use:
374
375# lpreserver set recursive off
376"
377};
378
379
380help_mksnap()
381{
382 title
383 echo "Help mksnap
384
385Create a new ZFS snapshot of a dataset and optionally replicate it immediately
386
387Usage:
388
389  lpreserver mksnap --replicate <dataset> <snapshotname>
390
391Example:
392
393  lpreserver mksnap tank1/usr/home/kris mysnapshot
394
395  or
396
397  lpreserver mksnap --replicate tank1/usr/home/kris mysnapshot
398
399NOTE:
400By default snapshots are recursive and will create snapshots
401of all children datasets automatically. To change this use:
402
403# lpreserver set recursive off
404"
405};
406
407# Check if we need to enable the zfs monitor
408enable_watcher
409
410# Check what the user wants to do
411case "$1" in
412
413   help) case "$2" in
414           get) help_get ;;
415           set) help_set ;;
416           mksnap) help_mksnap ;;
417           listcron) help_listcron ;;
418           listsnap) help_listsnap ;;
419           revertsnap) help_revertsnap ;;
420           zpool) help_zpool ;;
421           replicate) help_replicate ;;
422           cronsnap) help_cronsnap ;;
423           rmsnap) help_rmsnap ;;
424           status) help_status ;;
425           *) help_main ;;
426         esac  ;;
427
428    get) require_root
429         # Display our options 
430         title
431         echo "      Recursive mode: $RECURMODE"
432         echo "E-mail notifications: $EMAILMODE"
433         echo "    E-mail addresses: $EMAILADDY"
434         echo "  Disk space warn at: ${DUWARN}%"
435         exit 0
436         ;;
437
438    set) require_root
439         OPT="${2}" 
440
441         case $OPT in
442        emailopts) case $3 in
443                  ALL|WARN|ERROR) echo "E-Mail mode set to: $3" 
444                                  echo "$3" > ${DBDIR}/emaillevel ;;
445                     *) exit_err "Unknown option" ;;
446                   esac
447                   ;;
448            email) echo "Setting email notification to: $3" 
449                   echo "$3" > ${DBDIR}/emails ;;
450           duwarn) if [ ! $(is_num "$3") ] ; then exit_err "Invalid number, must be 0-99"; fi
451                   echo "Setting disk warning capacity to: ${3}%" 
452                   echo "$3" > ${DBDIR}/duwarn ;;
453        recursive) case $3 in
454                  OFF|Off|off)  echo "Recursive mode disabled" 
455                                touch ${DBDIR}/recursive-off ;;
456                     ON|On|on)  echo "Recursive mode enabled" 
457                                rm ${DBDIR}/recursive-off 2>/dev/null ;;
458                     *) exit_err "Unknown option" ;;
459                   esac
460                   ;;
461                *) exit_err "Invalid option!" ;;
462           esac
463         ;;
464
465   mksnap) require_root
466         if [ "$2" = "--replicate" ] ; then
467           REP="YES"
468           DATASET="$3"
469           SNAPNAME="$4"
470         else
471           REP="NO"
472           DATASET="$2"
473           SNAPNAME="$3"
474         fi
475         ${PROGDIR}/backend/zfsmksnap.sh "${DATASET}" "$SNAPNAME" "$REP"
476         ;;
477
478 listcron) require_root
479         ${PROGDIR}/backend/zfslistcron.sh
480         ;;
481
482 listsnap) require_root
483         DATASET="$2"
484         ${PROGDIR}/backend/zfslistsnap.sh "${DATASET}"
485         ;;
486
487 cronsnap) require_root
488         DATASET="$2"
489         ${PROGDIR}/backend/zfscronsnap.sh "${DATASET}" "$3" "$4" "$5"
490         ;;
491
492revertsnap) require_root
493         DATASET="$2"
494         SNAP="$3"
495         ${PROGDIR}/backend/zfsrevertsnap.sh "${DATASET}" "$SNAP"
496         ;;
497
498    rmsnap) require_root
499         DATASET="$2"
500         SNAP="$3"
501         ${PROGDIR}/backend/zfsrmsnap.sh "${DATASET}" "${SNAP}"
502         ;;
503    status) listStatus
504         ;;
505 replicate) shift
506            case ${1} in
507                add) require_root ; add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
508                init) require_root ; init_rep_task "$2" ;;
509                list) list_rep_task ;;
510                remove) require_root
511                        cat ${REPCONF} | grep -q "^${2}:" 
512                        if [ $? -eq 0 ] ; then
513                           rem_rep_task "$2"
514                           echo "Removed replication task for $2"
515                        else
516                           exit_err "No such replication task for dataset $2"
517                        fi
518                        ;;
519                *) help_replicate ;;
520            esac
521         ;;
522    zpool) require_root
523            shift
524            case ${1} in
525                attach) add_zpool_disk "$2" "$3" ;;
526                detach) rem_zpool_disk "$2" "$3" ;;
527                online) online_zpool_disk "$2" "$3" ;;
528               offline) offline_zpool_disk "$2" "$3" ;;
529                list) list_zpool_disks "$2" ;;
530                *) help_zpool ;;
531            esac
532         ;;
533   *) help_main ;;
534esac
535
536exit 0
Note: See TracBrowser for help on using the repository browser.