-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path,backup-homelab
executable file
Β·356 lines (336 loc) Β· 13.2 KB
/
,backup-homelab
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
#!/usr/bin/env bash
# Backup process:
# 1. automate the individual backup process of all services
# 2. rsync to a central dir
# 3. encrypt that
# 4. backup to hardware (usb, etc.)
# 5. backup to offsite (backblaze b2 bucket)
set -e
# services
services="$HOME/services"
NTFY_ENDPOINT="https://ntfy.local.ejsadiarin.com/backups"
# traefik
traefik="$HOME/services/traefik"
# authentik
authentik="$HOME/services/authentik"
# immich
immich="$HOME/services/immich"
# portainer
portainer="$HOME/services/portainer"
# gitea
gitea="$HOME/services/gitea"
# ntfy
ntfy="$HOME/services/ntfy"
# uptime kuma
uptime_kuma="$HOME/services/uptime_kuma"
# glance
glance="$HOME/services/glance"
# beszel
beszel="$HOME/services/beszel"
# essentials
restic="$HOME/services/restic"
vault="$HOME/vault"
# TODO: make one for vaultwarden
dirs=(
"$traefik"
"$authentik"
"$portainer"
"$gitea"
"$ntfy"
"$glance"
"$beszel"
"$uptime_kuma"
"$immich"
"$restic"
"$vault"
)
mkdir -p "$HOME/backups"
backups="$HOME/backups"
assert_rsync() {
local dir=$1
shift
"$@" || {
err=$("$@" 2>&1)
echo "Error on rsync [$dir]: $err"
return 1
}
}
dry_run() {
for dir in "${dirs[@]}"; do
echo "Backing up $dir"
bn=$(basename "$dir")
echo "Current bn: $bn"
bk_dir="$backups/$bn" # example: ~/backups/traefik
echo "Creating if not exists: $bk_dir"
case "$dir" in
"$traefik" | "$uptime_kuma" | "$glance" | "$beszel")
echo "current dir: $dir/ with bk_dir: $bk_dir/"
sudo rsync -azvh --delete --progress --dry-run "$dir"/ "$bk_dir"/
;;
"$portainer")
echo "current dir: $dir/ with bk_dir: $bk_dir/"
sudo rsync -azvh --delete --progress --dry-run "$dir"/ "$bk_dir"/
;;
"$authentik")
echo "current dir: $dir/ with bk_dir: $bk_dir/"
sudo rsync -azvh --delete --progress --dry-run "$dir"/ "$bk_dir"/
echo "will run: docker exec -i authentik-postgres /usr/local/bin/pg_dump --username authentik-pg-admin authentik-pg-db >""$bk_dir""/postgres-backup.sql"
;;
"$ntfy")
ntfy_etc="$dir/etc"
ntfy_cache="$dir/cache"
echo "backing up: files (maxdepth 1) on bk_dir: $bk_dir"
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress --dry-run {} "$bk_dir"
echo "backing up: $ntfy_etc with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/etc "$bk_dir"
echo "backing up: $ntfy_cache with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/cache "$bk_dir"
;;
"$gitea")
echo "backing up: files (maxdepth 1) on bk_dir: $bk_dir"
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress --dry-run {} "$bk_dir"
echo "backing up: dumps to bk_dir: $bk_dir/dumps/"
num_dumps=$(ls "$bk_dir"/dumps/ -A | wc -l)
if [[ "$num_dumps" -gt 3 ]]; then
oldest_zip=$(ls -Art "$bk_dir"/dumps/ | head -n 1)
echo "Will remove oldest dump file: $oldest_zip"
fi
;;
"$immich")
immich_library="$dir/library/library"
immich_profile="$dir/library/profile"
immich_backups="$dir/library/backups"
immich_upload="$dir/library/upload"
echo "backing up: files (maxdepth 1) on bk_dir: $bk_dir"
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress --dry-run {} "$bk_dir"
echo "backing up: $immich_library with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/library/library "$bk_dir"
echo "backing up: $immich_profile with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/library/profile "$bk_dir"
echo "backing up: $immich_backups with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/library/backups "$bk_dir"
echo "backing up: $immich_upload with bk_dir: $bk_dir"
sudo rsync -azvh --delete --progress --dry-run "$dir"/library/upload "$bk_dir"
;;
*)
echo "current dir: $dir/ with bk_dir: $bk_dir/"
;;
esac
done
}
local_backup() {
# onsite backups (hardware)
# TODO: refactor to use basename and not dir, so all inside services are backed up automatically just by adding new service
for dir in "${dirs[@]}"; do
bn=$(basename "$dir")
bk_dir="$backups/$bn" # example: ~/backups/traefik
mkdir -p "$bk_dir"
case "$dir" in
"$traefik" | "$uptime_kuma" | "$glance")
# traefik - backup everything
# src: ~/services/traefik/
# dest: ~/backups/traefik/
sudo rsync -azvh --delete --progress "$dir"/ "$bk_dir"/ || {
echo "[x] Error on rsync [$dir]"
continue
}
;;
"$portainer")
# portainer - backup everything
# dest: ~/backups/portainer
{
sudo rsync -azvh --delete --progress "$dir"/ "$bk_dir"/
sudo chown -R "$(whoami)":"$(whoami)" "$bk_dir"/data
} || {
echo "Error on rsync [$dir]"
continue
}
;;
"$authentik")
# dest: ~/backups/authentik/postgres-backup.sql
sudo rsync -azvh --delete --progress "$dir"/ "$bk_dir"/ || {
echo "[x] Error on rsync [$dir]"
continue
}
{
timestamp=$(date +%s%3N)
docker exec -i authentik-postgres /usr/local/bin/pg_dump --username authentik-pg-admin authentik-pg-db >"$bk_dir"/postgres-backup-"$timestamp".sql
} || {
echo "Error on docker pg_dump [$dir]: $err"
continue
}
;;
"$ntfy")
# ntfy - backup everything
echo "Backing up NTFY..."
{
# docker-compose, env files, etc.
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress {} "$bk_dir"
# dest (bk_dir): ~/backups/ntfy
sudo rsync -azvh --delete --progress "$dir"/etc "$bk_dir"
sudo rsync -azvh --delete --progress "$dir"/cache "$bk_dir"
} || {
echo "Error on rsync [$dir]"
continue
}
;;
"$gitea")
# gitea - backup everything via gitea dump inside container (outputs tar.gz file)
echo "Running gitea dump command..."
mkdir -p "$bk_dir/dumps"
{
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress {} "$bk_dir" # docker-compose, env files, etc.
docker exec -u git -it -w /tmp "$(docker ps -qf 'name=^gitea-server$')" bash -c '/usr/local/bin/gitea dump -c /data/gitea/conf/app.ini'
latest_zip=$(docker exec -u git "$(docker ps -qf 'name=^gitea-server$')" ls /tmp -Art | tail -n 1 | tr -d '\r')
echo "Latest dump file: $latest_zip"
docker cp "$(docker ps -qf 'name=^gitea-server$')":/tmp/"$latest_zip" "$bk_dir/dumps/" || {
echo "Error: Failed to copy $latest_zip from container."
continue
}
# after copy, delete the oldest one (retains 3 latest copies)
num_dumps=$(ls "$bk_dir"/dumps/ -A | wc -l)
if [[ "$num_dumps" -gt 3 ]]; then
oldest_zip=$(ls -Art "$bk_dir"/dumps/ | head -n 1)
echo "Removing oldest dump file: $oldest_zip"
sudo rm -f "$oldest_zip" || {
echo "Error: Failed to remove $oldest_zip."
continue
}
fi
echo "Gitea dump completed."
} || {
echo "Error on backing up via gitea dump [$dir]"
continue
}
;;
"$immich")
# immich - backup specific dirs only
echo "Backing up immich [$dir]..."
{
timestamp=$(date +%s%3n)
sudo docker exec -t immich_postgres pg_dumpall --clean --if-exists --username=postgres | gzip | sudo tee "$dir"/library/backups/immich-db-backup-"$timestamp".sql.gz >/dev/null
# docker-compose, env files, etc.
find "$dir" -maxdepth 1 -type f -print0 | xargs -0 -I {} rsync -azvh --delete --progress {} "$bk_dir"
# libs
sudo rsync -azvh --delete --progress "$dir"/library/library "$bk_dir"
# profile
sudo rsync -azvh --delete --progress "$dir"/library/profile "$bk_dir"
# database
sudo rsync -azvh --delete --progress "$dir"/library/backups "$bk_dir"
# imgs/vids
sudo rsync -azvh --delete --progress "$dir"/library/upload "$bk_dir"
echo "Successfully backed up immich"
} || {
echo "Error on rsync [$dir]"
continue
}
;;
*)
echo "Backing up $dir..."
sudo rsync -azvh --delete --progress "$dir"/ "$bk_dir"/ || {
echo "Error on rsync [$dir]"
continue
}
;;
esac
done
}
# offsite backup via restic
remote_backup() {
restic_env="$HOME/services/restic/restic-env"
if [[ -e $restic_env ]]; then
echo "Sourcing $restic_env"
source "$restic_env"
else
echo "The required restic-env file doesn't exist. Aborting..."
# TODO: maybe help setup restic if don't have this
exit 1
fi
# backup whole $HOME/backups
restic backup "$backups" || {
echo "Restic failed"
return 1
}
restic check || {
echo "Restic integrity check failed"
return 1
}
}
# flags
if [[ "$#" -eq 0 ]]; then
echo "Running this script by itself (without flags) will run --all/-a by default."
read -rp "Do you want to continue? [y/n] " yn
case $yn in
[Yy]*)
echo "initializing full backup..."
{
local_backup
remote_backup
echo "Successfully finished backing up!"
curl -X POST -H "Title: π’ Successful Homelab Backup" -d "Finished updating local and offsite backups (snapshots)" "$NTFY_ENDPOINT"
} || {
echo "Failed to backup..."
curl -X POST -H "Title: π΄ Failed Homelab Backup" -d "An error occured when backing up..." "$NTFY_ENDPOINT"
}
;;
[Nn]*)
echo "Aborting..."
exit 0
;;
*) printf " Error: only write 'y' or 'n'\n\n" ;;
esac
else
for arg in "$@"; do
case $arg in
--all | backup)
echo "initializing full backup..."
{
local_backup
remote_backup
echo "Successfully finished backing up!"
curl -X POST -H "Title: π’ Successful Homelab Backup" -d "Finished updating local and offsite backups (snapshots)" "$NTFY_ENDPOINT"
} || {
echo "Failed to backup..."
curl -X POST -H "Title: π΄ Failed Homelab Backup" -d "An error occured when backing up..." "$NTFY_ENDPOINT"
}
;;
--dry-run)
echo "initializing dry-run..."
{
dry_run
echo "Successfully finished dry run!"
curl -X POST -H "Title: π’ [DRY-RUN] Successful Homelab Backup" -d "Finished updating local and offsite backups (snapshots)" "$NTFY_ENDPOINT"
} || {
echo "Failed to backup..."
curl -X POST -H "Title: π΄ Failed Homelab Backup" -d "An error occured when backing up..." "$NTFY_ENDPOINT"
}
;;
--local | local)
echo "initializing local (via rsync)..."
{
local_backup
echo "Successfully finished backing up!"
curl -X POST -H "Title: π’ Successful Homelab Backup" -d "Finished updating local backups" "$NTFY_ENDPOINT"
} || {
echo "Failed to backup..."
curl -X POST -H "Title: π΄ Failed Homelab Backup" -d "An error occured when backing up..." "$NTFY_ENDPOINT"
}
;;
--remote | remote)
echo "initializing remote backup (via restic)..."
{
remote_backup
echo "Successfully finished backing up!"
curl -X POST -H "Title: π’ Successful Homelab Backup" -d "Finished updating offsite backups (snapshots)" "$NTFY_ENDPOINT"
} || {
echo "Failed to backup..."
curl -X POST -H "Title: π΄ Failed Homelab Backup" -d "An error occured when backing up..." "$NTFY_ENDPOINT"
}
;;
*)
echo "Unknown flag: $arg"
exit 1
;;
esac
done
fi