-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbackup-sync.sh
More file actions
235 lines (186 loc) · 6.59 KB
/
Copy pathbackup-sync.sh
File metadata and controls
235 lines (186 loc) · 6.59 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
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
#!/usr/bin/env bash
set -euo pipefail
RCLONE_REMOTE="${RCLONE_REMOTE:?RCLONE_REMOTE must be set to the backup destination}"
BACKUP_DIR="${BACKUP_DIR:-/data/backups}"
LOG="${LOG:-$BACKUP_DIR/logs/backup.log}"
RCLONE_LOG="${RCLONE_LOG:-$BACKUP_DIR/logs/rclone.log}"
DAILY_RETENTION_DAYS="${DAILY_RETENTION_DAYS:-7}"
WEEKLY_RETENTION_DAYS="${WEEKLY_RETENTION_DAYS:-56}"
LOCAL_DAILY_RETENTION_COUNT="${LOCAL_DAILY_RETENTION_COUNT:-5}"
LOCAL_WEEKLY_RETENTION_COUNT="${LOCAL_WEEKLY_RETENTION_COUNT:-5}"
LOCK_FILE="${LOCK_FILE:-$BACKUP_DIR/.backup-sync.lock}"
ONEDRIVE_HARD_DELETE="${ONEDRIVE_HARD_DELETE:-false}"
RCLONE_OPTIONS=(
--transfers 4
--checkers 8
--log-level NOTICE
--log-file "$RCLONE_LOG"
)
DRY_RUN="${DRY_RUN:-false}"
if [[ "$DRY_RUN" != "true" && "$DRY_RUN" != "false" ]]; then
printf 'DRY_RUN must be true or false\n' >&2
exit 1
fi
if [[ "$ONEDRIVE_HARD_DELETE" != "true" && "$ONEDRIVE_HARD_DELETE" != "false" ]]; then
printf 'ONEDRIVE_HARD_DELETE must be true or false\n' >&2
exit 1
fi
if [[ "$ONEDRIVE_HARD_DELETE" == "true" && "$RCLONE_REMOTE" != onedrive:* ]]; then
printf 'ONEDRIVE_HARD_DELETE=true requires an onedrive remote\n' >&2
exit 1
fi
if ! [[ "$LOCAL_DAILY_RETENTION_COUNT" =~ ^[1-9][0-9]*$ ]] || ! [[ "$LOCAL_WEEKLY_RETENTION_COUNT" =~ ^[1-9][0-9]*$ ]]; then
printf 'Local retention counts must be positive integers\n' >&2
exit 1
fi
log() {
printf '[%s] %s\n' "$(date)" "$*" >> "$LOG"
}
has_local_archive() {
local directory="$1"
local pattern="$2"
find "$directory" -maxdepth 1 -type f -name "$pattern" -print -quit | grep -q .
}
has_recent_local_archive() {
local directory="$1"
local pattern="$2"
local max_age_minutes=$((DAILY_RETENTION_DAYS * 1440))
find "$directory" -maxdepth 1 -type f -name "$pattern" \
-mmin "-${max_age_minutes}" -print -quit | grep -q .
}
prune_local_archives() {
local directory="$1"
local pattern="$2"
local keep_count="$3"
[[ -d "$directory" ]] || return 0
while IFS= read -r archive; do
rm -f -- "$archive"
log "LOCAL CLEANUP: removed $archive"
done < <(
find "$directory" -maxdepth 1 -type f -name "$pattern" -printf '%T@ %p\n' \
| sort -rn \
| tail -n +"$((keep_count + 1))" \
| cut -d' ' -f2-
)
}
sync_and_prune() {
local category="$1"
local pattern="$2"
local daily_source="$BACKUP_DIR/$category/daily"
local weekly_source="$BACKUP_DIR/$category/weekly"
local daily_remote="$RCLONE_REMOTE/$category/daily"
local weekly_remote="$RCLONE_REMOTE/$category/weekly"
# Free local space before copying. The creation job keeps the newest five;
# this also protects the host when an older job failed before its cleanup.
prune_local_archives "$daily_source" "$pattern" "$LOCAL_DAILY_RETENTION_COUNT"
prune_local_archives "$weekly_source" "$pattern" "$LOCAL_WEEKLY_RETENTION_COUNT"
if [[ ! -d "$daily_source" ]] || ! has_local_archive "$daily_source" "$pattern"; then
log "SYNC FAIL: $category daily has no local archive"
return 1
fi
if ! has_recent_local_archive "$daily_source" "$pattern"; then
log "SYNC FAIL: $category daily has no recent archive"
return 1
fi
if [[ "$DRY_RUN" == "true" ]]; then
if ! rclone copy "$daily_source" "$daily_remote" \
--include "$pattern" \
"${RCLONE_OPTIONS[@]}" \
--dry-run \
2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category daily copy"
return 1
fi
else
if ! rclone copy "$daily_source" "$daily_remote" \
--include "$pattern" \
"${RCLONE_OPTIONS[@]}" \
2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category daily copy"
return 1
fi
if ! rclone check "$daily_source" "$daily_remote" \
--one-way \
--include "$pattern" \
"${RCLONE_OPTIONS[@]}" \
2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category daily verification"
return 1
fi
fi
delete_args=(
"$daily_remote"
--min-age "${DAILY_RETENTION_DAYS}d"
--include "$pattern"
"${RCLONE_OPTIONS[@]}"
)
if [[ "$DRY_RUN" == "true" ]]; then
delete_args+=(--dry-run)
fi
if [[ "$ONEDRIVE_HARD_DELETE" == "true" ]]; then
delete_args+=(--onedrive-hard-delete)
fi
if ! rclone delete "${delete_args[@]}" 2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category daily cleanup"
return 1
fi
if [[ -d "$weekly_source" ]] && has_local_archive "$weekly_source" "$pattern"; then
weekly_copy_args=(
"$weekly_source"
"$weekly_remote"
--include "$pattern"
"${RCLONE_OPTIONS[@]}"
)
if [[ "$DRY_RUN" == "true" ]]; then
weekly_copy_args+=(--dry-run)
fi
if ! rclone copy "${weekly_copy_args[@]}" \
2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category weekly copy"
return 1
fi
if [[ "$DRY_RUN" != "true" ]] && ! rclone check "$weekly_source" "$weekly_remote" \
--one-way \
--include "$pattern" \
"${RCLONE_OPTIONS[@]}" \
2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category weekly verification"
return 1
fi
fi
delete_args=(
"$weekly_remote"
--min-age "${WEEKLY_RETENTION_DAYS}d"
--include "$pattern"
"${RCLONE_OPTIONS[@]}"
)
if [[ "$DRY_RUN" == "true" ]]; then
delete_args+=(--dry-run)
fi
if [[ "$ONEDRIVE_HARD_DELETE" == "true" ]]; then
delete_args+=(--onedrive-hard-delete)
fi
if ! rclone delete "${delete_args[@]}" 2>> "$RCLONE_LOG"; then
log "SYNC FAIL: $category weekly cleanup"
return 1
fi
if [[ "$DRY_RUN" == "true" ]]; then
log "SYNC DRY RUN: $category (daily ${DAILY_RETENTION_DAYS}d, weekly ${WEEKLY_RETENTION_DAYS}d, hard delete ${ONEDRIVE_HARD_DELETE})"
else
log "SYNC OK: $category (daily ${DAILY_RETENTION_DAYS}d, weekly ${WEEKLY_RETENTION_DAYS}d, hard delete ${ONEDRIVE_HARD_DELETE})"
fi
}
mkdir -p "$(dirname "$LOG")" "$(dirname "$RCLONE_LOG")"
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log 'SYNC SKIP: another backup sync is already running'
exit 1
fi
if ! command -v rclone > /dev/null 2>&1; then
log 'SYNC FAIL: rclone not installed'
exit 1
fi
sync_and_prune mysql '*.sql.gz'
sync_and_prune postgresql '*.sql.gz'
sync_and_prune files '*.tar.gz'
log 'OneDrive sync and remote retention complete'