-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-incremental.sh
executable file
·293 lines (269 loc) · 9.57 KB
/
backup-incremental.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
#set -x
# full and incremental backup script
# created 07 February 2000
# Based on a script by Daniel O'Callaghan <[email protected]>
# modified by Gerhard Mourani <[email protected]>
# modified by Jose Da Silva <[email protected]> 05/feb/08
# modified by Jose Da Silva <[email protected]> 08/sep/10
# modified by Tarjei Huse <[email protected]> 11/may/11
#
# This script was originally based on above script shown on
# <http://www.faqs.org/docs/securing/chap29sec306.html> and was
# modified to backup several directories plus keep a "lastweek"
# "prevweek" and "priorweek" backups too, and report errors.
#
# If you need to backup directories which contain active files,
# it is recommended that you do a snapshot of those directories
# and then do a backup of the snapshot, this way, you have less
# problems dealing with file-locks and files in transition.
# If you need to run a snapshot first, just insert a call to the
# appropriate script at the beginning/end of the main routine below.
#
# Change the variables below to fit your computer/backup system
# Reduce list of paths to trusted directories (most->least trusted)
PATH=/bin:/usr/bin:/usr/local/bin
# Name and location of commands so you don't need to search paths
TAR=/bin/tar # Name and location of commands
BASENAME=/usr/bin/basename # Use "which" to find commands, but
CAT=/bin/cat # try to check /bin before using
DATE=/bin/date # commands located elsewhere such as
ECHO=/bin/echo # in /usr/local/bin or in /usr/bin
CHMOD=/bin/chmod
CHOWN=/bin/chown
HOSTNAME=/bin/hostname
MKDIR=/bin/mkdir
MV=/bin/mv
WHOAMI=/usr/bin/whoami
COMPUTER=`hostname` # Name of this computer
DIRS=( "/home" "/etc" ) # Directories to backup daily
BKDIR=/backup # Where to store all the backup
TIMEDIR=/backup/last-full # Where to store time of full backup
ERRFILE=/var/log/syslog # Send error messages to this file
BKOWNER=root.adm # Owner and Group for backup files
CHMODCHOWN=1 # This file system can use chmod+chown
FULL_WEEK_DAY="Sun"
if [ $# -eq 1 ]
then
. $1
fi
function printConfig() {
cat <<EOD
Starting backup of $COMPUTER with config:
Day of week to do full backups: $FULL_WEEK_DAY
Backup directory: $BKDIR
Directories to backup:
EOD
for element in $(seq 0 $((${#DIRS[@]} - 1)))
do
echo "${DIRS[$element]}"
done
}
if [ ! -z "$PS1" ]; then
printConfig
fi
DOW=`$DATE +%a` # Day of the week e.g. Mon
DOM=`$DATE +%d` # Date of the Month e.g. 27
MD=`$DATE +%b%d` # Month and Date e.g. Sep27
MDT=`$DATE +"%b %d %T"` # Month Day Time Sep 27 12:00:00
CMD=`$BASENAME "$0"` # Command Line Program
EM="$MDT `$HOSTNAME` $CMD[$$]:" # Error Message info
errortmp=0 # Temporary error accumilator
errors=0 # 0 if no errors found at all
# On the 1st of the month, do permanent full backups.
#
# Every Sunday, prevweek's backup is pushed to priorweek's backup,
# lastweek's backup pushed to prevweek's backup, and then Sunday's
# backup is pushed to lastweek's backup before creating a Sunday
# full backup. This creates 4 weeks worth of rollover backups.
#
# Monday to Saturday, an incremental backup is made based on Sunday
# so that you you have daily backups for new files until next week.
#
# if NEWER = "", then tar backs up all files in the directories
# otherwise it backs up files newer than the NEWER date. NEWER
# gets its date from the file written every Sunday.
ErrorTest () {
# Check exit status of last command for any errors and set flag
if [ "$?" -ne 0 ]; then
errortmp=1;
fi
}
ErrorSet () {
# Set errors if errortmp=1 and send error message to $ERRFILE
if [ $errortmp -eq 1 ]; then
$ECHO "$EM Error $1" >> $ERRFILE;
errors=1
fi
}
UpdateTheDate() {
# Update full backup date so increments happen after this
errortmp=0;
NOW=`$DATE +"%Y-%m-%d %X"`; ErrorTest;
$ECHO "$NOW" > "$TIMEDIR/$COMPUTER-full-date"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$TIMEDIR/$COMPUTER-full-date"; ErrorTest;
$CHOWN $BKOWNER "$TIMEDIR/$COMPUTER-full-date"; ErrorTest;
fi
ErrorSet "with time stamp $TIMEDIR/$COMPUTER-full-date";
}
MakeFullMonthlyBackup() {
# Make a full monthly backup based on given directories
errortmp=0;
$TAR -cpzf "$BKDIR/$COMPUTER-$MD-$1.tar.gz" "$2"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$MD-$1.tar.gz"; ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$MD-$1.tar.gz"; ErrorTest;
fi
ErrorSet "with tar file $BKDIR/$COMPUTER-$MD-$1.tar.gz";
}
MakeFullWeeklyBackup() {
# Move previous week's backups into prior week's backups
errortmp=0;
if [[ -f "$BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz" ]]; then
$MV "$BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz" \
"$BKDIR/$COMPUTER-$DOW-priorweek-$1.tar.gz"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$DOW-priorweek-$1.tar.gz";
ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$DOW-priorweek-$1.tar.gz";
ErrorTest;
fi
ErrorSet "moving $BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz";
fi
# Move last week's backups into previous week's backups
errortmp=0;
if [[ -f "$BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz" ]]; then
$MV -f "$BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz" \
"$BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz";
ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$DOW-prevweek-$1.tar.gz";
ErrorTest;
fi
ErrorSet "moving $BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz";
fi
# Then move this week's full backups into last week's backups
errortmp=0;
if [[ -f "$BKDIR/$COMPUTER-$DOW-$1.tar.gz" ]]; then
$MV "$BKDIR/$COMPUTER-$DOW-$1.tar.gz" \
"$BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz";
ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$DOW-lastweek-$1.tar.gz";
ErrorTest;
fi
ErrorSet "moving weekly file $BKDIR/$COMPUTER-$DOW-$1.tar.gz";
fi
# Then create a new weekly backup for this day-of-week
errortmp=0;
$TAR -cpzf "$BKDIR/$COMPUTER-$DOW-$1.tar.gz" "$2"; ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$DOW-$1.tar.gz"; ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$DOW-$1.tar.gz"; ErrorTest;
fi
ErrorSet "with weekly file $BKDIR/$COMPUTER-$DOW-$1.tar.gz";
}
MakeIncrementalWeeklyBackup() {
# Make an incremental backup based on date in NEWER file
errortmp=0;
$TAR --newer="$1" -cpzf "$BKDIR/$COMPUTER-$DOW-$2.tar.gz" "$3";
ErrorTest;
if [ $CHMODCHOWN -eq 1 ]; then
$CHMOD 640 "$BKDIR/$COMPUTER-$DOW-$2.tar.gz"; ErrorTest;
$CHOWN $BKOWNER "$BKDIR/$COMPUTER-$DOW-$2.tar.gz"; ErrorTest;
fi
ErrorSet "with incremental file $BKDIR/$COMPUTER-$DOW-$2.tar.gz";
}
#----- Main program starts here -----
if [ "`$WHOAMI`" != "root" ]; then
$ECHO "$EM Sorry, you must be root!";
exit 1
fi
# Verify backup directory exists, otherwise create it.
if [[ ! -d "$BKDIR" ]]; then
$MKDIR "$BKDIR"
if [[ ! -d "$BKDIR" ]]; then
$ECHO "$EM Error, cannot make $BKDIR!" >> $ERRFILE;
$ECHO "$EM Error, no backup files made!" >> $ERRFILE;
exit 2
else
if [ $CHMODCHOWN -eq 1 ]; then
errortmp=0;
$CHMOD 740 "$BKDIR"; ErrorTest;
$CHOWN $BKOWNER "$BKDIR"; ErrorTest;
ErrorSet "setting permissions on directory $BKDIR";
fi
fi
fi
# Verify time directory exists, otherwise create it.
if [[ ! -d "$TIMEDIR" ]]; then
$MKDIR "$TIMEDIR"
if [[ ! -d "$TIMEDIR" ]]; then
$ECHO "$EM Error, cannot make $TIMEDIR!" >> $ERRFILE;
$ECHO "$EM Error, no backup files made!" >> $ERRFILE;
exit 3
else
if [ $CHMODCHOWN -eq 1 ]; then
errortmp=0;
$CHMOD 640 "$TIMEDIR"; ErrorTest;
$CHOWN $BKOWNER "$TIMEDIR"; ErrorTest;
ErrorSet "setting permissions on directory $TIMEDIR";
fi
fi
fi
# Verify time file exists, otherwise create it.
if [[ ! -f "$TIMEDIR/$COMPUTER-full-date" ]]; then
UpdateTheDate;
if [[ ! -f "$TIMEDIR/$COMPUTER-full-date" ]]; then
$ECHO "$EM Error, cannot find $TIMEDIR/$COMPUTER-full-date!" \
>> $ERRFILE;
$ECHO "$EM Error, no backup files made !" >> $ERRFILE;
exit 4
else
if [ $CHMODCHOWN -eq 1 ]; then
errortmp=0;
$CHMOD 640 "$TIMEDIR/$COMPUTER-full-date"; ErrorTest;
$CHOWN $BKOWNER "$TIMEDIR/$COMPUTER-full-date"; ErrorTest;
ErrorSet "setting permissions, $TIMEDIR/$COMPUTER-full-date";
fi
fi
fi
# Create Monthly Backups on 1st day of each month
if [ $DOM = "01" ]; then
for element in $(seq 0 $((${#DIRS[@]} - 1)))
do
DIR="${DIRS[$element]}"
NAME=`echo $DIR|sed 's/\//-/g'`
MakeFullMonthlyBackup $NAME $DIR;
done
fi
#if [ $DOW = "Sun" ]; then
if [ $DOW = $FULL_WEEK_DAY ]; then
# Create Full Weekly Backups on Sundays
for element in $(seq 0 $((${#DIRS[@]} - 1)))
do
DIR="${DIRS[$element]}"
NAME=`echo $DIR|sed 's/\//-/g'`
MakeFullWeeklyBackup $NAME $DIR;
done
UpdateTheDate;
else
# Make incremental backups - overwrite last weeks
# Get date of last full backup
NEWER="`$CAT $TIMEDIR/$COMPUTER-full-date`"
for element in $(seq 0 $((${#DIRS[@]} - 1)))
do
DIR="${DIRS[$element]}"
NAME=`echo $DIR|sed 's/\//-/g'`
MakeIncrementalWeeklyBackup "$NEWER" $NAME $DIR;
done
fi
#if [ $errors -eq 1 ]; then
# # Errors were found while doing a backup, warn someuser!
# $ECHO "$EM Error creating backups!" >> \
# /home/someuser/Desktop/warning.txt
# $CHMOD 777 /home/someuser/Desktop/warning.txt
#fi