Debian Codegraph @

debian/boxbackup-client.postinst

raw
1 #! /bin/bash
2 # postinst script for boxbackup-client
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 # summary of how this script can be called:
9 # * <postinst> `configure' <most-recently-configured-version>
10 # * <old-postinst> `abort-upgrade' <new version>
11 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
12 # <new-version>
13 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 # <failed-install-package> <version> `removing'
15 # <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
18 #
19 # quoting from the policy:
20 # Any necessary prompting should almost always be confined to the
21 # post-installation script, and should be protected with a conditional
22 # so that unnecessary prompting doesn't happen if a package's
23 # installation fails and the `postinst' is called with `abort-upgrade',
24 # `abort-remove' or `abort-deconfigure'.
25
26 #loading debconf module
27 . /usr/share/debconf/confmodule
28
29 CONFDIR=/etc/boxbackup
30 DEBCONFBB=$CONFDIR/bbackupd.debconf
31 BBCONF=$CONFDIR/bbackupd.conf
32 BBKEY=$CONFDIR/bbackupd/boxbackup-client-encrypt-key.raw
33 BBPRIVKEY=$CONFDIR/bbackupd/boxbackup-client-priv-key.pem
34 BBCERTREQ=$CONFDIR/bbackupd/boxbackup-client-cert-req.pem
35 BBCERT=$CONFDIR/bbackupd/boxbackup-client-cert.pem
36 BBCACERT=$CONFDIR/bbackupd/boxbackup-server-ca-cert.pem
37 DEBCONFNOTIFY=$CONFDIR/bbackupd/notifyadmin.debconf
38 NOTIFYSCRIPT=$CONFDIR/bbackupd/notifyadmin
39
40 case "$1" in
41 configure)
42 db_get boxbackup-client/debconf
43 if [ "$RET" = "true" ]; then
44 # Generate configuration files
45 # backupd.conf
46 echo "#To reconfigure boxbackup-client run #dpkg-reconfigure boxbackup-client" >> $DEBCONFBB
47
48 db_get boxbackup-client/backupServer
49 echo "StoreHostname = $RET" >> $DEBCONFBB
50
51 db_get boxbackup-client/accountNumber
52 ACCOUNT=$RET
53 echo "AccountNumber = 0x$ACCOUNT" >> $DEBCONFBB
54 echo "KeysFile = $BBKEY" >> $DEBCONFBB
55 echo "" >> $DEBCONFBB
56 echo "CertificateFile = $BBCERT" >> $DEBCONFBB
57 echo "PrivateKeyFile = $BBPRIVKEY" >> $DEBCONFBB
58 echo "TrustedCAsFile = $BBCACERT" >> $DEBCONFBB
59 echo "" >> $DEBCONFBB
60 echo "DataDirectory = /var/lib/bbackupd" >> $DEBCONFBB
61
62 cat >>$DEBCONFBB <<__EOF
63
64 # This script is run whenever bbackupd encounters a problem which requires
65 # the system administrator to assist:
66 # 1) The store is full, and no more data can be uploaded.
67 # 2) Some files or directories were not readable.
68 # The default script emails the system administrator.
69 NotifyScript = $NOTIFYSCRIPT
70
71 __EOF
72
73 db_get boxbackup-client/backupMode
74 if [ "$RET" = "lazy" ]; then
75 db_get boxbackup-client/UpdateStoreInterval
76 UPDATE=$RET
77 [ -z "$UPDATE" ] && UPDATE="3600"
78
79 db_get boxbackup-client/MinimumFileAge
80 FILEAGE=$RET
81 [ -z "$FILEAGE" ] && FILEAGE="21600"
82
83 db_get boxbackup-client/MaxUploadWait
84 UPWAIT=$RET
85 [ -z "$UPWAIT" ] && UPWAIT="86400"
86
87 AUTO=yes
88 else
89 AUTO=no
90 UPDATE=0
91 FILEAGE=0
92 UPWAIT=0
93 fi
94
95 cat >>$DEBCONFBB <<__EOF
96 # Backup mode specification
97 # With snapshot mode, you will need to run bbackupctl to instruct the daemon to upload files.
98 # Set to no for snapshot mode and yes for lazy mode
99 AutomaticBackup = $AUTO
100
101 # A scan of the local discs will be made once an hour (approximately).
102 # To avoid cycles of load on the server, this time is randomly adjusted by a small
103 # percentage as the daemon runs.
104 # Defaults: 3600 for lazy mode - 0 for snapshot mode
105 UpdateStoreInterval = $UPDATE
106
107 # A file must have been modified at least 6 hours ago before it will be uploaded.
108 # Defaults: 21600 for lazy mode - 0 for snapshot mode
109 MinimumFileAge = $FILEAGE
110
111 # If a file is modified repeated, it won't be uploaded immediately in case it's modified again.
112 # However, it should be uploaded eventually. This is how long we should wait after first noticing
113 # a change. (1 day)
114 # Defaults: 86400 for lazy mode - 0 for snapshot mode
115 MaxUploadWait = $UPWAIT
116
117 # Files above this size (in bytes) are tracked, and if they are renamed they will simply be
118 # renamed on the server, rather than being uploaded again. (64k - 1)
119 FileTrackingSizeThreshold = 65535
120
121 # The daemon does "changes only" uploads for files above this size (in bytes).
122 # Files less than it are uploaded whole without this extra processing.
123 DiffingUploadSizeThreshold = 8192
124
125 # The limit on how much time is spent diffing files. Most files shouldn't take very long,
126 # but if you have really big files you can use this to limit the time spent diffing them.
127 # * Reduce if you are having problems with processor usage.
128 # * Increase if you have large files, and think the upload of changes is too large and want
129 # to spend more time searching for unchanged blocks.
130 MaximumDiffingTime = 20
131
132 # Uncomment this line to see exactly what the daemon is going when it's connected to the server.
133 # ExtendedLogging = yes
134
135 # Use this to temporarily stop bbackupd from syncronising or connecting to the store.
136 # This specifies a program or script script which is run just before each sync, and ideally
137 # the full path to the interpreter. It will be run as the same user bbackupd is running as,
138 # usually root.
139 # The script prints either "now" or a number to STDOUT (and a terminating newline, no quotes).
140 # If the result was "now", then the sync will happen. If it's a number, then the script will
141 # be asked again in that number of seconds.
142 # For example, you could use this on a laptop to only backup when on a specific network.
143
144 # SyncAllowScript = /path/to/intepreter/or/exe script-name parameters etc
145
146 # Where the command socket is created in the filesystem.
147 CommandSocket = /var/run/bbackupd.sock
148
149 Server
150 {
151 PidFile = /var/run/bbackupd.pid
152 }
153
154 #
155 # BackupLocations specifies which locations on disc should be backed up. Each
156 # directory is in the format
157 #
158 # name
159 # {
160 # Path = /path/of/directory
161 # (optional exclude directives)
162 # }
163 #
164 # 'name' is derived from the Path by the config script, but should merely be
165 # unique.
166 #
167 # The exclude directives are of the form
168 #
169 # [Exclude|AlwaysInclude][File|Dir][|sRegex] = regex or full pathname
170 #
171 # (The regex suffix is shown as 'sRegex' to make File or Dir plural)
172 #
173 # For example:
174 #
175 # ExcludeDir = /home/guest-user
176 # ExcludeFilesRegex = \.(mp3|MP3)$
177 # AlwaysIncludeFile = /home/username/veryimportant.mp3
178 #
179 # This excludes the directory /home/guest-user from the backup along with all mp3
180 # files, except one MP3 file in particular.
181 #
182 # In general, Exclude excludes a file or directory, unless the directory is
183 # explicitly mentioned in a AlwaysInclude directive.
184 #
185 # If a directive ends in Regex, then it is a regular expression rather than a
186 # explicit full pathname. See
187 #
188 # man 7 re_format
189 #
190 # for the regex syntax on your platform.
191 #
192
193 BackupLocations
194 {
195 __EOF
196
197 db_get boxbackup-client/backupDirs
198
199 for dir in $RET; do
200 NAME=`echo $dir | sed 's/\//-/g' | sed 's/^-//'`
201
202 # TODO : exclude encrypt key file from the backup
203
204 echo " $NAME" >> $DEBCONFBB
205 echo " {" >> $DEBCONFBB
206 echo " Path = $dir" >> $DEBCONFBB
207 echo " }" >> $DEBCONFBB
208 done
209
210 echo "}" >> $DEBCONFBB
211
212 # Encryption key
213 if [ ! -e $BBKEY ]; then
214 if ! openssl rand -out $BBKEY 1024 >&2; then
215 echo "Can't generate encryption key. Check why." >&2
216 fi
217 fi
218
219 chmod 600 $BBKEY || true
220
221 # SSL stuff
222 if [ ! -z "$ACCOUNT" ]; then
223 if [ ! -e $BBPRIVKEY -a ! -e $BBCERT ]; then
224 db_get boxbackup-client/generateCertificate
225
226 if [ "$RET" = "true" ]; then
227 if ! openssl genrsa -out $BBPRIVKEY 2048 >&2; then
228 echo "Private key generation failed! Check why." >&2
229 else
230 chmod 600 $BBPRIVKEY || true
231 fi
232
233
234 if ! openssl req -new -key $BBPRIVKEY -sha256 -out $BBCERTREQ >&2 <<__EOF
235 .
236 .
237 .
238 .
239 .
240 BACKUP-$ACCOUNT
241 .
242 .
243 .
244 __EOF
245 then
246 echo "Certificate request generation failed ! Check why." >&2
247 fi
248 fi
249 fi
250 fi
251
252 # Generate notify script
253 CLIENTNAME=`hostname --fqdn`
254
255 db_get boxbackup-client/notifyMail
256 MAILTO=$RET
257
258 cat >>$DEBCONFNOTIFY <<__EOF
259 #!/bin/sh
260 #To reconfigure boxbackup-client run #dpkg-reconfigure boxbackup-client
261
262 # This script is run whenever bbackupd changes state or encounters a
263 # problem which requires the system administrator to assist:
264 #
265 # 1) The store is full, and no more data can be uploaded.
266 # 2) Some files or directories were not readable.
267 # 3) A backup run starts or finishes.
268 #
269 # The default script emails the system administrator, except for backups
270 # starting and stopping, where it does nothing.
271
272 SUBJECT="BACKUP PROBLEM on host $CLIENTNAME"
273 SENDTO="$MAILTO"
274
275 if [ "\$1" = "" ]; then
276 echo "Usage: \$0 <store-full|read-error|backup-ok|backup-error|backup-start|backup-finish>" >&2
277 exit 2
278 elif [ "\$1" = store-full ]; then
279 $sendmail \$SENDTO <<EOM
280 Subject: \$SUBJECT (store full)
281 To: \$SENDTO
282
283
284 The store account for $CLIENTNAME is full.
285
286 =============================
287 FILES ARE NOT BEING BACKED UP
288 =============================
289
290 Please adjust the limits on account $account_num on server $server.
291
292 EOM
293 elif [ "\$1" = read-error ]; then
294 $sendmail \$SENDTO <<EOM
295 Subject: \$SUBJECT (read errors)
296 To: \$SENDTO
297
298
299 Errors occured reading some files or directories for backup on $CLIENTNAME.
300
301 ===================================
302 THESE FILES ARE NOT BEING BACKED UP
303 ===================================
304
305 Check the logs on $CLIENTNAME for the files and directories which caused
306 these errors, and take appropriate action.
307
308 Other files are being backed up.
309
310 EOM
311 elif [ "\$1" = backup-start -o "\$1" = backup-finish -o "\$1" = backup-ok ]; then
312 # do nothing by default
313 true
314 else
315 $sendmail \$SENDTO <<EOM
316 Subject: \$SUBJECT (unknown)
317 To: \$SENDTO
318
319
320 The backup daemon on $CLIENTNAME reported an unknown error (\$1).
321
322 ==========================
323 FILES MAY NOT BE BACKED UP
324 ==========================
325
326 Please check the logs on $CLIENTNAME.
327
328 EOM
329 fi
330 __EOF
331
332 if [ -x "`which ucf`" ]; then
333 ucf --three-way --debconf-ok $DEBCONFBB $BBCONF
334 fi
335 rm -f $DEBCONFBB
336 chmod 644 $BBCONF || true
337 chown root:root $BBCONF || true
338
339 if [ -x "`which ucf`" ]; then
340 ucf --three-way --debconf-ok $DEBCONFNOTIFY $NOTIFYSCRIPT
341 fi
342 rm -f $DEBCONFNOTIFY
343 chmod 755 $NOTIFYSCRIPT || true
344 chown root:root $NOTIFYSCRIPT || true
345 fi
346 ;;
347
348 abort-upgrade|abort-remove|abort-deconfigure)
349 ;;
350
351 *)
352 echo "postinst called with unknown argument \`$1'" >&2
353 exit 1
354 ;;
355 esac
356
357 db_stop
358
359 # dh_installdeb will replace this with shell code automatically
360 # generated by other debhelper scripts.
361
362 #DEBHELPER#
363
364 exit 0