This commit is contained in:
2025-11-22 09:27:58 -08:00
parent 0c06324882
commit fec776b0bf
3 changed files with 95 additions and 21 deletions

View File

@@ -29,33 +29,50 @@ class ConsulRestore:
self.headers['X-Consul-Token'] = token
def find_backup_files(self, backup_dir: str) -> List[Path]:
"""Find all JSON backup files in the backup directory."""
"""Find all backup files in the backup directory."""
backup_path = Path(backup_dir)
if not backup_path.exists():
print(f"Backup directory not found: {backup_path}")
sys.exit(1)
# Find all JSON files (excluding metadata.json)
# Find all backup files (excluding metadata.json and directories)
backup_files = []
for file_path in backup_path.rglob("*.json"):
if file_path.name != "metadata.json":
for file_path in backup_path.rglob("*"):
if file_path.is_file() and file_path.name != "metadata.json":
backup_files.append(file_path)
return backup_files
def parse_backup_file(self, file_path: Path) -> Optional[Dict[str, Any]]:
def parse_backup_file(self, file_path: Path, backup_dir: str) -> Optional[Dict[str, Any]]:
"""Parse a backup file and extract KV data."""
try:
with open(file_path, 'r') as f:
data = json.load(f)
# Validate backup file structure
required_fields = ['key', 'value']
if not all(field in data for field in required_fields):
print(f"Invalid backup file format: {file_path}")
return None
return data
# Check if it's a JSON backup file
if file_path.suffix == '.json':
with open(file_path, 'r') as f:
data = json.load(f)
# Validate backup file structure
required_fields = ['key', 'value']
if not all(field in data for field in required_fields):
print(f"Invalid backup file format: {file_path}")
return None
return data
else:
# Handle binary files - read as binary and use filename as key
with open(file_path, 'rb') as f:
content = f.read()
# Convert path back to Consul key format
key = str(file_path.relative_to(Path(backup_dir)))
if os.path.sep != '/':
key = key.replace(os.path.sep, '/')
return {
'key': key,
'value': content,
'is_binary': True
}
except (IOError, json.JSONDecodeError) as e:
print(f"Error reading backup file {file_path}: {e}")
return None