From 4546aec6bfd0cc9a9462238807d6f53bb15c8c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Gierwia=C5=82o?= Date: Tue, 20 Aug 2024 16:10:00 +0200 Subject: [PATCH] Add dry-run functionality and complete backup workflow - Comprehensive dry-run mode with detailed preview - Complete backup process orchestration - Service restart in finally block for reliability - Archive preview showing what would be backed up - Detailed configuration summary in dry-run mode --- docker2pbs.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/docker2pbs.py b/docker2pbs.py index 7f4fd17..7fdaa12 100644 --- a/docker2pbs.py +++ b/docker2pbs.py @@ -281,6 +281,75 @@ class Docker2PBS: os.remove(paths_file) elif self.dry_run: print(f"[DRY-RUN] Would remove temporary file: {paths_file}") + + def show_dry_run_summary(self) -> None: + """Show summary of what would be done in dry-run mode""" + print("\n=== DRY-RUN SUMMARY ===") + print(f"Service: {self.service_name}") + print(f"Compose file: {self.compose_file}") + print(f"Volumes found: {len(self.volumes)}") + print(f"Networks found: {len(self.networks)}") + + print("\nVolumes configuration:") + for i, volume in enumerate(self.volumes, 1): + vol_type = volume.get('type', 'bind') + source = volume.get('source', 'N/A') + target = volume.get('target', 'N/A') + external = " (external)" if volume.get('external') else "" + print(f" {i}. {vol_type}: {source} -> {target}{external}") + + print("\nNetworks configuration:") + for i, network in enumerate(self.networks, 1): + name = network.get('name', 'N/A') + external = " (external)" if network.get('external') else "" + print(f" {i}. {name}{external}") + + print(f"\nPBS Repository: {self.pbs_config['repository']}") + print("\n=== END SUMMARY ===") + + def run_backup(self) -> None: + """Run the complete backup process""" + try: + mode_str = "[DRY-RUN] " if self.dry_run else "" + print(f"{mode_str}Starting backup process for service: {self.service_name}") + + # Load and parse compose file + self.load_compose_file() + + # Parse configuration + self.volumes = self.parse_volumes() + self.networks = self.parse_networks() + + print(f"Found {len(self.volumes)} volumes and {len(self.networks)} networks") + + if self.dry_run: + self.show_dry_run_summary() + print("\n=== ARCHIVE PREVIEW ===") + volume_backups = self.prepare_volume_backups() + for vb in volume_backups: + print(f" {vb['archive_name']}: {vb['source_path']} ({vb['volume_type']})") + print("=== END PREVIEW ===") + + # Stop service + self.stop_service() + + # Create backup + self.create_pbs_backup() + + mode_str = "[DRY-RUN] " if self.dry_run else "" + print(f"{mode_str}Backup process completed successfully") + + except Exception as e: + mode_str = "[DRY-RUN] " if self.dry_run else "" + print(f"{mode_str}Backup process failed: {e}") + raise + finally: + # Always try to restart the service + try: + self.start_service() + except Exception as e: + mode_str = "[DRY-RUN] " if self.dry_run else "" + print(f"{mode_str}Warning: Failed to restart service: {e}") def main(): @@ -308,8 +377,7 @@ def main(): try: backup_tool = Docker2PBS(args.compose_file, args.service_name, pbs_config, args.dry_run) - backup_tool.load_compose_file() - print(f"Successfully loaded compose file and found service: {args.service_name}") + backup_tool.run_backup() except Exception as e: print(f"Error: {e}") sys.exit(1)