File size: 13,099 Bytes
a74b879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""
Backup System - Automated Database Backups with Cloud Storage
"""
import os
import shutil
import sqlite3
import json
import gzip
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional
import subprocess

OUTPUT_DIR = Path(os.environ.get('OUTPUT_DIR', './output'))
BACKUP_DIR = OUTPUT_DIR / 'backups'
BACKUP_DIR.mkdir(parents=True, exist_ok=True)

# Database files to backup
DB_FILES = [
    OUTPUT_DIR / 'jobs.db',
    OUTPUT_DIR / 'users.db',
    OUTPUT_DIR / 'security.db',
    OUTPUT_DIR / 'competitor_cache.db',
    OUTPUT_DIR / 'result_cache.db'
]

# ── Backup Functions ────────────────────────────────────────────────────────

def create_backup(compress: bool = True) -> dict:
    """Create full backup of all databases and important files"""
    timestamp = datetime.utcnow().strftime('%Y%m%d_%H%M%S')
    backup_name = f"backup_{timestamp}"
    backup_path = BACKUP_DIR / backup_name
    backup_path.mkdir(exist_ok=True)
    
    backed_up = []
    errors = []
    
    # Backup databases
    for db_file in DB_FILES:
        if db_file.exists():
            try:
                dest = backup_path / db_file.name
                shutil.copy2(db_file, dest)
                backed_up.append(str(db_file.name))
            except Exception as e:
                errors.append(f"{db_file.name}: {str(e)}")
    
    # Backup important JSON files
    json_files = [
        'audit.json', 'analysis.json', 'history.json', 
        'recommendations.json', 'settings.json'
    ]
    
    for json_file in json_files:
        src = OUTPUT_DIR / json_file
        if src.exists():
            try:
                dest = backup_path / json_file
                shutil.copy2(src, dest)
                backed_up.append(json_file)
            except Exception as e:
                errors.append(f"{json_file}: {str(e)}")
    
    # Create backup manifest
    manifest = {
        'timestamp': timestamp,
        'created_at': datetime.utcnow().isoformat(),
        'files': backed_up,
        'errors': errors,
        'compressed': False
    }
    
    manifest_path = backup_path / 'manifest.json'
    with open(manifest_path, 'w') as f:
        json.dump(manifest, f, indent=2)
    
    # Compress if requested
    if compress:
        try:
            archive_path = BACKUP_DIR / f"{backup_name}.tar.gz"
            shutil.make_archive(str(backup_path), 'gztar', backup_path)
            shutil.rmtree(backup_path)
            manifest['compressed'] = True
            manifest['archive_path'] = str(archive_path) + '.tar.gz'
            manifest['size_mb'] = round(Path(str(archive_path) + '.tar.gz').stat().st_size / 1024 / 1024, 2)
        except Exception as e:
            errors.append(f"Compression failed: {str(e)}")
    
    return manifest

def list_backups() -> list:
    """List all available backups"""
    backups = []
    
    # List compressed backups
    for archive in BACKUP_DIR.glob('backup_*.tar.gz'):
        try:
            stat = archive.stat()
            backups.append({
                'name': archive.stem,
                'path': str(archive),
                'size_mb': round(stat.st_size / 1024 / 1024, 2),
                'created_at': datetime.fromtimestamp(stat.st_mtime).isoformat(),
                'compressed': True
            })
        except Exception:
            pass
    
    # List uncompressed backups
    for backup_dir in BACKUP_DIR.glob('backup_*'):
        if backup_dir.is_dir():
            manifest_path = backup_dir / 'manifest.json'
            if manifest_path.exists():
                try:
                    with open(manifest_path) as f:
                        manifest = json.load(f)
                    backups.append({
                        'name': backup_dir.name,
                        'path': str(backup_dir),
                        'created_at': manifest.get('created_at'),
                        'files': manifest.get('files', []),
                        'compressed': False
                    })
                except Exception:
                    pass
    
    # Sort by creation time (newest first)
    backups.sort(key=lambda x: x['created_at'], reverse=True)
    return backups

def restore_backup(backup_name: str) -> dict:
    """Restore from backup"""
    backup_path = BACKUP_DIR / backup_name
    archive_path = BACKUP_DIR / f"{backup_name}.tar.gz"
    
    restored = []
    errors = []
    
    # Extract if compressed
    if archive_path.exists():
        try:
            shutil.unpack_archive(archive_path, BACKUP_DIR)
            backup_path = BACKUP_DIR / backup_name
        except Exception as e:
            return {'success': False, 'error': f"Extract failed: {str(e)}"}
    
    if not backup_path.exists():
        return {'success': False, 'error': 'Backup not found'}
    
    # Read manifest
    manifest_path = backup_path / 'manifest.json'
    if not manifest_path.exists():
        return {'success': False, 'error': 'Invalid backup (no manifest)'}
    
    with open(manifest_path) as f:
        manifest = json.load(f)
    
    # Restore databases
    for db_file in DB_FILES:
        src = backup_path / db_file.name
        if src.exists():
            try:
                # Backup current before overwriting
                if db_file.exists():
                    backup_current = db_file.with_suffix('.db.bak')
                    shutil.copy2(db_file, backup_current)
                
                shutil.copy2(src, db_file)
                restored.append(db_file.name)
            except Exception as e:
                errors.append(f"{db_file.name}: {str(e)}")
    
    # Restore JSON files
    for json_file in manifest.get('files', []):
        if json_file.endswith('.json'):
            src = backup_path / json_file
            dest = OUTPUT_DIR / json_file
            if src.exists():
                try:
                    shutil.copy2(src, dest)
                    restored.append(json_file)
                except Exception as e:
                    errors.append(f"{json_file}: {str(e)}")
    
    return {
        'success': len(errors) == 0,
        'restored': restored,
        'errors': errors,
        'backup_date': manifest.get('created_at')
    }

def delete_backup(backup_name: str) -> bool:
    """Delete a backup"""
    backup_path = BACKUP_DIR / backup_name
    archive_path = BACKUP_DIR / f"{backup_name}.tar.gz"
    
    try:
        if archive_path.exists():
            archive_path.unlink()
        if backup_path.exists() and backup_path.is_dir():
            shutil.rmtree(backup_path)
        return True
    except Exception:
        return False

def cleanup_old_backups(keep_days: int = 30):
    """Delete backups older than specified days"""
    cutoff = datetime.utcnow() - timedelta(days=keep_days)
    deleted = []
    
    for backup in list_backups():
        created_at = datetime.fromisoformat(backup['created_at'])
        if created_at < cutoff:
            if delete_backup(backup['name']):
                deleted.append(backup['name'])
    
    return deleted

# ── Cloud Backup (S3/GCS) ───────────────────────────────────────────────────

def upload_to_s3(backup_path: str, bucket: str, key: str) -> bool:
    """Upload backup to AWS S3"""
    try:
        import boto3
        s3 = boto3.client('s3',
            aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
            aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY')
        )
        s3.upload_file(backup_path, bucket, key)
        return True
    except Exception as e:
        print(f"❌ S3 upload failed: {e}")
        return False

def upload_to_gcs(backup_path: str, bucket: str, blob_name: str) -> bool:
    """Upload backup to Google Cloud Storage"""
    try:
        from google.cloud import storage
        client = storage.Client()
        bucket = client.bucket(bucket)
        blob = bucket.blob(blob_name)
        blob.upload_from_filename(backup_path)
        return True
    except Exception as e:
        print(f"❌ GCS upload failed: {e}")
        return False

def backup_to_cloud(backup_name: str) -> dict:
    """Upload backup to configured cloud storage"""
    archive_path = BACKUP_DIR / f"{backup_name}.tar.gz"
    
    if not archive_path.exists():
        return {'success': False, 'error': 'Backup archive not found'}
    
    results = {}
    
    # Try S3
    s3_bucket = os.getenv('BACKUP_S3_BUCKET')
    if s3_bucket:
        key = f"geo-platform-backups/{backup_name}.tar.gz"
        results['s3'] = upload_to_s3(str(archive_path), s3_bucket, key)
    
    # Try GCS
    gcs_bucket = os.getenv('BACKUP_GCS_BUCKET')
    if gcs_bucket:
        blob_name = f"geo-platform-backups/{backup_name}.tar.gz"
        results['gcs'] = upload_to_gcs(str(archive_path), gcs_bucket, blob_name)
    
    return {
        'success': any(results.values()),
        'results': results
    }

# ── Scheduled Backup ────────────────────────────────────────────────────────

def schedule_daily_backup():
    """Schedule daily backup (call this from scheduler)"""
    try:
        manifest = create_backup(compress=True)
        
        # Upload to cloud if configured
        if manifest.get('compressed'):
            backup_name = Path(manifest['archive_path']).stem
            backup_to_cloud(backup_name)
        
        # Cleanup old backups
        cleanup_old_backups(keep_days=30)
        
        return {'success': True, 'manifest': manifest}
    except Exception as e:
        return {'success': False, 'error': str(e)}

# ── Export/Import Data ──────────────────────────────────────────────────────

def export_all_data() -> dict:
    """Export all data to JSON format"""
    export_data = {
        'exported_at': datetime.utcnow().isoformat(),
        'version': '1.0',
        'data': {}
    }
    
    # Export from each database
    for db_file in DB_FILES:
        if db_file.exists():
            try:
                conn = sqlite3.connect(str(db_file))
                conn.row_factory = sqlite3.Row
                c = conn.cursor()
                
                # Get all tables
                c.execute("SELECT name FROM sqlite_master WHERE type='table'")
                tables = [row[0] for row in c.fetchall()]
                
                db_data = {}
                for table in tables:
                    c.execute(f"SELECT * FROM {table}")
                    rows = [dict(row) for row in c.fetchall()]
                    db_data[table] = rows
                
                export_data['data'][db_file.stem] = db_data
                conn.close()
            except Exception as e:
                export_data['data'][db_file.stem] = {'error': str(e)}
    
    return export_data

def import_data(data: dict) -> dict:
    """Import data from JSON format"""
    imported = []
    errors = []
    
    for db_name, db_data in data.get('data', {}).items():
        db_file = OUTPUT_DIR / f"{db_name}.db"
        
        try:
            conn = sqlite3.connect(str(db_file))
            c = conn.cursor()
            
            for table, rows in db_data.items():
                if isinstance(rows, list) and rows:
                    # Get column names
                    columns = list(rows[0].keys())
                    placeholders = ','.join(['?' for _ in columns])
                    
                    # Insert rows
                    for row in rows:
                        values = [row.get(col) for col in columns]
                        c.execute(f"INSERT OR REPLACE INTO {table} ({','.join(columns)}) VALUES ({placeholders})", values)
            
            conn.commit()
            conn.close()
            imported.append(db_name)
        except Exception as e:
            errors.append(f"{db_name}: {str(e)}")
    
    return {
        'success': len(errors) == 0,
        'imported': imported,
        'errors': errors
    }

# ── Backup Status ───────────────────────────────────────────────────────────

def get_backup_status() -> dict:
    """Get backup system status"""
    backups = list_backups()
    
    total_size = sum(b.get('size_mb', 0) for b in backups)
    latest = backups[0] if backups else None
    
    return {
        'total_backups': len(backups),
        'total_size_mb': round(total_size, 2),
        'latest_backup': latest,
        'backup_dir': str(BACKUP_DIR),
        'cloud_configured': {
            's3': bool(os.getenv('BACKUP_S3_BUCKET')),
            'gcs': bool(os.getenv('BACKUP_GCS_BUCKET'))
        }
    }