Create backup archive and return path to created file
(
self,
include_patterns: List[str],
exclude_patterns: List[str],
include_hidden: bool = True,
backup_name: str = "agent-zero-backup"
)
| 334 | return matched_files |
| 335 | |
| 336 | async def create_backup( |
| 337 | self, |
| 338 | include_patterns: List[str], |
| 339 | exclude_patterns: List[str], |
| 340 | include_hidden: bool = True, |
| 341 | backup_name: str = "agent-zero-backup" |
| 342 | ) -> str: |
| 343 | """Create backup archive and return path to created file""" |
| 344 | |
| 345 | # Create metadata for test_patterns |
| 346 | metadata = { |
| 347 | "include_patterns": include_patterns, |
| 348 | "exclude_patterns": exclude_patterns, |
| 349 | "include_hidden": include_hidden |
| 350 | } |
| 351 | |
| 352 | # Get the complete matched file set. Preview and dry-run callers may |
| 353 | # cap their scans for UI responsiveness, but the archive itself must be |
| 354 | # complete. |
| 355 | matched_files = await self.test_patterns(metadata, max_files=None) |
| 356 | |
| 357 | if not matched_files: |
| 358 | raise Exception("No files matched the backup patterns") |
| 359 | |
| 360 | # Create temporary zip file |
| 361 | temp_dir = tempfile.mkdtemp() |
| 362 | zip_path = os.path.join(temp_dir, f"{backup_name}.zip") |
| 363 | |
| 364 | try: |
| 365 | with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: |
| 366 | # Add comprehensive metadata |
| 367 | metadata = { |
| 368 | # Basic backup information |
| 369 | "agent_zero_version": self.agent_zero_version, |
| 370 | "timestamp": Localization.get().now_iso(), |
| 371 | "backup_name": backup_name, |
| 372 | "include_hidden": include_hidden, |
| 373 | |
| 374 | # Pattern arrays for granular control during restore |
| 375 | "include_patterns": include_patterns, |
| 376 | "exclude_patterns": exclude_patterns, |
| 377 | |
| 378 | # System and environment information |
| 379 | "system_info": await self._get_system_info(), |
| 380 | "environment_info": await self._get_environment_info(), |
| 381 | "backup_author": await self._get_backup_author(), |
| 382 | |
| 383 | # Backup configuration |
| 384 | "backup_config": { |
| 385 | "include_patterns": include_patterns, |
| 386 | "exclude_patterns": exclude_patterns, |
| 387 | "include_hidden": include_hidden, |
| 388 | "compression_level": 6, |
| 389 | "integrity_check": True |
| 390 | }, |
| 391 | |
| 392 | # File information |
| 393 | "files": [ |