source: src-sh/lpreserver/lpreserver @ 9558561

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

Allow running "lpreserver replicate list" as a user

  • Property mode set to 100755
File size: 10.7 KB
RevLine 
[c684936]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
[3c8d32d]37cat<<__EOF__
38Available commands
[c684936]39
[3c8d32d]40Type in help <command> for information and usage about that command
[c684936]41
[1996f85]42       help - This help file or the help for the specified command
43
44   cronsnap - Schedule snapshot creation via cron
[5cc3d00]45        get - Get list of lpreserver options
[1fd551d]46   listcron - Listing of scheduled snapshots
[1996f85]47   listsnap - List snapshots of a zpool/dataset
48     mksnap - Create a ZFS snapshot of a zpool/dataset
[1fd551d]49  replicate - Enable / Disable ZFS replication to a remote system
[1996f85]50 revertsnap - Revert zpool/dataset to a snapshot
51     rmsnap - Remove a snapshot
[5cc3d00]52        set - Set lpreserver options
[cb8f638]53     status - List datasets, along with last snapshot / replication date
[7c0c657]54      zpool - Manage a zpool by attaching / detaching disks
[c684936]55__EOF__
56};
57
[59f367e]58help_zpool()
[1996f85]59{
60 title
[59f367e]61 echo "Help zpool
[1996f85]62
[59f367e]63Life-Preserver zpool sub-system
[1996f85]64
[59f367e]65The 'zpool' sub-command allows you to attach / detach drives to a zpool,
66as well as get status and more.
[1996f85]67
[59f367e]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.
[1996f85]72
[59f367e]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
[1996f85]79
[59f367e]80Attach Options:
[1996f85]81
[59f367e]82        attach <zpool> <new disk>
[1996f85]83
[59f367e]84        Example 1:
[1996f85]85
[59f367e]86        attach tank1 /dev/da0
[1996f85]87
[59f367e]88        Attach the disk /dev/da0 to tank1 pool. The disk will be
89        auto-partioned with a SWAP / ZFS partition and made bootable.
[1996f85]90
[59f367e]91Detach Options:
[1996f85]92
[59f367e]93        detach <zpool> <disk>
94       
95        Detach a disk from the zpool
[1996f85]96
[59f367e]97        Use 'zpool list' for a list of available disks
[1996f85]98
[59f367e]99Offline Options:
[1996f85]100
[59f367e]101        offline <zpool> <disk> 
[1996f85]102       
[59f367e]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.
[1996f85]105
[59f367e]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.
[1996f85]112
113List Options:
114
[c7aa193]115        list <zpool>
[1996f85]116       
[59f367e]117        Lists the connected disks of a zpool
[1996f85]118
119Usage:
120
[59f367e]121  lpreserver zpool <subcmd> <flags>
[1996f85]122
123Example:
124
[59f367e]125  lpreserver zpool list tank1
[1996f85]126"
127};
128
[1fd551d]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
[fa6febe]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
[a493689]145# zfs allow -u <user> create,receive,mount,userprop,destroy,send,hold <remotedataset>
[fa6febe]146
[1fd551d]147Available Flags:
[c0e6494]148
[1fd551d]149         add - Add a new replication target
[c0e6494]150        init - Initialize the remote side again
151        list - List replication targets
[1fd551d]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
[c0e6494]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
[1fd551d]179Remove Options:
180
[4e5cced]181        remove <dataset>
[1fd551d]182       
[4e5cced]183        Remove a replication task indicated by <dataset>
[1fd551d]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
[d515a9e]197help_set()
[c684936]198{
199 title
200 echo "Help set
201
202Set Life-Preserver options
203
204Usage:
205
206  lpreserver set
207
208Config options
209
[8c2bd1a]210     duwarn - Set to a disk percentage [0-99] at which to warn of low disk space
211
[c684936]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
[583ccad]217                 WARN - Send mail for warnings and errors (Default)
[c684936]218                ERROR - Send mail for errors only
219
[583ccad]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
[c684936]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
[1fd551d]250help_listcron()
251{
252 title
253 echo "Help listcron
254
255List scheduled cron snapshots
256
257Usage:
258
259  lpreserver listcron
260"
261};
[c684936]262
[cb8f638]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
[c684936]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
[4f0d211]306  # lpreserver listcron
[c684936]307
308  or
309
310  To start / stop snapshot scheduling
311
312  # lpreserver cronsnap <dataset> <action> <frequency> <numToKeep>
313 
314  action = start / stop
[f615958]315  frequency = daily@XX / hourly / 30min / 10min / 5min
316                    ^^ Hour to execute
[c684936]317  numToKeep = Number of snapshots to keep total
318
319Example:
320
[f615958]321  lpreserver cronsnap tank1/usr/home/kris start daily@22 10
[c684936]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
[583ccad]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
[c684936]366"
367};
368
369
370help_mksnap()
371{
372 title
373 echo "Help mksnap
374
[aac00e0]375Create a new ZFS snapshot of a dataset and optionally replicate it immediately
[c684936]376
377Usage:
378
[aac00e0]379  lpreserver mksnap --replicate <dataset> <snapshotname>
[c684936]380
381Example:
382
[aac00e0]383  lpreserver mksnap tank1/usr/home/kris mysnapshot
384
385  or
386
387  lpreserver mksnap --replicate tank1/usr/home/kris mysnapshot
[583ccad]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
[c684936]394"
395};
396
[ca25b67]397# Check if we need to enable the zfs monitor
398enable_watcher
399
[c684936]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 ;;
[1fd551d]407           listcron) help_listcron ;;
[c684936]408           listsnap) help_listsnap ;;
409           revertsnap) help_revertsnap ;;
[de39bc3b]410           zpool) help_zpool ;;
[1fd551d]411           replicate) help_replicate ;;
[c684936]412           cronsnap) help_cronsnap ;;
413           rmsnap) help_rmsnap ;;
[cb8f638]414           status) help_status ;;
[c684936]415           *) help_main ;;
416         esac  ;;
417
418    get) require_root
[7814c14]419         # Display our options 
420         title
421         echo "      Recursive mode: $RECURMODE"
[8c2bd1a]422         echo "E-mail notifications: $EMAILMODE"
423         echo "    E-mail addresses: $EMAILADDY"
424         echo "  Disk space warn at: ${DUWARN}%"
[7814c14]425         exit 0
[c684936]426         ;;
427
428    set) require_root
429         OPT="${2}" 
430
431         case $OPT in
[7814c14]432        emailopts) case $3 in
433                  ALL|WARN|ERROR) echo "E-Mail mode set to: $3" 
434                                  echo "$3" > ${DBDIR}/emaillevel ;;
[d515a9e]435                     *) exit_err "Unknown option" ;;
436                   esac
437                   ;;
[7814c14]438            email) echo "Setting email notification to: $3" 
439                   echo "$3" > ${DBDIR}/emails ;;
[8c2bd1a]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 ;;
[7814c14]443        recursive) case $3 in
[d515a9e]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                   ;;
[c684936]451                *) exit_err "Invalid option!" ;;
452           esac
453         ;;
454
455   mksnap) require_root
[aac00e0]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"
[c684936]466         ;;
467
[1fd551d]468 listcron) require_root
469         ${PROGDIR}/backend/zfslistcron.sh
470         ;;
471
[c684936]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         ;;
[cb8f638]493    status) listStatus
494         ;;
[9558561]495 replicate) shift
[4e5cced]496            case ${1} in
[9558561]497                add) require_root ; add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
498                init) require_root ; init_rep_task "$2" ;;
[feb0fb0]499                list) list_rep_task ;;
[9558561]500                remove) require_root
501                        cat ${REPCONF} | grep -q "^${2}:" 
[4e5cced]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                        ;;
[1996f85]509                *) help_replicate ;;
510            esac
511         ;;
[59f367e]512    zpool) require_root
[5d99101]513            shift
514            case ${1} in
[59f367e]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 ;;
[5d99101]521            esac
522         ;;
[c684936]523   *) help_main ;;
524esac
525
526exit 0
Note: See TracBrowser for help on using the repository browser.