~m-grant-prg/lixbackups/trunk

« back to all changes in this revision

Viewing changes to source/progs/bash/attbckshare.in

  • Committer: Mark Grant
  • Date: 2017-05-01 09:22:42 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: m.grant.prg@gmail.com-20170501092242-7wfdrcutul5h2kty
Tags: upstream-1.2.5-12-g20783e6
ImportĀ upstreamĀ versionĀ 1.2.5-12-g20783e6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env bash
2
 
##########################################################################
3
 
##                                                                      ##
4
 
##      attbckshare is automatically generated,                         ##
5
 
##              please do not modify!                                   ##
6
 
##                                                                      ##
7
 
##########################################################################
8
 
 
9
 
##########################################################################
10
 
##                                                                      ##
11
 
## Script ID: attbckshare                                               ##
12
 
## Author: Mark Grant                                                   ##
13
 
##                                                                      ##
14
 
## Purpose:                                                             ##
15
 
## To mount a CIFS or NFS backup share. E.g.                            ##
16
 
##      \\Ambrosia\charybdisbck or hermes:/srv/backups/charybdis        ##
17
 
##      on                                                              ##
18
 
##      /mnt/charybdisbck                                               ##
19
 
##                                                                      ##
20
 
## Syntax:      attbckshare [ -a --adhoc || -h --help || -v --verbose   ##
21
 
##                              || -V --version || -w --weekly ]        ##
22
 
##                                                                      ##
23
 
## Exit Codes:  0 & 64 - 113 as per C/C++ standard                      ##
24
 
##              0 - success                                             ##
25
 
##              64 - Invalid arguments                                  ##
26
 
##              65 - Failed mounting backup share                       ##
27
 
##              66 - Backup share already mounted                       ##
28
 
##              67 - trap received                                      ##
29
 
##                                                                      ##
30
 
## Further Info:                                                        ##
31
 
## This script mounts a CIFS or NFS share as a target for the backup    ##
32
 
## scripts.                                                             ##
33
 
##                                                                      ##
34
 
## In order to make the package portable all the necessary parameters   ##
35
 
## are stored in a $PREFIX/etc/lixbackups/backups.conf file.            ##
36
 
## For further information please consult the backups.conf man page.    ##
37
 
##                                                                      ##
38
 
##########################################################################
39
 
 
40
 
##########################################################################
41
 
##                                                                      ##
42
 
## Changelog                                                            ##
43
 
##                                                                      ##
44
 
## Date         Author  Version Description                             ##
45
 
##                                                                      ##
46
 
## 09/04/2010   MG      1.0.1   Created for Linux.                      ##
47
 
## 26/08/2010   MG      1.0.2   Major revision completed supporting     ##
48
 
##                              FreeBSD as well as Linux. Also put all  ##
49
 
##                              relevant parameters in variables at     ##
50
 
##                              beginning of script to enhance          ##
51
 
##                              portability. (e.g. System, backup user  ##
52
 
##                              etc.).                                  ##
53
 
## 18/11/2010   MG      1.0.3   Changed to emit help and version on     ##
54
 
##                              input of correct flag as argument. Also ##
55
 
##                              stored version in string in Init section##
56
 
## 28/11/2010   MG      1.0.4   Changed script to read parameters from  ##
57
 
##                              etclocation/backups.conf and, when      ##
58
 
##                              necessary (FreeBSD), the ~/.nsmbrc file.##
59
 
## 10/01/2012   MG      1.0.5   Removed the .sh extension from the      ##
60
 
##                              command name.                           ##
61
 
## 05/11/2012   MG      1.0.6   Reverted to the .sh file extension.     ##
62
 
## 26/02/2013   MG      1.0.7   Changed command line option processing  ##
63
 
##                              to use getopts.                         ##
64
 
## 01/04/2013   MG      1.0.8   Moved config files to new backups       ##
65
 
##                              directory under sysconfdir. Added       ##
66
 
##                              exclude files for system, weekly, daily ##
67
 
##                              and data.                               ##
68
 
## 22/08/2013   MG      1.0.9   Debian best practice documentation      ##
69
 
##                              requires that programs placed in the    ##
70
 
##                              PATH should not have extensions as the  ##
71
 
##                              programs may be rewritten in a          ##
72
 
##                              different language. This would then     ##
73
 
##                              require a name change, or, if left      ##
74
 
##                              as-is would cause confusion. So name    ##
75
 
##                              changed to remove .sh extension.        ##
76
 
## 06/01/2014   MG      1.1.1   Changed stdout & stderr message output  ##
77
 
##                              to use a function directing to one or   ##
78
 
##                              other based on a status. Changed        ##
79
 
##                              version option to -V. Added verbose     ##
80
 
##                              option -v.                              ##
81
 
