-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
383 lines (327 loc) · 11 KB
/
backup.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env bash
###############################################################################
# SPDX-License-Identifier: MIT #
# File: backup.sh #
# File Created: Friday, June 7th 2024, 16:19:50 #
# Author: Igor Kha #
# --------------------------------------------------------------------------- #
# Last Modified: 2024-06-07, 16:20:09 #
# Modified By: Igor Kha #
# --------------------------------------------------------------------------- #
# License: MIT License https://opensource.org/licenses/MIT #
###############################################################################
# ! TODO: Change to your backup prefix
# Backup archive prefix and name
BACKUP_PREFIX="${BACKUP_PREFIX:-backup}"
BACKUP_NAME="$BACKUP_PREFIX-$(date +%Y-%m-%d_%H%M%S).tar.gz"
# ! TODO: Change to your directories
# Default directories
BACKUP_DIR="${BACKUP_DIR:-./backup}"
SOURCE_DIR="${SOURCE_DIR:-./source}"
TARGET_DIR="${TARGET_DIR:-./target}"
# LOGGER CONFIGURATION
LOG_TO_FILE="${LOG_TO_FILE:-True}"
LOG_FILE="${LOG_FILE:-backup.log}"
# Global variables
LATEST_BACKUP=""
declare -i UNZIP_ARCHIVE_SIZE
declare -i PARTITION_SPACE_AVAILABLE
###############################################################################
# SERVICE FUNCTIONS AND HELPERS #
###############################################################################
# Error function
error() {
echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR: $1" >&2
help
exit 1
}
# Logger function
logger() {
echo "$(date +"%Y-%m-%d %H:%M:%S")# $1"
if [ "$LOG_TO_FILE" = True ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S")# $1" >> "$LOG_FILE"
fi
}
# Function to check dependencies
check_dependencies() {
local cmds=("$@")
[ ${#cmds[@]} -eq 0 ] && cmds=(tar mkdir cat find awk grep date sort tail head basename file cut du)
for cmd in "${cmds[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
logger "ERROR: $cmd is not installed." >&2
exit 1
fi
done
}
# Function to convert bytes to human-readable format
bytesto() {
local value=$1
local -a powers=(B KB MB GB TB PB EB ZB YB)
local power=0
while (( $(awk -v val="$value" 'BEGIN {print (val>=1024)}') )) && (( power < 8 )); do
value=$(awk -v val="$value" 'BEGIN {printf "%.2f", val / 1024}')
((power++))
done
echo "$value ${powers[$power]}"
}
# Function to get the available space in a partition
get_partition_space() {
check_dependencies df
if [ ! -d "$TARGET_DIR" ]; then
logger "ERROR: Directory $TARGET_DIR does not exist." >&2
exit 1
fi
local size_human
partition_space=$(df -k "$TARGET_DIR" | tail -1 | awk '{print $4}')
# Convert to bytes
partition_space=$((partition_space * 1024))
PARTITION_SPACE_AVAILABLE="$partition_space"
size_human=$(bytesto "$partition_space")
logger "Available space in the partition of $TARGET_DIR: $PARTITION_SPACE_AVAILABLE bytes [$size_human]"
}
compare_partition_space() {
if [ "$UNZIP_ARCHIVE_SIZE" -gt "$PARTITION_SPACE_AVAILABLE" ]; then
logger "ERROR: Not enough space in $TARGET_PARTITION to extract the backup." >&2
exit 1
fi
}
# Help function
help() {
cat << EOF
$0 - Backup script to create and restore backups [version: 1.1.0]
$0 [MODE][OPTIONS]
[MODE]
Usage:
-c --create Create a backup
-r --restore Restore the latest backup
[OPTIONS]
Create options:
-s Source directory to backup
-b Backup directory
-v Verify backup after creation
-l [PATH/NAME] Log file to write the output (default: backup.log)
-k [NUMBER] Number of backups to keep (latest backups)
Restore options:
-b Backup directory
-t Target directory to restore
-l [PATH/NAME] Log file to write the output (default: backup.log)
Examples:
$0 -c # Create backup in the default directory
$0 -c -v # Verify backup after creation
$0 -c -k 3 -v # Keep the latest 3 backups and verify
$0 -c -s /path/to/source # Create backup of a specific directory
$0 -c -b /path/to/backup # Create backup in a specific directory
$0 -c -s /path/to/source -b /path/to/backup
$0 -c -s /path/to/source -b /path/to/backup -v -l /path/to/logfile.log
$0 -r # Restore the latest backup in the default directory
$0 -r -b /path/to/backup # Restore the latest backup in a specific directory
$0 -r -b /path/to/backup -t /path/to/target
$0 -r -b /path/to/backup -t /path/to/target -l logfile.log
EOF
}
###############################################################################
# CREATE BACKUP FUNCTIONS #
###############################################################################
# Function to create a backup of a directory
backup() {
if [ ! -d "$SOURCE_DIR" ]; then
logger "ERROR: Source directory $SOURCE_DIR does not exist." >&2
exit 1
fi
local backup_size
# Create backup directory if it does not exist
mkdir -p "$BACKUP_DIR"
# Create backup
tar --exclude='tmp' --exclude="$BACKUP_NAME" -czf "$BACKUP_DIR"/"$BACKUP_NAME" -C "$SOURCE_DIR" .
# Get the size of the backup
backup_size=$(($(du -sk "$BACKUP_DIR"/"$BACKUP_NAME" | cut -f1) * 1024))
logger "SUCCESS: Backup $BACKUP_NAME created in $BACKUP_DIR"
logger "INFO: Backup size: $backup_size bytes [$(bytesto "$backup_size")]"
}
# Function to verify the backup
verify_backup() {
if [ -z "$BACKUP_DIR" ] || [ -z "$BACKUP_NAME" ]; then
logger "ERROR: Backup directory or name is not set." >&2
exit 1
fi
if [ ! -f "$BACKUP_DIR"/"$BACKUP_NAME" ]; then
logger "ERROR: Backup file $BACKUP_DIR/$BACKUP_NAME does not exist." >&2
exit 1
fi
if ! file "$BACKUP_DIR"/"$BACKUP_NAME" | grep -q tar; then
logger "ERROR: Backup file $BACKUP_DIR/$BACKUP_NAME is not a tar archive." >&2
exit 1
fi
if tar -tzf "$BACKUP_DIR"/"$BACKUP_NAME" &> /dev/null; then
logger "SUCCESS: Backup $BACKUP_NAME is valid"
else
logger "ERROR: Backup $BACKUP_NAME is not valid" >&2
exit 1
fi
}
# Function to remove old backup archives
remove_old_backups() {
# Number of backups to keep
local keep=$1
if [ ! -d "$BACKUP_DIR" ]; then
logger "ERROR: Backup directory $BACKUP_DIR does not exist." >&2
exit 1
fi
# Get a list of all backup files, sorted by date
mapfile -t backups < <(find "$BACKUP_DIR" -name "${BACKUP_PREFIX}*.tar.gz" -exec basename {} \; | sort -r)
if [ ${#backups[@]} -le "$keep" ]; then
logger "INFO: No old backup files to remove." >&2
return 0
fi
# Remove all backup files that are older than the specified number
for (( i=keep; i<${#backups[@]}; i++ )); do
rm "$BACKUP_DIR/${backups[$i]}"
logger "INFO: Removed old backup ${backups[$i]}"
done
}
###############################################################################
# RESTORE BACKUP FUNCTIONS #
###############################################################################
# Function to get the latest backup archive name
get_latest_backup() {
if [ ! -d "$BACKUP_DIR" ]; then
logger "ERROR: Backup directory $BACKUP_DIR does not exist." >&2
exit 1
fi
max_date_file=$(find "$BACKUP_DIR" -name "${BACKUP_PREFIX}*.tar.gz" -exec basename {} \; | sort -r | head -n 1)
if [ -z "$max_date_file" ]; then
logger "ERROR: No backup files found in $BACKUP_DIR" >&2
exit 1
fi
# Assign the value to a global variable
LATEST_BACKUP="$max_date_file"
}
# Function to get the archive and estimate the space needed for extraction
get_unzip_archive_size() {
if [ -z "$BACKUP_DIR" ] || [ -z "$LATEST_BACKUP" ]; then
logger "ERROR: Backup directory or name is not set." >&2
exit 1
fi
if [ ! -f "$BACKUP_DIR"/"$LATEST_BACKUP" ]; then
logger "ERROR: Backup file $BACKUP_DIR/$LATEST_BACKUP does not exist." >&2
exit 1
fi
if ! file "$BACKUP_DIR"/"$LATEST_BACKUP" | grep -q tar; then
logger "ERROR: Backup file $BACKUP_DIR/$LATEST_BACKUP is not a tar archive." >&2
exit 1
fi
logger "Checking archive size..."
# Estimate the size of the extracted files
size_in_bytes=$(tar -tvf "$BACKUP_DIR"/"$LATEST_BACKUP" | awk '{s+=$5} END {print s}')
size_in_megabytes=$(bytesto "$size_in_bytes")
UNZIP_ARCHIVE_SIZE="$size_in_bytes"
logger "Latest backup: $LATEST_BACKUP :: requires $UNZIP_ARCHIVE_SIZE bytes [$size_in_megabytes] of disc space."
}
# Function to restore the latest backup
restore_backup() {
if [ ! -d "$TARGET_DIR" ]; then
logger "ERROR: Directory $TARGET_DIR does not exist." >&2
exit 1
fi
# ! TODO: RESTORE
# Extract the latest backup
tar -xzf "$BACKUP_DIR"/"$LATEST_BACKUP" -C "$TARGET_DIR"
logger "SUCCESS: Restored backup $LATEST_BACKUP in $TARGET_DIR"
}
###############################################################################
# MAIN FUNCTION #
###############################################################################
welcome() {
logger ""
logger "------------------------------------ Backup script ------------------------------------"
}
main() {
# Parse command-line options
case "$1" in
-h|--help)
help
exit 0
;;
-c|--create)
VERIFY=false
KEEP=0
shift
while getopts ":s:b:vl:k:" opt; do
case ${opt} in
s )
SOURCE_DIR=$OPTARG
;;
b )
BACKUP_DIR=$OPTARG
;;
v )
VERIFY=true
;;
l )
LOG_FILE=$OPTARG
;;
k )
KEEP=$OPTARG
;;
\? )
error "Invalid option: -$OPTARG"
;;
: )
error "Option -$OPTARG requires an argument."
;;
esac
done
welcome
# Check dependencies
check_dependencies
logger "MODE: Start creating backup..."
backup
if [ "$VERIFY" = true ]; then
verify_backup
fi
# If the keep option was specified, remove old backups
if [ "$KEEP" -gt 0 ]; then
remove_old_backups "$KEEP"
fi
logger "Done!"
exit 0
;;
-r|--restore)
shift
while getopts ":b:t:l:" opt; do
case ${opt} in
b )
BACKUP_DIR=$OPTARG
;;
t )
TARGET_DIR=$OPTARG
;;
l )
LOG_FILE=$OPTARG
;;
\? )
error "Invalid option: -$OPTARG"
;;
: )
error "Option -$OPTARG requires an argument."
;;
esac
done
welcome
# Check dependencies
check_dependencies
logger "MODE: Start restoring backup..."
get_latest_backup
get_unzip_archive_size
get_partition_space
compare_partition_space
restore_backup
logger "Done!"
exit 0
;;
*)
help
exit 1
esac
}
main "$@"