changement vers google drive storage pour les contrats.
CI - Security, Lint & Tests / validate (push) Failing after 1m15s
CI - Security, Lint & Tests / validate (push) Failing after 1m15s
Tentative d'obtenir un token de connection échoue
This commit is contained in:
@@ -42,7 +42,11 @@ from urllib.parse import unquote, urlparse
|
||||
# to be — see DOCUMENTS_DIR below.
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from app.storage import backups_root, documents_root # noqa: E402 — needs the path above
|
||||
from app.storage import ( # noqa: E402 — needs the path above
|
||||
backups_root,
|
||||
document_storage_backend,
|
||||
documents_root,
|
||||
)
|
||||
|
||||
# Configuration
|
||||
BACKUP_DIR = backups_root()
|
||||
@@ -292,6 +296,75 @@ def backup_documents():
|
||||
return zip_path
|
||||
|
||||
|
||||
def backup_drive_documents():
|
||||
"""Download every file from the configured Google Drive folder and archive it.
|
||||
|
||||
Called **in addition to** ``backup_documents()`` when the storage backend is
|
||||
``google_drive``. When the backend is ``local`` this function silently
|
||||
skips — local backups already handled by ``backup_documents()``.
|
||||
|
||||
Returns:
|
||||
str | None: Path to the created archive, or None when skipped.
|
||||
|
||||
Raises:
|
||||
BackupError: If Drive is configured but listing or downloading fails.
|
||||
"""
|
||||
if document_storage_backend() != 'google_drive':
|
||||
print('[SKIP] Google Drive is not configured; local documents only.')
|
||||
return None
|
||||
|
||||
try:
|
||||
from app.google_drive import (
|
||||
GoogleDriveStorageError,
|
||||
list_folder_files,
|
||||
)
|
||||
from app.google_drive import (
|
||||
download_file as download_drive_file,
|
||||
)
|
||||
except ImportError:
|
||||
raise BackupError(
|
||||
'Google Drive dependencies are not installed. Install requirements.txt again.'
|
||||
) from None
|
||||
|
||||
print('[INFO] Listing Google Drive contract folder...')
|
||||
try:
|
||||
files = list_folder_files()
|
||||
except (GoogleDriveStorageError, Exception) as exc: # noqa: BLE001 — boundary
|
||||
raise BackupError(f'Google Drive listing failed: {exc}') from exc
|
||||
|
||||
if not files:
|
||||
print('[INFO] Google Drive folder is empty; no Drive documents to back up.')
|
||||
return None
|
||||
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
archive_basename = os.path.join(BACKUP_DIR, f'documents_backup_drive_{timestamp}')
|
||||
|
||||
download = {}
|
||||
for file_id, name in files:
|
||||
print(f' Downloading {name} ({file_id})...')
|
||||
try:
|
||||
download[name] = download_drive_file(file_id)
|
||||
except (GoogleDriveStorageError, Exception) as exc: # noqa: BLE001 — boundary
|
||||
raise BackupError(f'Google Drive download failed for {name}: {exc}') from exc
|
||||
|
||||
try:
|
||||
import zipfile
|
||||
|
||||
zip_path = f'{archive_basename}.zip'
|
||||
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
||||
for name, data in download.items():
|
||||
zf.writestr(name, data)
|
||||
except Exception as exc: # noqa: BLE001 — boundary
|
||||
raise BackupError(f'Drive document archive failed: {exc}') from exc
|
||||
|
||||
if not os.path.exists(zip_path) or os.path.getsize(zip_path) == 0:
|
||||
raise BackupError('Drive document archiver reported success but produced an empty file.')
|
||||
|
||||
size_mb = os.path.getsize(zip_path) / (1024 * 1024)
|
||||
print(f'[OK] Drive documents backed up to: {zip_path} ({size_mb:.1f} MB)')
|
||||
return zip_path
|
||||
|
||||
|
||||
def cleanup_old_backups():
|
||||
"""Remove backup files older than BACKUP_RETENTION_DAYS."""
|
||||
if not os.path.exists(BACKUP_DIR):
|
||||
@@ -365,10 +438,16 @@ def main(argv=None):
|
||||
except BackupError as exc:
|
||||
documents_ok = False
|
||||
print(f'[ERROR] {exc}')
|
||||
drive_ok = True
|
||||
try:
|
||||
backup_drive_documents()
|
||||
except BackupError as exc:
|
||||
drive_ok = False
|
||||
print(f'[ERROR] {exc}')
|
||||
cleanup_old_backups()
|
||||
|
||||
print()
|
||||
if verified and documents_ok:
|
||||
if verified and documents_ok and drive_ok:
|
||||
print('=== Backup completed successfully ===')
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user