#!/bin/bash DB="QNAPHybridBackupSync.db" FIXED="QNAPHybridBackupSync.db_fixed" if [ ! -f "$DB" ]; then echo "Error: $DB not found in current folder!" exit 1 fi echo "Found $DB" echo "Step 1: Forcing WAL checkpoint to merge latest data..." sqlite3 "$DB" "PRAGMA wal_checkpoint(FULL);" > /dev/null 2>&1 sleep 2 echo "Step 2: Backing up original database..." cp "$DB" "${DB}_backup_$(date +%Y%m%d_%H%M%S)" echo "Step 3: Dumping and rebuilding database..." sqlite3 "$DB" ".output repair_dump.sql" ".dump" > /dev/null 2>&1 if [ -s repair_dump.sql ]; then sqlite3 "$FIXED" < repair_dump.sql > /dev/null 2>&1 rm -f repair_dump.sql else echo "Step 3 FAILED: .dump produced no data!" echo "Repair stopped." exit 1 fi echo "Step 4: Verifying the new database..." CHECK=$(sqlite3 "$FIXED" "PRAGMA integrity_check;" 2>/dev/null) if echo "$CHECK" | grep -q "^ok$"; then echo "Step 4: PASSED -> Database is 100% healthy!" echo "" echo "Automatically replacing the old corrupted file..." mv "$FIXED" "$DB" echo "Replacement complete!" echo "" echo "====================================================" echo "REPAIR SUCCESSFUL! HBS can be used normally now." echo "====================================================" else echo "Step 4: FAILED -> Still corrupted" echo "Details:" echo "$CHECK" echo "" echo "A repaired attempt file has been kept: $FIXED" echo "Please send this file + the backup to engineering team." fi echo "All finished!"