fix: add auto-seeding from backup
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 41s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 41s
This commit is contained in:
@@ -46,25 +46,6 @@ wait_for_litefs() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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 with TTL Check
|
||||||
register_service() {
|
register_service() {
|
||||||
echo "Registering service ${SERVICE_ID} with Consul..."
|
echo "Registering service ${SERVICE_ID} with Consul..."
|
||||||
@@ -99,6 +80,19 @@ start_app() {
|
|||||||
# Ensure shared directories exist
|
# Ensure shared directories exist
|
||||||
mkdir -p /shared_data/plugins /shared_data/cache /shared_data/backup /shared_data/artist_images
|
mkdir -p /shared_data/plugins /shared_data/cache /shared_data/backup /shared_data/artist_images
|
||||||
|
|
||||||
|
# SEEDING LOGIC: If DB doesn't exist, restore from backup
|
||||||
|
if [ ! -f /data/navidrome.db ]; then
|
||||||
|
echo "Database /data/navidrome.db not found. Looking for backups to seed..."
|
||||||
|
local latest_backup=$(ls -t /shared_data/backup/navidrome.db_*.bak 2>/dev/null | head -n 1)
|
||||||
|
if [ -n "$latest_backup" ]; then
|
||||||
|
echo "Seeding from $latest_backup..."
|
||||||
|
# We use litefs import to properly initialize the distributed state
|
||||||
|
litefs import -name navidrome.db "$latest_backup"
|
||||||
|
else
|
||||||
|
echo "No backups found. Navidrome will start with a fresh database."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Wait for LiteFS to expose the DB file in the FUSE mount
|
# Wait for LiteFS to expose the DB file in the FUSE mount
|
||||||
echo "Waiting for /data/navidrome.db..."
|
echo "Waiting for /data/navidrome.db..."
|
||||||
local db_timeout=30
|
local db_timeout=30
|
||||||
@@ -155,9 +149,6 @@ trap cleanup TERM INT
|
|||||||
echo "Starting Supervisor. Waiting for leadership settle..."
|
echo "Starting Supervisor. Waiting for leadership settle..."
|
||||||
wait_for_litefs || exit 1
|
wait_for_litefs || exit 1
|
||||||
|
|
||||||
LAST_BACKUP_TIME=$(date +%s)
|
|
||||||
BACKUP_INTERVAL=86400 # 24 hours
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
if check_primary; then
|
if check_primary; then
|
||||||
# === WE ARE PRIMARY ===
|
# === WE ARE PRIMARY ===
|
||||||
@@ -166,13 +157,6 @@ while true; do
|
|||||||
register_service
|
register_service
|
||||||
fi
|
fi
|
||||||
pass_ttl
|
pass_ttl
|
||||||
|
|
||||||
# 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
|
else
|
||||||
# === WE ARE REPLICA ===
|
# === WE ARE REPLICA ===
|
||||||
if [ "${NAVIDROME_PID}" -gt 0 ]; then
|
if [ "${NAVIDROME_PID}" -gt 0 ]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user