Replace backup script with autorestic

This commit is contained in:
Tobias Reisinger 2024-10-06 01:59:46 +02:00
parent 13084e3558
commit ed51a86935
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
17 changed files with 180 additions and 224 deletions
roles/backup/templates

View file

@ -1,11 +1,11 @@
[Unit]
Description=Autostart several tools and services
Description=Run the backup script
StartLimitIntervalSec=7200
StartLimitBurst=5
[Service]
Type=oneshot
ExecStart={{ service_path }}/backup.sh
ExecStart={{ (service_path, 'backup.sh') | path_join }}
WorkingDirectory={{ service_path }}
Restart=on-failure
RestartSec=15min

74
roles/backup/templates/backup.sh.j2 Executable file → Normal file
View file

@ -1,68 +1,12 @@
#!/usr/bin/env bash
set -e
{{ backup_hc_command_start }}
set -a
. "{{ service_path }}/service.env"
set +a
duration_start=$(date +%s)
_duration_get () {
duration_end=$(date +%s)
echo "$((duration_end - duration_start))"
}
hc_url="https://hc-ping.com/$HC_UID"
uptime_kuma_url="https://status.serguzim.me/api/push/$UPTIME_KUMA_TOKEN"
_hc_ping () {
curl -fsSL --retry 3 "$hc_url$1" >/dev/null
}
_uptime_kuma_ping () {
duration=$(_duration_get)
curl -fsSL --retry 3 \
--url-query "status=$1" \
--url-query "msg=$2" \
--url-query "ping=${duration}000" \
"$uptime_kuma_url" >/dev/null
}
_fail () {
_hc_ping "/fail"
_uptime_kuma_ping "down" "$1"
rm -rf "$BACKUP_LOCATION"
exit 1
}
_success () {
_hc_ping
_uptime_kuma_ping "up" "backup successful"
}
_hc_ping "/start"
BACKUP_LOCATION="$(mktemp -d --suffix=-backup)"
export BACKUP_LOCATION
cd "$BACKUP_LOCATION" || _fail "failed to cd to $BACKUP_LOCATION"
shopt -s nullglob
for file in "{{ service_path }}/backup.d/"*
do
file_name="$(basename "$file")"
echo ""
echo "running $file_name"
time "$file" >"/tmp/$file_name.log" || _fail "error while running $file_name"
done || true
cd "{{ service_path }}"
docker compose run --rm -v "$BACKUP_LOCATION:/backup/misc" app backup /backup || _fail "error during restic backup"
_success
rm -rf "$BACKUP_LOCATION"
echo "forgetting old backups for {{ ansible_facts.hostname }}"
docker compose run --rm app forget --host "{{ ansible_facts.hostname }}" --prune \
--keep-last 7 \
--keep-daily 14 \
--keep-weekly 16 \
--keep-monthly 12 \
--keep-yearly 2
if autorestic backup -av --ci
then
{{ backup_hc_command_success }}
{{ backup_uk_command_success }}
else
{{ backup_hc_command_fail }}
{{ backup_uk_command_fail }}
fi