-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackupDir.sh
48 lines (40 loc) · 1.23 KB
/
backupDir.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
#!/bin/bash
# Global Variables
SOURCE_DIR="$1"
BACKUP_DIR="$2"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
LOG_FILE="/var/log/backup_script.log"
# Ensure source and backup directories are provided
validate_directories() {
if [[ -z "$SOURCE_DIR" || -z "$BACKUP_DIR" ]]; then
printf "Usage: %s <source_directory> <backup_directory>\n" "$0" >&2
return 1
fi
}
# Create the backup directory if it doesn't exist
prepare_backup_dir() {
mkdir -p "$BACKUP_DIR" || {
printf "Failed to create backup directory: %s\n" "$BACKUP_DIR" >&2
return 1
}
}
# Perform the backup using rsync
perform_backup() {
local backup_subdir; backup_subdir="$BACKUP_DIR/backup_$TIMESTAMP"
mkdir -p "$backup_subdir" || {
printf "Failed to create subdirectory for the backup: %s\n" "$backup_subdir" >&2
return 1
}
if ! rsync -a --delete "$SOURCE_DIR/" "$backup_subdir/"; then
printf "Backup failed: %s to %s\n" "$SOURCE_DIR" "$backup_subdir" >&2
return 1
fi
printf "Backup successful: %s to %s\n" "$SOURCE_DIR" "$backup_subdir" >> "$LOG_FILE"
}
# Main function
main() {
validate_directories || return 1
prepare_backup_dir || return 1
perform_backup || return 1
}
main "$@"