source: src-sh/lpreserver/lpreserver @ 9cd0fa0

releng/10.0.1releng/10.0.2
Last change on this file since 9cd0fa0 was 9cd0fa0, checked in by Kris Moore <kris@…>, 5 months ago
  • Major update to life-preserver backend

Add new "automatic" mode for creating snapshots

When enabled, this will create snapshots in the following manner:

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