2
#########################################################################
4
# netbckup is automatically generated, #
5
# please do not modify! #
7
#########################################################################
9
#########################################################################
11
# Script ID: netbckup #
12
# Author: Mark Grant #
15
# Depending on command line arguments runs:- #
16
# a full ad hoc backup. #
17
# a level 0 --listed-incremental (ie full) backup of entire file #
18
# system. It allows for a cycle of 5 backups for weekly #
19
# coverage, designated by 1 - 5. #
20
# a level 1 --listed-incremental backup to produce a differential #
22
# a level 1 - n --listed-incremental backup to produce an #
23
# incremental backup. #
24
# a selective backup. #
26
# Syntax: netbckup [ -d or --differential || -f or --full || #
27
# -h or --help || -i or --incremental || #
28
# -s or --selective || -V or --version || #
31
# Exit Codes: 0 & 64 - 113 as per C/C++ standard #
33
# 64 - Invalid arguments #
34
# 65 - Failed mounting backup network share #
35
# 66 - backup.snar non-existent or inaccesssible #
36
# 67 - trap received #
38
#########################################################################
40
#########################################################################
44
# Date Author Version Description #
46
# 24/09/2014 MG 1.0.1 Created from consolidation of seperate #
47
# full, weekly, daily and data scripts. #
48
# 28/09/2014 MG 1.0.2 Changed latency_compensate function to #
49
# attempt 1 execution of command without #
51
# 30/09/2014 MG 1.0.3 If the root account is accessible, ('P' #
52
# from passwd -S), then deny login access #
53
# to non-root users for the duration of #
54
# the backup. If root is not accessible, #
55
# then do not deny access as it would be #
56
# dangerous if for some reason it is not #
57
# re-allowed. Only root would have access.#
58
# (Even though shutdown clears the #
59
# restriction, this script does not #
60
# demand a shutdown). #
61
# 22/11/2014 MG 1.0.4 Add overall package version to -V. #
62
# 22/11/2014 MG 1.0.5 Switched from getopts to GNU getopt to #
63
# allow long options. #
64
# 03/02/2015 MG 1.0.6 Remove BSD support. #
65
# 03/02/2015 MG 1.2.1 Add support for NFS backup shares. Add #
66
# support for different backup share #
67
# targets for weekly and ad hoc type #
68
# backups. Remove latency_compensate #
69
# function as improved hardware or #
70
# removal of BSD has cured the problem. #
71
# 24/02/2015 MG 1.2.2 Change configuration files directory to #
72
# lixbackups to limit risk of conflict #
73
# with other backup packages. #
74
# 26/02/2015 MG 1.2.3 Use logger to write key messages to #
76
# 01/03/2015 MG 1.2.4 Use log file in localstatedir/log. Log #
77
# file management set to use logrotate in #
78
# this package release. #
79
# 16/03/2015 MG 1.2.5 Improve logging. #
80
# 05/06/2015 MG 1.2.6 Reduce df output from df -ah to a more #
83
#########################################################################
89
version="1.2.6" # set version variable
90
etclocation=@sysconfdir@/lixbackups # Path to etc directory
91
outputprefix="netbckup: "
97
bckuptype="" # Selected backup type
98
bckupCL="" # Backup command line
99
packageversion=@pkgversion@ # Version of the complete package
103
attachtype="" # attbckshare weekly or adhoc
104
loglocation=@localstatedir@/log # Log file location
110
# Output $1 to stdout or stderr depending on $2 and write to syslog if required.
111
# Syntax = output $1 (The message) $2 (0 = stdout, > 0 = stderr) $3 (TRUE = syslog it).
116
echo $(date +'[ %a %b %d %T ]')" $outputprefix$1"
118
echo $(date +'[ %a %b %d %T ]')" $outputprefix$1" 1>&2
122
logger "$outputprefix$1"
123
std_cmd_err_handler $?
127
# Standard function to test command error ($1 is $?) and exit if non-zero
128
std_cmd_err_handler()
137
# Standard function to cleanup and return exit code
140
# The backup share might or might not be mounted.
141
if [ "$(chkbckshare -bt)" = "TRUE" ]
144
# No error check cos could loop via std_cmd_err_handler
147
# FD's 6 & 7 may or may not be open at this point so test individually for closing.
148
# This reads lsof with ANDed conditions of suppress warnings, FD 6, output only FD
149
# for this current PID and pipe it's output to wc to count the lines. If FD 6 is
150
# open, 2 lines result, PID number and f6.
151
if [ $(lsof -aw -d 6 -F f -p $$ | wc -l) -gt 0 ]
153
# Final log entries and restore stdout & stderr
154
output $script_short_desc" completed with status $script_exit_code" $script_exit_code FALSE
155
output "Processing of "$backpath" is complete. Status: $script_exit_code" $script_exit_code FALSE
157
exec 1>&6 6>&- # Restore stdout and close FD 6
159
if [ $(lsof -aw -d 7 -F f -p $$ | wc -l) -gt 0 ]
161
exec 2>&7 7>&- # Restore stderr and close FD 7
167
output $script_short_desc" completed with status "$script_exit_code $script_exit_code TRUE
169
exit $script_exit_code
172
# Standard trap exit function
176
output "Script terminating due to trap received." 1 TRUE
181
trap trap_exit SIGHUP SIGINT SIGTERM
186
# Process command line arguments with GNU getopt.
187
GETOPTTEMP=`getopt -o dfhisVw \
188
--long differential,full,help,incremental,selective,version,weekly -n "$0" -- "$@"`
195
eval set -- "$GETOPTTEMP"
206
if [ $fulladhoc = TRUE -o $incremental = TRUE -o $selective = TRUE -o $weekly = TRUE ]
208
output "Options d, f, i, s and w are all mutually exclusive." 64 FALSE
212
bckuptype="differential"
214
script_short_desc=$(uname -n)" "$(date +%A)" Daily Differential Backup"
218
if [ $differential = TRUE -o $incremental = TRUE -o $selective = TRUE -o $weekly = TRUE ]
220
output "Options f, i, s, w and d are all mutually exclusive." 64 FALSE
224
bckuptype="fulladhoc"
226
script_short_desc=$(uname -n)" Ad Hoc Full Backup "$(date '+%Y%m%d%H%M')
230
echo "Usage is $0 [options]"
231
echo " -d or --differential - perform a differential backup"
232
echo " -f or --full - perform a full ad hoc backup"
233
echo " -h or --help displays usage information"
234
echo " -i or --incremental - perform an incremental backup"
235
echo " -s or --selective - perform a selective backup"
236
echo " -V or --version displays version information"
238
echo " -w or --weekly - perform a weekly full backup"
243
if [ $fulladhoc = TRUE -o $differential = TRUE -o $selective = TRUE -o $weekly = TRUE ]
245
output "Options i, s, w, d and f are all mutually exclusive." 64 FALSE
249
bckuptype="incremental"
251
script_short_desc=$(uname -n)" "$(date +%A)" Daily Incremental Backup"
255
if [ $fulladhoc = TRUE -o $differential = TRUE -o $incremental = TRUE -o $weekly = TRUE ]
257
output "Options s, w, d, f and i are all mutually exclusive." 64 FALSE
261
bckuptype="selective"
263
script_short_desc=$(uname -n)" "$(date +%A)" Selective Backup"
266
-V|--version) echo "$0 Script version "$version
267
echo "$0 Package version "$packageversion
272
if [ $fulladhoc = TRUE -o $differential = TRUE -o $incremental = TRUE -o $selective = TRUE ]
274
output "Options w, d, f, i and s are all mutually exclusive." 64 FALSE
280
# Calculate backup sequence number.
281
# Remove leading zeros otherwise thinks it is octal
283
day=$(echo $day | sed 's/^0*//')
285
if [ $day -gt 28 ]; then
287
elif [ $day -gt 21 ] ; then
289
elif [ $day -gt 14 ] ; then
291
elif [ $day -gt 7 ] ; then
294
script_short_desc=$(uname -n)" Weekly Full Backup - "$bckseq
300
*) output "Internal error." 1 FALSE
306
# Script does not accept other arguments.
309
output "Invalid argument." 64 FALSE
313
# One option has to be selected.
314
if [ $differential = FALSE -a $fulladhoc = FALSE -a $incremental = FALSE \
315
-a $selective = FALSE -a $weekly = FALSE ]
317
output "Either d, f, i, s or w must be set." 64 FALSE
321
output "Refer to $loglocation/lixbackups.log for further information." 0 TRUE
322
output "$script_short_desc" 0 TRUE
324
# Ensure backup log exists.
325
mkdir -p $loglocation
326
if [ ! -f $loglocation/lixbackups.log ]
328
touch $loglocation/lixbackups.log
329
std_cmd_err_handler $?
330
chown syslog:adm $loglocation/lixbackups.log
331
std_cmd_err_handler $?
332
chmod 0640 $loglocation/lixbackups.log
333
std_cmd_err_handler $?
336
# Make copies of stdout & stderr then re-direct stdout & stderr to backup log
337
# and write initial entries
340
exec 1>> $loglocation/lixbackups.log 2>&1
342
output "$script_short_desc" 0 FALSE
344
# Check to see if root account is accessible, if so, prohibit non-root login
345
pwdstatus=$(passwd -Sa | awk '{ if ($1 == "root")
348
if [ $pwdstatus = "P" ]
350
output "Root password status is "$pwdstatus". Denying non-root login access." 0 TRUE
351
echo "Login refused. Backup in progress." > /etc/nologin
353
output "Root password status is "$pwdstatus". Login access not being denied." 0 TRUE
356
# Read parameters from $etclocation/backups.conf
359
exec 3<$etclocation/backups.conf
360
while read -u3 -ra input
367
weeklynotifyuser=${input[1]}
370
adhocnotifyuser=${input[1]}
376
if [ $attachtype = "weekly" ]
378
notifyuser=$weeklynotifyuser
380
notifyuser=$adhocnotifyuser
383
# Check to see if the network backup share is mounted, if so umount as it could
384
# be mounted using different parameters, then mount
385
if [ "$(chkbckshare -bt)" = "TRUE" ]
388
std_cmd_err_handler $?
390
attbckshare --$attachtype --verbose
391
std_cmd_err_handler $?
393
# Start build of backup command
398
# Build the backup & incremental file names and paths
399
backpath="$mntdir/backup"$(date +%a)".tar.gz"
400
snarpath="$mntdir/backup"$(date +%a)".snar"
402
# If the incremental files exists, delete.
403
# (Just in case full backup has not done this)
404
if [ -f $snarpath ] ; then
406
std_cmd_err_handler $?
409
# Copy level 0 incremental file in order to perform a
410
# level 1, effective differential backup each run.
411
if [ -f $mntdir/backup.snar -a -r $mntdir/backup.snar \
412
-a -w $mntdir/backup.snar ] ; then
413
cp $mntdir/backup.snar $snarpath
414
std_cmd_err_handler $?
417
output "backup.snar does not exist or is not accessible. Exit code "$script_exit_code 1 TRUE
421
bckupCL=$bckupCL"$backpath --listed-incremental=$snarpath "
422
bckupCL=$bckupCL"--exclude-from=$etclocation/bckdaily.exclude "
425
# Build the backup file name and path
426
backpath="$mntdir/backup"$(date '+%Y%m%d%H%M')".tar.gz"
428
bckupCL=$bckupCL"$backpath "
431
# Build the backup & incremental file names and paths
432
backpath="$mntdir/backup"$(date +%a)".tar.gz"
433
snarpath="$mntdir/backup.snar"
435
# Check that the incremental file exists.
436
if [ ! -f $snarpath -o ! -r $snarpath -o ! -w $snarpath ] ; then
438
output $snarpath" does not exist or is not accessible. Exit code "$script_exit_code 1 TRUE
442
bckupCL=$bckupCL"$backpath --listed-incremental=$snarpath "
443
bckupCL=$bckupCL"--exclude-from=$etclocation/bckdaily.exclude "
446
# Build the backup file name and path
447
backpath="$mntdir/backupsel"$(date +%a)".tar.gz"
449
bckupCL=$bckupCL"$backpath --files-from=$etclocation/bckseldaily.files "
450
bckupCL=$bckupCL"--exclude-from=$etclocation/bckseldaily.exclude "
453
# Build the backup & incremental file names and paths
454
backpath="$mntdir/backup"$bckseq".tar.gz"
455
snarpath="$mntdir/backup.snar"
457
# If the level 0 incremental file exists, delete
461
std_cmd_err_handler $?
464
bckupCL=$bckupCL"$backpath --listed-incremental=$snarpath "
465
bckupCL=$bckupCL"--exclude-from=$etclocation/bckweekly.exclude "
469
# If the backup file exists, delete
473
std_cmd_err_handler $?
476
output "Attempting to process backup - "$backpath 0 TRUE
478
# Empty the network trashbox if it exists
479
rm -fr $mntdir/trashbox/*
480
std_cmd_err_handler $?
482
# Produce list of sockets to exclude
483
# ignoring directories appearing in bcksystem.exclude
484
# If the directory being excluded is explicit then use wholename option so
485
# the path has to be an exact match.
486
# If the directory is quoted then use -name which matches any part of the full path.
488
findCL="find / -type d '(' "
490
exec 3<$etclocation/bcksystem.exclude
491
while read -u3 -ra input
493
if [ $count != 0 ] ; then
496
nameoption=$(echo $input | awk '{if (substr($0,1,1)=="'"'"'")
499
print "-wholename ";}')
500
findCL=$findCL$nameoption$input
501
((count = count + 1))
505
findCL=$findCL" ')' -prune -o -type s -print > "$mntdir/sockets.exclude
507
output "Find command used:- $findCL" 0 FALSE
511
# Run the backup excluding system directories
512
bckupCL=$bckupCL"--exclude-from=$etclocation/bcksystem.exclude "
513
bckupCL=$bckupCL"--exclude-from=$mntdir/sockets.exclude "
515
# If selective backup then uses --files-from so doesn't need starting node (root)
516
if [ $selective = TRUE ] ; then
517
bckupCL=$bckupCL"--exclude-caches"
519
bckupCL=$bckupCL"--exclude-caches /"
522
output "Backup command used:- $bckupCL" 0 FALSE
524
std_cmd_err_handler $?
527
df -h | mailx -s "$script_short_desc" $notifyuser
529
# Mail backup file date hierarchy
530
ls -lht $mntdir | mailx -s "$script_short_desc" $notifyuser