-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.sh
311 lines (288 loc) · 8.56 KB
/
lib.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
#./getDateTime.sh
function MsgType {
msg="$1"
error="$2"
print_now=`date +"%Y-%m-%d %T"`
if [ "$error" = 1 ]; then
echo -e "\e[1m${print_now}: \e[31m $msg \e[0m"
echo -e "\e[1m${print_now}: \e[31m Script stopped. \e[0m"
exit
else
echo -e "\e[1m${print_now}: \e[0;92m $msg \e[0m"
fi
}
function LoadConfiguration {
local config="$1"
if [ -f "$config" ]; then
MsgType "Configuration file: $config"
else
MsgType "Configuration file not found!" 1
fi
source "$config"
}
function ValidateBackupSource {
local source="$1"
if [ -z "$source" ]; then
MsgType "No backup source defined!" 1
fi
if [ ! -d "$source" ]; then
MsgType "Backup source not found!" 1
fi
MsgType "Backup source : $source "
}
function ValidateBackupDestination {
local destination="$1"
if [ -z "$destination" ]; then
MsgType "No backup destination defined!" 1
fi
if [ ! -d "$destination" ]; then
MsgType "Backup destination not found. Creating ..."
mkdir -p "$destination"
if [ ! -d "$destination" ]; then
MsgType "Could not create the backup folder" 1
fi
fi
MsgType "Backup destination : $destination "
}
function ValidateBackupParent {
local parent="$1"
local destination="$2"
if [ -z "$parent" ]; then
MsgType "No backup parent defined!" 1
fi
if [ ! -d "$parent" ]; then
parentDirs=($(find "$destination" -type d -name "$parent"))
if [ "${#parentDirs[@]}" -gt 1 ]; then
MsgType "Backup parent is ambivalent!" 1
for dir in "${parentDirs[@]}"; do
MsgType "$dir" 1
done
MsgType "Cannot continue." 1
elif [ "${#parentDirs[@]}" -eq 0 ]; then
MsgType "Backup parent not found!" 1
fi
fi
MsgType "Backup parent : $parent "
}
function SetConfigField(){
local path="$1"
local field="$2"
local value="$3"
sudo sed -i "s/^\($field\s*=\s*\).*\$/\1$value/" $path
}
function FullBackup {
MsgType "Full Backup requested."
local source="$1"
local destination="$2"
local filename="$3"
local config="$4"
local snapshot="level0.snapshot"
local timestamp=`date +%Y%m%d-%H%M%S`
local directory="$destination/$timestamp-full"
local archive="$filename.tar.gz"
MsgType "Performing full backup..."
PerformBackup "$source" "$directory" "$archive" "$snapshot"
MsgType "Saving new parent to config..."
SetConfigField $config BACKUP_PARENT "$timestamp-full"
}
function IncrementalBackup {
MsgType "Incremental Backup requested."
local source="$1"
local destination="$2"
local filename="$3"
local config="$4"
local parent="$5"
local timestamp=`date +%Y%m%d-%H%M%S`
local archive="$filename.tar.gz"
local level=(`find $destination/$parent/ -name "*.snapshot" | wc -l`);
local directory="$destination/$parent/$timestamp-incremental_$level"
local lastlevel=$(($level-1))
local lastfile=(`find $destination/$parent/ -name "level$lastlevel.snapshot"`);
local parentSnapshot="${lastfile[0]}"
if [ -z "$parentSnapshot" ]; then
MsgType "No snapshot found, please do full backup first." 1
fi
MsgType "Backup from : $parentSnapshot"
MsgType "Level increment : $lastlevel -> $level"
local snapshot="level$level.snapshot"
MsgType "Snapshot file : $snapshot"
MsgType "Creating incremental backup ..."
mkdir -p "$directory"
cp -a "$parentSnapshot" "$directory/$snapshot"
PerformBackup "$source" "$directory" "$archive" "$snapshot"
}
function PerformBackup {
source="$1"
destination="$2"
backupFile="$3"
snapshotFile="$4"
mkdir -p "$destination"
backupStart=$SECONDS
local tarOps="-cpvzf"
tar --listed-incremental="$destination/$snapshotFile" $tarOps "$destination/$backupFile" -C "$source" .
res=$?
if [ ! $res -eq 0 ];
then
MsgType "Tar failed! ($res)" 1
else
backupDuration=$(($SECONDS - $backupStart))
backupMin=$(($backupDuration / 60))
backupSec=$(($backupDuration % 60))
MsgType "Backup completed. Time: ${backupMin}min ${backupSec}sec."
fi
return $res
}
function list {
local destination="$1"
readDirectory "$destination" 0 "$2"
}
function readDirectory {
local directory="$1"
local files=($directory/*)
local fullDirs=()
local depth="$2"
local filename="$3"
for file in "${files[@]}"; do
[[ -d "$file" ]] && fullDirs+=("$file")
done
for currentDir in "${fullDirs[@]}"; do
local backupName=`basename "$currentDir"`
readBackup "$currentDir" "$filename"
if [ "${backupInfo[0]}" = false ]; then
echo -n "[$backupName] "
MsgType "${backupInfo[1]}" 1
else
local archiveDate=`date -r "$currentDir" "+%d.%m.%Y-%H:%M:%S"`
local archiveDateNumber=`date -r "$currentDir" "+%s.%N"`
[ "$depth" -gt 0 ] && type="Level $depth"
echo "$archiveDate $type Datetime: [$backupName]"
fi
readDirectory "$currentDir" "$((depth+1))" "$filename"
done
}
function readBackup {
local backupName=`basename $1`
local filename="$2"
local files=($(find "$1" -maxdepth 1 -type f -name "$filename.tar.gz" -o -type f -name "$filename.tar.gz2"))
local archive="${files[0]}"
files=($1/*.snapshot);
local snapshot="${files[0]}"
if [ ! -f "$archive" ]; then
MsgType "No archive file found!" 1
elif [ ! -f "$snapshot" ]; then
MsgType "No snapshot file found!" 1
fi
local level=${snapshot##*level}
level=${level%.*}
local success=true
local error=""
if [ ! -f "$archive" ]; then
success=false
error="No archive file found!"
elif [ ! -f "$snapshot" ]; then
success=false
error="No snapshot file found!"
elif [ -z "$level" ]; then
success=false
error="Could not determine backup level!"
fi
if [ "$success" = true ]; then
backupInfo=(true "$archive" "$snapshot" "$level")
else
backupInfo=(false "$error")
fi
}
function GetCloseSnap {
MsgType "Browsing backup files..."
local destination="$2"
local date_c=$(date -d "$1" +%s)
local actiontype="$3"
local filename="$4"
old_distance="$date_c"
local old_value=""
for d in $destination*; do
for a in $d/*; do
local date_a=$(date -r "$a" +%s)
local distance=$(("$date_a"-"$date_c"))
if (( 0 > $distance )) ;then
distance=$((distance*-1))
fi
if (( $old_distance > $distance )) ;then
old_distance="$distance"
if [[ $a == *"archive.tar.gz"* ]]; then
old_value=$d
else
old_value=$a
fi
fi
done
done
MsgType "Closest date is: $(date -r "$old_value" +%d.%m.%Y-%H:%M:%S)"
MsgType "Backup found: $old_value"
if (( $actiontype == 1)) ; then
MsgType "Restoring folder..."
local lastfile=(`find $old_value/ -name "level*.snapshot"`);
local parentSnapshot="${lastfile[0]}"
echo $parentSnapshot
restore "$old_value" "$filename"
MsgType "Folder restored."
else
MsgType "Providing metadata..."
tar tzf "$old_value/archive.tar.gz"
MsgType "Metadata provided."
fi
}
function restore {
local backuppath="$1"
local filename="$2"
MsgType "Restoration requested."
readBackup "$backuppath" "$filename"
if [ "${backupInfo[0]}" = false ]; then
MsgType "${backupInfo[1]}" 1
fi
local archive="${backupInfo[1]}"
local snapshot="${backupInfo[2]}"
local level="${backupInfo[3]}"
echo "Archive file : `basename "$archive"`"
echo "Snapshot file : `basename "$snapshot"`"
echo "Level : $level"
MsgType "Building incremental backup chain ..."
local backupChain=($archive)
local currentDirectory=`dirname "$archive"`
local currentLevel="$level"
local root_folder="${backuppath%/*}"
while [ "$currentLevel" -gt 0 ]; do
local findsnapshot=(`find $root_folder/ -name "*level$((currentLevel-1)).snapshot"`);
local snapshotfound="${findsnapshot[0]}"
local validpath="${snapshotfound%/*}"
MsgType "Restoring from: $validpath"
readBackup "$validpath" "$filename"
if [ "${backupInfo[0]}" = false ]; then
MsgType "${backupInfo[1]}" 1
fi
backupChain+=(${backupInfo[1]})
((currentLevel--))
done
printf '%s\n' "${backupChain[@]}"
echo "Restoring backup ... "
local chainLastIndex=$((${#backupChain[@]}-1))
for ((chainIndex=$chainLastIndex; chainIndex >= 0; chainIndex--)); do
local backupArchive="${backupChain[$chainIndex]}"
local backupDir=`dirname "$backupArchive"`
local backupName=`basename "$backupDir"`
local tarOps="-x"
tarOps="${tarOps}v"
tarOps="${tarOps}z"
tarOps="${tarOps}f"
MsgType "[$backupName] ... "
tar $tarOps "$backupArchive" -C "$BACKUP_SOURCE"
res=$?
if [[ "$res" -eq 0 ]]; then
MsgType "Success"
else
echo "error $res"
MsgType "Could not restore backup!" 1
fi
done
}