|
@@ -3,7 +3,7 @@ import os
|
|
|
import subprocess
|
|
import subprocess
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
-from db import db, Job, Run
|
|
|
|
|
|
|
+from db import db, Job, Run, _size_human
|
|
|
|
|
|
|
|
|
|
|
|
|
BACKUP_DIR = None # initialisé depuis app.config
|
|
BACKUP_DIR = None # initialisé depuis app.config
|
|
@@ -41,42 +41,55 @@ def execute_job(job_id):
|
|
|
from jobs.utils import sudo_getsize
|
|
from jobs.utils import sudo_getsize
|
|
|
size_bytes = sudo_getsize(archive_path) or None
|
|
size_bytes = sudo_getsize(archive_path) or None
|
|
|
|
|
|
|
|
- run.status = "success"
|
|
|
|
|
|
|
+ # Checkpoint 1 : archive créée — persister immédiatement
|
|
|
run.archive_name = archive_name
|
|
run.archive_name = archive_name
|
|
|
run.size_bytes = size_bytes
|
|
run.size_bytes = size_bytes
|
|
|
- run.log_text = log
|
|
|
|
|
|
|
+ run.log_text = f"[archive] {archive_name} créée ({_size_human(size_bytes)})\n\n{log}"
|
|
|
|
|
+ db.session.commit()
|
|
|
|
|
|
|
|
from retention import apply_retention
|
|
from retention import apply_retention
|
|
|
deleted = apply_retention(job, archive_name, backup_dir)
|
|
deleted = apply_retention(job, archive_name, backup_dir)
|
|
|
if deleted:
|
|
if deleted:
|
|
|
- run.log_text += f"\n\nRétention : {len(deleted)} archive(s) supprimée(s) : {', '.join(deleted)}"
|
|
|
|
|
|
|
+ run.log_text += f"\n\nRétention locale : {len(deleted)} archive(s) supprimée(s) : {', '.join(deleted)}"
|
|
|
|
|
+ db.session.commit()
|
|
|
|
|
|
|
|
- # Transfert automatique post-backup
|
|
|
|
|
|
|
+ # Checkpoint 2 : transfert
|
|
|
if job.destination_id:
|
|
if job.destination_id:
|
|
|
from db import Destination
|
|
from db import Destination
|
|
|
dest = db.session.get(Destination, job.destination_id)
|
|
dest = db.session.get(Destination, job.destination_id)
|
|
|
if dest and dest.enabled:
|
|
if dest and dest.enabled:
|
|
|
data_dir = current_app.config["DATA_DIR"]
|
|
data_dir = current_app.config["DATA_DIR"]
|
|
|
|
|
+ run.log_text += f"\n\nTransfert → {dest.remote_str} : démarré…"
|
|
|
|
|
+ db.session.commit()
|
|
|
try:
|
|
try:
|
|
|
from jobs.transfer import transfer_archive
|
|
from jobs.transfer import transfer_archive
|
|
|
transfer_log = transfer_archive(archive_name, dest, backup_dir, data_dir)
|
|
transfer_log = transfer_archive(archive_name, dest, backup_dir, data_dir)
|
|
|
- run.log_text += f"\n\nTransfert → {dest.remote_str} :\n{transfer_log}"
|
|
|
|
|
|
|
+ run.log_text += f"\n{transfer_log}"
|
|
|
except Exception as transfer_exc:
|
|
except Exception as transfer_exc:
|
|
|
- run.log_text += f"\n\n⚠ Transfert échoué vers {dest.remote_str} :\n{transfer_exc}"
|
|
|
|
|
|
|
+ run.log_text += f"\n⚠ Transfert échoué : {transfer_exc}"
|
|
|
|
|
+ db.session.commit()
|
|
|
elif job.remote_instance_id:
|
|
elif job.remote_instance_id:
|
|
|
from db import RemoteInstance
|
|
from db import RemoteInstance
|
|
|
inst = db.session.get(RemoteInstance, job.remote_instance_id)
|
|
inst = db.session.get(RemoteInstance, job.remote_instance_id)
|
|
|
if inst:
|
|
if inst:
|
|
|
|
|
+ run.log_text += f"\n\nTransfert HTTP → {inst.name} ({inst.url}) : démarré…"
|
|
|
|
|
+ db.session.commit()
|
|
|
try:
|
|
try:
|
|
|
from jobs.transfer import push_archive_to_instance
|
|
from jobs.transfer import push_archive_to_instance
|
|
|
transfer_log = push_archive_to_instance(archive_name, inst, backup_dir, job=job)
|
|
transfer_log = push_archive_to_instance(archive_name, inst, backup_dir, job=job)
|
|
|
- run.log_text += f"\n\nTransfert HTTP → {inst.name} :\n{transfer_log}"
|
|
|
|
|
|
|
+ run.log_text += f"\n{transfer_log}"
|
|
|
except Exception as transfer_exc:
|
|
except Exception as transfer_exc:
|
|
|
- run.log_text += f"\n\n⚠ Transfert HTTP échoué vers {inst.name} :\n{transfer_exc}"
|
|
|
|
|
|
|
+ run.log_text += f"\n⚠ Transfert HTTP échoué : {transfer_exc}"
|
|
|
|
|
+ db.session.commit()
|
|
|
|
|
+
|
|
|
|
|
+ run.status = "success"
|
|
|
|
|
|
|
|
except Exception as exc:
|
|
except Exception as exc:
|
|
|
run.status = "error"
|
|
run.status = "error"
|
|
|
- run.log_text = str(exc)
|
|
|
|
|
|
|
+ if run.log_text:
|
|
|
|
|
+ run.log_text += f"\n\nErreur fatale : {exc}"
|
|
|
|
|
+ else:
|
|
|
|
|
+ run.log_text = str(exc)
|
|
|
|
|
|
|
|
finally:
|
|
finally:
|
|
|
run.finished_at = datetime.utcnow()
|
|
run.finished_at = datetime.utcnow()
|