|
|
@@ -1,7 +1,6 @@
|
|
|
import json
|
|
|
import os
|
|
|
import subprocess
|
|
|
-import tarfile
|
|
|
import tempfile
|
|
|
import time
|
|
|
from datetime import datetime
|
|
|
@@ -123,11 +122,16 @@ def restore_db_dump(archive_name, backup_dir):
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
dump_path = os.path.join(tmpdir, f"{dbname}.sql")
|
|
|
|
|
|
- # Extraction du dump depuis l'archive
|
|
|
- with tarfile.open(archive_path) as tar:
|
|
|
- member = tar.getmember(f"db/{dbname}.sql")
|
|
|
- with tar.extractfile(member) as src, open(dump_path, "wb") as dst:
|
|
|
- dst.write(src.read())
|
|
|
+ # Extraction du dump depuis l'archive via sudo (backup_dir est 750 root)
|
|
|
+ result = subprocess.run(
|
|
|
+ ["sudo", "tar", "-xOf", archive_path, f"db/{dbname}.sql"],
|
|
|
+ capture_output=True,
|
|
|
+ )
|
|
|
+ if result.returncode != 0:
|
|
|
+ err = result.stderr.decode("utf-8", errors="replace").strip()
|
|
|
+ raise RuntimeError(f"Extraction du dump échouée : {err}")
|
|
|
+ with open(dump_path, "wb") as dst:
|
|
|
+ dst.write(result.stdout)
|
|
|
|
|
|
if db_type == "mysql":
|
|
|
return _restore_mysql(dbname, dump_path)
|
|
|
@@ -208,14 +212,8 @@ def _restore_postgresql(dbname, dump_path):
|
|
|
|
|
|
|
|
|
def _read_backup_info(archive_path):
|
|
|
- try:
|
|
|
- with tarfile.open(archive_path) as tar:
|
|
|
- member = tar.extractfile("backup_info.json")
|
|
|
- if member:
|
|
|
- return json.loads(member.read())
|
|
|
- except Exception:
|
|
|
- pass
|
|
|
- return {}
|
|
|
+ from jobs.utils import sudo_read_backup_info
|
|
|
+ return sudo_read_backup_info(archive_path)
|
|
|
|
|
|
|
|
|
def _write_tar(tmpdir, dump_path, dbname, archive_name, backup_dir, job, instance, instance_url):
|