-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
185 lines (160 loc) · 5.19 KB
/
backup.sh
File metadata and controls
185 lines (160 loc) · 5.19 KB
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
#!/bin/bash
set -euo pipefail
########################################
# Remote Backup Script
# Usage: ./backup.sh [--dry-run] <config_file>
########################################
NOTIFY_SCRIPT="/usr/local/emhttp/webGui/scripts/notify"
DRY_RUN=false
LOG_FILE=""
########################################
# Argument parsing
########################################
config_file=""
for arg in "$@"; do
case "$arg" in
--help)
echo "Usage: $0 [--dry-run] <config_file>"
exit 0
;;
--dry-run)
DRY_RUN=true
;;
-*)
echo "Unknown option: $arg"
echo "Usage: $0 [--dry-run] <config_file>"
exit 1
;;
*)
config_file="$arg"
;;
esac
done
if [[ -z "$config_file" ]]; then
echo "Usage: $0 [--dry-run] <config_file>"
exit 1
fi
########################################
# Logging
########################################
log() {
local ts
ts="$(date '+%Y-%m-%d %H:%M:%S')"
local line="[$ts] $*"
echo "$line"
[[ -n "$LOG_FILE" ]] && echo "$line" >> "$LOG_FILE"
}
########################################
# Notifications
########################################
notify_backup_result() {
local status="$1"
local message icon
if [[ "$status" -ne 0 ]]; then
message="Backup Failed"
icon="alert"
else
message="Backup Completed"
icon="normal"
fi
if [[ -x "$NOTIFY_SCRIPT" ]]; then
"$NOTIFY_SCRIPT" -e "Remote Server Backup" -s "${config[NAME]:-unknown}" -d "$message" -i "$icon"
fi
}
########################################
# Verify and load config
########################################
if [[ ! -s "$config_file" ]]; then
echo "Error: Missing or empty config file ($config_file)"
exit 1
fi
declare -A config
while IFS='=' read -r key value; do
[[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
key="${key#"${key%%[![:space:]]*}"}"
key="${key%"${key##*[![:space:]]}"}"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
[[ -z "$key" ]] && continue
config["$key"]="$value"
done < "$config_file"
# Validate required keys
required_keys=(NAME NETWORK DEST REMOTE_IP PORT SSHKEY SOURCE)
for k in "${required_keys[@]}"; do
if [[ -z "${config[$k]:-}" ]]; then
echo "Error: Missing required config key: $k"
exit 1
fi
done
########################################
# Setup Backup Paths & Logging
########################################
backup_base="${config[DEST]}/${config[NETWORK]}/${config[NAME]}"
daily_dir="$backup_base/Daily"
weekly_dir="$backup_base/Weekly"
monthly_dir="$backup_base/Monthly"
mkdir -p "$backup_base"
LOG_FILE="$backup_base/backup.log"
[[ "$DRY_RUN" == true ]] && log "DRY RUN ENABLED - No actual data will be transferred or modified."
########################################
# Daily Backup
########################################
rsync_base_opts=(-aHAXx --delete)
extra_opts=()
if [[ -n "${config[OPTIONS]:-}" ]]; then
IFS=' ' read -ra extra_opts <<< "${config[OPTIONS]}"
fi
if [[ "$DRY_RUN" == true ]]; then
log "DRY RUN - Would make \"$daily_dir\""
log "DRY RUN - Would sync from remote to \"$daily_dir/\""
rsync_daily_opts=("${rsync_base_opts[@]}" "${extra_opts[@]}" --dry-run)
else
mkdir -p "$daily_dir"
rsync_daily_opts=("${rsync_base_opts[@]}" "${extra_opts[@]}")
fi
rsync "${rsync_daily_opts[@]}" \
-e "/usr/bin/ssh -o Compression=no -x -p '${config[PORT]}' -i '${config[SSHKEY]}'" \
"root@${config[REMOTE_IP]}:${config[SOURCE]}" "$daily_dir/" 2>&1 | tee -a "$LOG_FILE" || true
status=${PIPESTATUS[0]}
notify_backup_result "$status"
[[ "$status" -ne 0 ]] && exit 1
########################################
# Weekly Sunday Backup
########################################
if [[ "$(date +%a)" == "Sun" ]]; then
if [[ "$DRY_RUN" == true ]]; then
log "DRY RUN - Would make \"$weekly_dir\""
log "DRY RUN - Would sync: \"$daily_dir/\" -> \"$weekly_dir/\""
rsync_weekly_opts=("${rsync_base_opts[@]}" --dry-run)
else
mkdir -p "$weekly_dir"
rsync_weekly_opts=("${rsync_base_opts[@]}")
fi
rsync "${rsync_weekly_opts[@]}" "$daily_dir/" "$weekly_dir/" 2>&1 | tee -a "$LOG_FILE" || true
status=${PIPESTATUS[0]}
notify_backup_result "$status"
[[ "$status" -ne 0 ]] && exit 1
fi
########################################
# Monthly Backup
########################################
if [[ "$(date +%d)" == "01" ]]; then
tar_file="$monthly_dir/${config[NAME]}_$(date +%B-%Y).tar.gz"
if [[ "$DRY_RUN" == true ]]; then
log "DRY RUN - Would make \"$monthly_dir\""
log "DRY RUN - Would create archive: \"$tar_file\""
else
mkdir -p "$monthly_dir"
log "Creating monthly archive at \"$tar_file\""
tar -czf "$tar_file" -C "$(dirname "$daily_dir")" "$(basename "$daily_dir")" 2>&1 | tee -a "$LOG_FILE" || true
status=${PIPESTATUS[0]}
if [[ "$status" -ne 0 ]]; then
log "[!] tar failed"
notify_backup_result 1
exit 1
fi
log "Generating checksum"
sha256sum "$tar_file" > "$tar_file.sha256"
notify_backup_result 0
fi
fi