feat: Add automated LiteFS backups and GitHub deployment workflow
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m52s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m52s
This commit is contained in:
@@ -16,6 +16,25 @@ PRIMARY_TAGS='["navidrome","web","traefik.enable=true","urlprefix-/navidrome","t
|
||||
|
||||
# --- Helper Functions ---
|
||||
|
||||
# Backup Database (Only on Primary)
|
||||
run_backup() {
|
||||
local backup_dir="/shared_data/backup"
|
||||
local timestamp=$(date +%Y%m%d_%H%M%S)
|
||||
local backup_file="${backup_dir}/navidrome.db_${timestamp}.bak"
|
||||
|
||||
echo "Backing up database to ${backup_file}..."
|
||||
mkdir -p "$backup_dir"
|
||||
|
||||
if litefs export -name navidrome.db "$backup_file"; then
|
||||
echo "Backup successful."
|
||||
# Keep only last 7 days
|
||||
find "$backup_dir" -name "navidrome.db_*.bak" -mtime +7 -delete
|
||||
echo "Old backups cleaned."
|
||||
else
|
||||
echo "ERROR: Backup failed!"
|
||||
fi
|
||||
}
|
||||
|
||||
# Register Service with TTL Check
|
||||
register_service() {
|
||||
echo "Promoted! Registering service ${SERVICE_ID}..."
|
||||
@@ -86,6 +105,9 @@ echo "Consul: $CONSUL_HTTP_ADDR"
|
||||
# Small sleep to let LiteFS settle and leadership election complete
|
||||
sleep 5
|
||||
|
||||
LAST_BACKUP_TIME=0
|
||||
BACKUP_INTERVAL=86400 # 24 hours
|
||||
|
||||
while true; do
|
||||
# In LiteFS 0.5, .primary file exists ONLY on replicas.
|
||||
if [ ! -f "$DB_LOCK_FILE" ]; then
|
||||
@@ -103,6 +125,13 @@ while true; do
|
||||
# 2. Maintain the heartbeat (TTL)
|
||||
pass_ttl
|
||||
|
||||
# 3. Handle periodic backup
|
||||
CURRENT_TIME=$(date +%s)
|
||||
if [ $((CURRENT_TIME - LAST_BACKUP_TIME)) -ge $BACKUP_INTERVAL ]; then
|
||||
run_backup
|
||||
LAST_BACKUP_TIME=$CURRENT_TIME
|
||||
fi
|
||||
|
||||
else
|
||||
# === WE ARE REPLICA ===
|
||||
|
||||
@@ -111,6 +140,8 @@ while true; do
|
||||
echo "Lost leadership. Demoting..."
|
||||
stop_app
|
||||
deregister_service
|
||||
# Reset backup timer so the next primary can start fresh or we start fresh if promoted again
|
||||
LAST_BACKUP_TIME=0
|
||||
fi
|
||||
|
||||
# No service registration exists for replicas to keep Consul clean.
|
||||
|
||||
Reference in New Issue
Block a user