source: src-sh/lpreserver/lpreserver @ c0e6494

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

Update to lpreserver backend:

Added new "replicate init <dataset>" option, which will re-init the remote side of the replication server, cleaning up any corrupt datasets and preparing to sync fresh again.

Also added a check during replication, we won't try to kickoff a second replication task if a previous one is still running, to avoid collisions. In addition we will not delete any snapshots until the initial replication is finished, in order to prevent undercutting the active replication

  • Property mode set to 100755
File size: 10.7 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 = daily@XX / hourly / 30min / 10min / 5min
316                    ^^ Hour to execute
317  numToKeep = Number of snapshots to keep total
318
319Example:
320
321  lpreserver cronsnap tank1/usr/home/kris start daily@22 10
322
323  or
324
325  lpreserver cronsnap tank1/usr/home/kris stop
326"
327};
328
329help_revertsnap()
330{
331 title
332 echo "Help revertsnap
333
334Revert ZFS to a snapshot of a dataset
335
336Usage:
337
338  lpreserver revertsnap <dataset> <SNAP>
339
340Example:
341
342  lpreserver revertsnap tank1/usr/home/kris 20130729-140510
343"
344};
345
346help_rmsnap()
347{
348 title
349 echo "Help rmsnap
350
351Remove ZFS snapshot of a dataset
352
353Usage:
354
355  lpreserver rmsnap <dataset> <SNAP>
356
357Example:
358
359  lpreserver rmsnap tank1/usr/home/kris 20130729-140510
360
361NOTE:
362By default snapshots are recursive and will remove snapshots
363of all children datasets automatically. To change this use:
364
365# lpreserver set recursive off
366"
367};
368
369
370help_mksnap()
371{
372 title
373 echo "Help mksnap
374
375Create a new ZFS snapshot of a dataset and optionally replicate it immediately
376
377Usage:
378
379  lpreserver mksnap --replicate <dataset> <snapshotname>
380
381Example:
382
383  lpreserver mksnap tank1/usr/home/kris mysnapshot
384
385  or
386
387  lpreserver mksnap --replicate tank1/usr/home/kris mysnapshot
388
389NOTE:
390By default snapshots are recursive and will create snapshots
391of all children datasets automatically. To change this use:
392
393# lpreserver set recursive off
394"
395};
396
397# Check if we need to enable the zfs monitor
398enable_watcher
399
400# Check what the user wants to do
401case "$1" in
402
403   help) case "$2" in
404           get) help_get ;;
405           set) help_set ;;
406           mksnap) help_mksnap ;;
407           listcron) help_listcron ;;
408           listsnap) help_listsnap ;;
409           revertsnap) help_revertsnap ;;
410           zpool) help_zpol ;;
411           replicate) help_replicate ;;
412           cronsnap) help_cronsnap ;;
413           rmsnap) help_rmsnap ;;
414           status) help_status ;;
415           *) help_main ;;
416         esac  ;;
417
418    get) require_root
419         # Display our options 
420         title
421         echo "      Recursive mode: $RECURMODE"
422         echo "E-mail notifications: $EMAILMODE"
423         echo "    E-mail addresses: $EMAILADDY"
424         echo "  Disk space warn at: ${DUWARN}%"
425         exit 0
426         ;;
427
428    set) require_root
429         OPT="${2}" 
430
431         case $OPT in
432        emailopts) case $3 in
433                  ALL|WARN|ERROR) echo "E-Mail mode set to: $3" 
434                                  echo "$3" > ${DBDIR}/emaillevel ;;
435                     *) exit_err "Unknown option" ;;
436                   esac
437                   ;;
438            email) echo "Setting email notification to: $3" 
439                   echo "$3" > ${DBDIR}/emails ;;
440           duwarn) if [ ! $(is_num "$3") ] ; then exit_err "Invalid number, must be 0-99"; fi
441                   echo "Setting disk warning capacity to: ${3}%" 
442                   echo "$3" > ${DBDIR}/duwarn ;;
443        recursive) case $3 in
444                  OFF|Off|off)  echo "Recursive mode disabled" 
445                                touch ${DBDIR}/recursive-off ;;
446                     ON|On|on)  echo "Recursive mode enabled" 
447                                rm ${DBDIR}/recursive-off 2>/dev/null ;;
448                     *) exit_err "Unknown option" ;;
449                   esac
450                   ;;
451                *) exit_err "Invalid option!" ;;
452           esac
453         ;;
454
455   mksnap) require_root
456         if [ "$2" = "--replicate" ] ; then
457           REP="YES"
458           DATASET="$3"
459           SNAPNAME="$4"
460         else
461           REP="NO"
462           DATASET="$2"
463           SNAPNAME="$3"
464         fi
465         ${PROGDIR}/backend/zfsmksnap.sh "${DATASET}" "$SNAPNAME" "$REP"
466         ;;
467
468 listcron) require_root
469         ${PROGDIR}/backend/zfslistcron.sh
470         ;;
471
472 listsnap) require_root
473         DATASET="$2"
474         ${PROGDIR}/backend/zfslistsnap.sh "${DATASET}"
475         ;;
476
477 cronsnap) require_root
478         DATASET="$2"
479         ${PROGDIR}/backend/zfscronsnap.sh "${DATASET}" "$3" "$4" "$5"
480         ;;
481
482revertsnap) require_root
483         DATASET="$2"
484         SNAP="$3"
485         ${PROGDIR}/backend/zfsrevertsnap.sh "${DATASET}" "$SNAP"
486         ;;
487
488    rmsnap) require_root
489         DATASET="$2"
490         SNAP="$3"
491         ${PROGDIR}/backend/zfsrmsnap.sh "${DATASET}" "${SNAP}"
492         ;;
493    status) listStatus
494         ;;
495 replicate) require_root
496            shift
497            case ${1} in
498                add) add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
499                init) init_rep_task "$2" ;;
500                list) list_rep_task ;;
501                remove) cat ${REPCONF} | grep -q "^${2}:" 
502                        if [ $? -eq 0 ] ; then
503                           rem_rep_task "$2"
504                           echo "Removed replication task for $2"
505                        else
506                           exit_err "No such replication task for dataset $2"
507                        fi
508                        ;;
509                *) help_replicate ;;
510            esac
511         ;;
512    zpool) require_root
513            shift
514            case ${1} in
515                attach) add_zpool_disk "$2" "$3" ;;
516                detach) rem_zpool_disk "$2" "$3" ;;
517                online) online_zpool_disk "$2" "$3" ;;
518               offline) offline_zpool_disk "$2" "$3" ;;
519                list) list_zpool_disks "$2" ;;
520                *) help_zpool ;;
521            esac
522         ;;
523   *) help_main ;;
524esac
525
526exit 0
Note: See TracBrowser for help on using the repository browser.