## 16/11/2014   MG      1.1.2   Switched from getopts to GNU getopt to  ##
82
 
##                              allow long options.                     ##
83
 
## 17/11/2014   MG      1.1.3   Change FreeBSD specifics to *BSD and    ##
84
 
##                              change Linux to be the default.         ##
85
 
## 22/11/2014   MG      1.1.4   Add overall package version to -V.      ##
86
 
## 02/02/2015   MG      1.1.5   Remove BSD support.                     ##
87
 
## 02/02/2015   MG      1.2.1   Add support for NFS shares. Add support ##
88
 
##                              for different weekly and ad hoc targets.##
89
 
## 24/02/2015   MG      1.2.2   Change configuration files directory to ##
90
 
##                              lixbackups to limit risk of conflict    ##
91
 
##                              with other backup packages.             ##
92
 
## 26/02/2015   MG      1.2.3   Use logger to write key messages to     ##
93
 
##                              syslog.                                 ##
94
 
##                                                                      ##
95
 
##########################################################################
96
 
 
97
 
####################
98
 
## Init variables ##
99
 
####################
100
 
script_exit_code=0
101
 
outputprefix="attbckshare: "
102
 
version="1.2.3"                         # set version variable
103
 
etclocation=@sysconfdir@/lixbackups     # Path to etc directory
104
 
packageversion=@pkgversion@             # Version of the complete package
105
 
 
106
 
verbosity=""
107
 
adhoc=FALSE
108
 
weekly=FALSE
109
 
weeklytype=""
110
 
weeklytarget=""
111
 
weeklycredfile=""
112
 
weeklynotifyuser=""
113
 
adhoctype=""
114
 
adhoctarget=""
115
 
adhoccredfile=""
116
 
adhocnotifyuser=""
117
 
mntdir=""
118
 
bckuptype=""
119
 
 
120
 
###############
121
 
## Functions ##
122
 
###############
123
 
 
124
 
# Output $1 to stdout or stderr depending on $2 and write to syslog if required.
125
 
# Syntax = output $1 (The message) $2 (0 = stdout, > 0 = stderr) $3 (TRUE = syslog it).
126
 
output()
127
 
{
128
 
        if [ $2 = 0 ]
129
 
        then
130
 
                if [ "$verbosity" = "-v" ]
131
 
                then
132
 
                        echo "$outputprefix$1"
133
 
                fi
134
 
        else
135
 
                echo "$outputprefix$1" 1>&2
136
 
        fi
137
 
        if [ $3 = TRUE ]
138
 
        then
139
 
                logger "$outputprefix$1"
140
 
                std_cmd_err_handler $?
141
 
        fi
142
 
}
143
 
 
144
 
# Standard function to test command error ($1 is $?) and exit if non-zero
145
 
std_cmd_err_handler()
146
 
{
147
 
        if [ $1 != 0 ]
148
 
        then
149
 
                script_exit_code=$1
150
 
                script_exit
151
 
        fi
152
 
}
153
 
 
154
 
# Standard function to tidy up and return exit code
155
 
script_exit()
156
 
{
157
 
        exit $script_exit_code
158
 
}
159
 
 
160
 
# Standard trap exit function
161
 
trap_exit()
162
 
{
163
 
script_exit_code=67
164
 
output "Script terminating due to trap received. Code: "$script_exit_code 1 TRUE
165
 
script_exit
166
 
}
167
 
 
168
 
# Setup trap
169
 
trap trap_exit SIGHUP SIGINT SIGTERM
170
 
 
171
 
##########
172
 
## Main ##
173
 
##########
174
 
# Process command line arguments with GNU getopt.
175
 
GETOPTTEMP=`getopt -o ahvVw --long adhoc,help,verbose,version,weekly -n "$0" -- "$@"`
176
 
std_cmd_err_handler $?
177
 
 
178
 
eval set -- "$GETOPTTEMP"
179
 
std_cmd_err_handler $?
180
 
 
181
 
while true
182
 
do
183
 
        case "$1" in
184
 
        -a|--adhoc)
185
 
                if [ $weekly = TRUE ]
186
 
                then
187
 
                        script_exit_code=64
188
 
                        output "Options a and w are mutually exclusive." 1 FALSE
189
 
                        script_exit
190
 
                fi
191
 
                adhoc=TRUE
192
 
                bckuptype="adhoc"
193
 
                shift
194
 
                ;;
195
 
        -h|--help)
196
 
                echo "Usage is $0 [options]"
197
 
                echo "  -a or --adhoc mount the ad hoc backup target share"
198
 
                echo "  OR"
199
 
                echo "  -h or --help displays usage information"
200
 
                echo "  OR"
201
 
                echo "  -v or --verbose verbose output"
202
 
                echo "  OR"
203
 
                echo "  -V or --version displays version information"
204
 
                echo "  OR"
