|
|
@@ -175,46 +175,66 @@ def archive_restore(archive_name):
|
|
|
if request.method == "GET":
|
|
|
return render_template("restore_confirm.html", archive_name=archive_name, info=info)
|
|
|
|
|
|
+ # Trouver le job_id depuis le Run original pour pouvoir tracer la restauration
|
|
|
+ original_run = Run.query.filter_by(archive_name=archive_name).first()
|
|
|
+ restore_run_id = None
|
|
|
+ if original_run:
|
|
|
+ restore_run = Run(
|
|
|
+ job_id=original_run.job_id,
|
|
|
+ started_at=datetime.utcnow(),
|
|
|
+ status="running",
|
|
|
+ archive_name=archive_name,
|
|
|
+ log_text="[RESTAURATION en cours…]",
|
|
|
+ )
|
|
|
+ db.session.add(restore_run)
|
|
|
+ db.session.commit()
|
|
|
+ restore_run_id = restore_run.id
|
|
|
+
|
|
|
def _do_restore():
|
|
|
with app.app_context():
|
|
|
+ run = db.session.get(Run, restore_run_id) if restore_run_id else None
|
|
|
try:
|
|
|
backup_dir = app.config["YUNOHOST_BACKUP_DIR"]
|
|
|
if archive_type == "custom_dir":
|
|
|
from jobs.custom_dir import restore_custom_dir
|
|
|
- restore_custom_dir(archive_name, backup_dir)
|
|
|
+ log = restore_custom_dir(archive_name, backup_dir)
|
|
|
elif archive_type in ("mysql", "postgresql"):
|
|
|
from jobs.db_dump import restore_db_dump
|
|
|
- restore_db_dump(archive_name, backup_dir)
|
|
|
+ log = restore_db_dump(archive_name, backup_dir)
|
|
|
elif archive_type == "ynh_app":
|
|
|
result = subprocess.run(
|
|
|
["sudo", "yunohost", "backup", "restore", archive_name,
|
|
|
"--apps", "--force"],
|
|
|
capture_output=True, text=True, timeout=3600,
|
|
|
)
|
|
|
+ log = (result.stdout + result.stderr).strip()
|
|
|
if result.returncode != 0:
|
|
|
- raise RuntimeError(
|
|
|
- f"yunohost backup restore a échoué :\n"
|
|
|
- f"{(result.stdout + result.stderr).strip()}"
|
|
|
- )
|
|
|
- app.logger.info(f"Restauration ynh_app {archive_name} OK")
|
|
|
+ raise RuntimeError(f"yunohost backup restore a échoué :\n{log}")
|
|
|
elif archive_type == "ynh_system":
|
|
|
result = subprocess.run(
|
|
|
["sudo", "yunohost", "backup", "restore", archive_name,
|
|
|
"--system", "--force"],
|
|
|
capture_output=True, text=True, timeout=3600,
|
|
|
)
|
|
|
+ log = (result.stdout + result.stderr).strip()
|
|
|
if result.returncode != 0:
|
|
|
- raise RuntimeError(
|
|
|
- f"yunohost backup restore a échoué :\n"
|
|
|
- f"{(result.stdout + result.stderr).strip()}"
|
|
|
- )
|
|
|
- app.logger.info(f"Restauration ynh_system {archive_name} OK")
|
|
|
+ raise RuntimeError(f"yunohost backup restore a échoué :\n{log}")
|
|
|
else:
|
|
|
raise NotImplementedError(
|
|
|
f"Restauration non supportée pour le type '{archive_type}'."
|
|
|
)
|
|
|
+ if run:
|
|
|
+ run.status = "success"
|
|
|
+ run.finished_at = datetime.utcnow()
|
|
|
+ run.log_text = f"[RESTAURATION]\n{log or 'OK'}"
|
|
|
+ db.session.commit()
|
|
|
except Exception as exc:
|
|
|
app.logger.error(f"Restauration {archive_name} échouée : {exc}")
|
|
|
+ if run:
|
|
|
+ run.status = "error"
|
|
|
+ run.finished_at = datetime.utcnow()
|
|
|
+ run.log_text = f"[RESTAURATION]\n{exc}"
|
|
|
+ db.session.commit()
|
|
|
|
|
|
import threading
|
|
|
threading.Thread(target=_do_restore, daemon=True).start()
|