Backup before syncing server scripts

This commit is contained in:
Hamza-Ayed
2026-08-16 13:17:56 +03:00
parent dc9d546733
commit 32865e78df
26 changed files with 3919 additions and 2123 deletions
+62 -12
View File
@@ -11,7 +11,7 @@
set -e # Exit on error
echo "🚀 Starting 10-day map update (v2 — Jordan + Syria + Egypt + Delta)..."
echo "🚀 Starting 10-day map update (v3 — Jordan + Syria + Egypt + Iraq + Delta)..."
APP_DIR="/home/hamzadoctor/app"
DATA_DIR="${APP_DIR}/infrastructure/osm-data"
@@ -21,27 +21,28 @@ DELTA_FILE="${DATA_DIR}/delta.osm"
cd "${APP_DIR}"
# ── Step 1: Download all three country PBFs ────────────────────────────────
echo "🌍 Downloading Jordan, Syria & Egypt PBF data from Geofabrik..."
# ── Step 1: Download all four country PBFs ─────────────────────────────────
echo "🌍 Downloading Jordan, Syria, Egypt & Iraq PBF data from Geofabrik..."
wget -q --show-progress -O "${DATA_DIR}/jordan-latest.osm.pbf.new" \
"https://download.geofabrik.de/asia/jordan-latest.osm.pbf"
wget -q --show-progress -O "${DATA_DIR}/syria-latest.osm.pbf.new" \
"https://download.geofabrik.de/africa/egypt-latest.osm.pbf"
# ✅ FIX: Syria URL was accidentally downloading Egypt above in old script — corrected
wget -q --show-progress -O "${DATA_DIR}/egypt-latest.osm.pbf.new" \
"https://download.geofabrik.de/africa/egypt-latest.osm.pbf"
# ✅ FIX: كان يحمّل مصر إلى ملف سوريا ثم يعيد تحميل سوريا فوقها — هدر 177MB كل دورة
wget -q --show-progress -O "${DATA_DIR}/syria-latest.osm.pbf.new" \
"https://download.geofabrik.de/asia/syria-latest.osm.pbf"
wget -q --show-progress -O "${DATA_DIR}/egypt-latest.osm.pbf.new" \
"https://download.geofabrik.de/africa/egypt-latest.osm.pbf"
wget -q --show-progress -O "${DATA_DIR}/iraq-latest.osm.pbf.new" \
"https://download.geofabrik.de/asia/iraq-latest.osm.pbf"
mv "${DATA_DIR}/jordan-latest.osm.pbf.new" "${DATA_DIR}/jordan-latest.osm.pbf"
mv "${DATA_DIR}/syria-latest.osm.pbf.new" "${DATA_DIR}/syria-latest.osm.pbf"
mv "${DATA_DIR}/egypt-latest.osm.pbf.new" "${DATA_DIR}/egypt-latest.osm.pbf"
mv "${DATA_DIR}/iraq-latest.osm.pbf.new" "${DATA_DIR}/iraq-latest.osm.pbf"
echo "✅ Downloads complete."
# ── Step 2: Import all three countries into PostGIS ───────────────────────
# ── Step 2: Import all four countries into PostGIS ─────────────────────────
echo "💾 Importing Jordan into PostGIS (--create resets planet_osm_* tables)..."
docker compose --profile import run --rm osm-import \
osm2pgsql --create --slim --cache 1000 \
@@ -61,13 +62,30 @@ docker compose --profile import run --rm osm-import \
--database mapdb --host db --user mapuser \
/data/egypt-latest.osm.pbf
# ── Step 3: Merge all three PBFs into master_map.osm.pbf ─────────────────
echo "💾 Importing Iraq into PostGIS (--append)..."
docker compose --profile import run --rm osm-import \
osm2pgsql --append --slim --cache 1000 \
--database mapdb --host db --user mapuser \
/data/iraq-latest.osm.pbf
# ── Step 2b: إسقاط جداول osm2pgsql الوسيطة ────────────────────────────────
# planet_osm_nodes/ways/rels ينشئها --slim لدعم --append فقط، ولا يقرأها
# التطبيق ولا martin إطلاقاً (جداول الإخراج هي line/polygon/point/roads).
# قياس 30/07/2026: nodes 5031MB + ways 1400MB ≈ 6.4GB نائمة بين الدورات.
# آمن لأن --create في الخطوة 2 يعيد إنشاءها من الصفر كل تشغيل.
# ⚠️ لا تشغّل osm2pgsql --append بعد هذه النقطة حتى الدورة القادمة.
echo "🧹 إسقاط جداول osm2pgsql الوسيطة (تُعاد في الدورة القادمة)..."
docker compose exec -T db psql -U mapuser -d mapdb -c \
"DROP TABLE IF EXISTS planet_osm_nodes, planet_osm_ways, planet_osm_rels;"
# ── Step 3: Merge all four PBFs into master_map.osm.pbf ────────────────────
# ✅ FIX: Old script wrote to region.osm.pbf — GraphHopper reads master_map.osm.pbf
echo "🗺️ Merging Jordan + Syria + Egypt → ${MASTER_FILE}..."
echo "🗺️ Merging Jordan + Syria + Egypt + Iraq → ${MASTER_FILE}..."
osmium merge \
"${DATA_DIR}/jordan-latest.osm.pbf" \
"${DATA_DIR}/syria-latest.osm.pbf" \
"${DATA_DIR}/egypt-latest.osm.pbf" \
"${DATA_DIR}/iraq-latest.osm.pbf" \
-o "${MASTER_FILE}" --overwrite
echo "✅ Master PBF built: $(du -sh ${MASTER_FILE} | cut -f1)"
@@ -92,16 +110,48 @@ docker compose exec -T db psql -U mapuser -d mapdb -t -c \
echo "✅ Landmark sync done."
# ── Step 6: Rebuild GraphHopper routing index ─────────────────────────────
# ذروة البناء لأربع دول ~3GB. على سيرفر بـ7.8GB الهامش ضيق، فنُخلي الذاكرة
# للحاويات غير الضرورية أثناء البناء فقط ثم نعيدها. db و redis يبقيان لأن
# الخطوات التالية تحتاجهما.
echo "🚗 Rebuilding GraphHopper routing graph (may take 5-10 min)..."
echo "⏸️ إيقاف مؤقت للحاويات غير الضرورية لتحرير الذاكرة أثناء البناء..."
docker compose stop api web dashboard martin || true
# نُعيدها مهما كانت نتيجة البناء — بما في ذلك الخروج المبكر عند الفشل
trap 'echo "▶️ إعادة تشغيل الحاويات المؤقتة..."; docker compose up -d api web dashboard martin || true' EXIT
docker compose stop routing
rm -rf "${DATA_DIR}/graph-cache" "${DATA_DIR}/default-gh"
docker compose up -d routing
echo "✅ GraphHopper restarting from ${MASTER_FILE}."
# ── Step 6b: بوابة تحقق — اتجاهات السير ──────────────────────────────────
# انحدار 26/07/2026: سقوط قاعدة "!car_access → 0" جعل المحرك يتجاهل كل oneway
# وكل دوار لأيام دون أن يلاحظ أحد. لا نُنهي التحديث دون إثبات أنها تعمل.
# التفاصيل: docs/ROUTING_ONEWAY_AR.md
echo "⏳ انتظار جهوزية المحرك قبل فحص الاتجاهات..."
GH_READY=0
for _ in $(seq 1 90); do
if curl -fsS http://localhost:8989/health >/dev/null 2>&1; then GH_READY=1; break; fi
sleep 10
done
if [ "$GH_READY" = "0" ]; then
echo "❌ المحرك لم يجهز خلال 15 دقيقة — راجع: docker compose logs -f routing"
echo " (السبب المتكرر: exited with code 137 أي نفاد ذاكرة أثناء البناء)"
exit 1
fi
if ! APP_DIR="${APP_DIR}" bash "${APP_DIR}/infrastructure/scripts/verify-oneway.sh"; then
echo ""
echo "❌❌ المحرك يتجاهل اتجاهات السير — التوجيه غير صالح للاستخدام."
echo " اقرأ: docs/ROUTING_ONEWAY_AR.md"
exit 1
fi
# ── Step 7: Cache flush ───────────────────────────────────────────────────
echo "🧹 Flushing Redis traffic cache..."
docker compose exec -T redis redis-cli flushall
echo ""
echo "✅ 10-day update complete — Jordan + Syria + Egypt + approved delta applied."
echo "✅ 10-day update complete — Jordan + Syria + Egypt + Iraq + approved delta applied."
echo " GraphHopper is rebuilding in the background. Allow 5-10 min for routing to be ready."