205
 
                echo "  -w or --weekly mount the weekly target backup share"
206
 
                shift
207
 
                script_exit_code=0
208
 
                script_exit
209
 
                ;;
210
 
        -v|--verbose)
211
 
                verbosity="-v"
212
 
                shift
213
 
                ;;
214
 
        -V|--version)
215
 
                echo "$0 Script version "$version
216
 
                echo "$0 Package version "$packageversion
217
 
                shift
218
 
                script_exit_code=0
219
 
                script_exit
220
 
                ;;
221
 
        -w|--weekly)
222
 
                if [ $adhoc = TRUE ]
223
 
                then
224
 
                        script_exit_code=64
225
 
                        output "Options a and w are mutually exclusive." 1 FALSE
226
 
                        script_exit
227
 
                fi
228
 
                weekly=TRUE
229
 
                bckuptype="weekly"
230
 
                shift
231
 
                ;;
232
 
        --)     shift
233
 
                break
234
 
                ;;
235
 
        *)      script_exit_code=1
236
 
                output "Internal error." 1 FALSE
237
 
                script_exit
238
 
                ;;
239
 
        esac
240
 
done
241
 
 
242
 
# Script does not accept other arguments.
243
 
if [ $# -gt 0 ]
244
 
then
245
 
        script_exit_code=64
246
 
        output "Invalid argument." 1 FALSE
247
 
        script_exit
248
 
fi
249
 
 
250
 
# One option has to be selected.
251
 
if [ $adhoc = FALSE -a $weekly = FALSE ]
252
 
then
253
 
        script_exit_code=64
254
 
        output "Either a or w must be set." 1 FALSE
255
 
        script_exit
256
 
fi
257
 
 
258
 
# Read parameters from $etclocation/backups.conf
259
 
IFS="="
260
 
 
261
 
exec 3<$etclocation/backups.conf
262
 
while read -u3 -ra input
263
 
do
264
 
        case ${input[0]} in
265
 
        weeklytype)
266
 
                weeklytype=${input[1]}
267
 
                ;;
268
 
        weeklytarget)
269
 
                weeklytarget=${input[1]}
270
 
                ;;
271
 
        weeklycredfile)
272
 
                weeklycredfile=${input[1]}
273
 
                ;;
274
 
        weeklynotifyuser)
275
 
                weeklynotifyuser=${input[1]}
276
 
                ;;
277
 
        adhoctype)
278
 
                adhoctype=${input[1]}
279
 
                ;;
280
 
        adhoctarget)
281
 
                adhoctarget=${input[1]}
282
 
                ;;
283
 
        adhoccredfile)
284
 
                adhoccredfile=${input[1]}
285
 
                ;;
286
 
        adhocnotifyuser)
287
 
                adhocnotifyuser=${input[1]}
288
 
                ;;
289
 
        mntdir)
290
 
                mntdir=${input[1]}
291
 
                ;;
292
 
        esac
293
 
done
294
 
exec 3<&-
295
 
 
296
 
case $bckuptype in
297
 
adhoc)
298
 
        type=$adhoctype
299
 
        target=$adhoctarget
300
 
        credfile=$adhoccredfile
301
 
        notifyuser=$adhocnotifyuser
302
 
        ;;
303
 
weekly)
304
 
        type=$weeklytype
305
 
        target=$weeklytarget
306
 
        credfile=$weeklycredfile
307
 
        notifyuser=$weeklynotifyuser
308
 
        ;;
309
 
esac
310
 
 
311
 
mountCL="mount -t "
312
 
case $type in
313
 
NFS)
314
 
        mountCL=$mountCL"nfs $target $mntdir"
315
 
        ;;
316
 
CIFS)
317
 
        mountCL=$mountCL"cifs -o credentials=$credfile $target $mntdir"
318
 
        ;;
319
 
*)      script_exit_code=1
320
 
        output "Invalid share type." 1 TRUE
321
 
        script_exit
322
 
        ;;
323
 
esac
324
 
 
325
 
# Check to see if the network backup share is mounted, if not, mount
326
 
if [ "$(chkbckshare -bt)" = "FALSE" ]
327
 
then
328
 
        eval "$mountCL"
329
 
        status=$?
330
 
        if [ $status != 0 ]
331
 
        then
332
 
                script_exit_code=65
333
 
                output "Failed to mount backup share. Mount error: "$status" Script exit code: "$script_exit_code 1 TRUE
334
 
                script_exit
335
 
        fi
336
 
else
337
 
        script_exit_code=66
338
 
        output "Backup share already mounted. Script exit code: "$script_exit_code 1 TRUE
339
 
        script_exit
340
 
fi
341
 
 
342
 
script_exit_code=0
343
 
output "Share $target mounted on $mntdir" 0 TRUE
344
 
script_exit