Files
claudetools/api/models/backup_log.py
Mike Swanson 390b10b32c Complete Phase 6: MSP Work Tracking with Context Recall System
Implements production-ready MSP platform with cross-machine persistent memory for Claude.

API Implementation:
- 130 REST API endpoints across 21 entities
- JWT authentication on all endpoints
- AES-256-GCM encryption for credentials
- Automatic audit logging
- Complete OpenAPI documentation

Database:
- 43 tables in MariaDB (172.16.3.20:3306)
- 42 SQLAlchemy models with modern 2.0 syntax
- Full Alembic migration system
- 99.1% CRUD test pass rate

Context Recall System (Phase 6):
- Cross-machine persistent memory via database
- Automatic context injection via Claude Code hooks
- Automatic context saving after task completion
- 90-95% token reduction with compression utilities
- Relevance scoring with time decay
- Tag-based semantic search
- One-command setup script

Security Features:
- JWT tokens with Argon2 password hashing
- AES-256-GCM encryption for all sensitive data
- Comprehensive audit trail for credentials
- HMAC tamper detection
- Secure configuration management

Test Results:
- Phase 3: 38/38 CRUD tests passing (100%)
- Phase 4: 34/35 core API tests passing (97.1%)
- Phase 5: 62/62 extended API tests passing (100%)
- Phase 6: 10/10 compression tests passing (100%)
- Overall: 144/145 tests passing (99.3%)

Documentation:
- Comprehensive architecture guides
- Setup automation scripts
- API documentation at /api/docs
- Complete test reports
- Troubleshooting guides

Project Status: 95% Complete (Production-Ready)
Phase 7 (optional work context APIs) remains for future enhancement.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-17 06:00:26 -07:00

148 lines
4.6 KiB
Python

"""
Backup Log model for tracking ClaudeTools database backups.
This model logs all backup operations with verification status,
ensuring the ClaudeTools database can be reliably restored if needed.
"""
from datetime import datetime
from typing import Optional
from sqlalchemy import (
BigInteger,
CheckConstraint,
Index,
Integer,
String,
Text,
)
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.sql import func
from .base import Base, UUIDMixin
class BackupLog(Base, UUIDMixin):
"""
Backup tracking for ClaudeTools database.
Logs all backup operations including timing, file details, and verification
status. Ensures database can be restored with confidence.
Attributes:
id: Unique identifier
backup_type: Type of backup (daily, weekly, monthly, manual, pre-migration)
file_path: Path to the backup file
file_size_bytes: Size of the backup file in bytes
backup_started_at: When the backup started
backup_completed_at: When the backup completed
duration_seconds: Computed duration of backup operation
verification_status: Status of backup verification (passed, failed, not_verified)
verification_details: JSON with specific verification check results
database_host: Host where database is located
database_name: Name of the database backed up
backup_method: Method used for backup (mysqldump, etc.)
created_at: Timestamp when log entry was created
"""
__tablename__ = "backup_log"
# Backup details
backup_type: Mapped[str] = mapped_column(
String(50),
CheckConstraint(
"backup_type IN ('daily', 'weekly', 'monthly', 'manual', 'pre-migration')"
),
nullable=False,
doc="Type of backup performed",
)
file_path: Mapped[str] = mapped_column(
String(500),
nullable=False,
doc="Path to the backup file",
)
file_size_bytes: Mapped[int] = mapped_column(
BigInteger,
nullable=False,
doc="Size of backup file in bytes",
)
# Timing
backup_started_at: Mapped[datetime] = mapped_column(
nullable=False,
doc="When the backup started",
)
backup_completed_at: Mapped[datetime] = mapped_column(
nullable=False,
doc="When the backup completed",
)
# Note: SQLAlchemy doesn't support TIMESTAMPDIFF directly, so we'll calculate in Python
# The duration will be computed by the application layer rather than as a stored generated column
duration_seconds: Mapped[Optional[int]] = mapped_column(
Integer,
nullable=True,
doc="Duration of backup in seconds (computed in application)",
)
# Verification
verification_status: Mapped[Optional[str]] = mapped_column(
String(50),
CheckConstraint(
"verification_status IN ('passed', 'failed', 'not_verified')"
),
nullable=True,
doc="Verification status of the backup",
)
verification_details: Mapped[Optional[str]] = mapped_column(
Text,
nullable=True,
doc="JSON with specific verification check results",
)
# Metadata
database_host: Mapped[Optional[str]] = mapped_column(
String(255),
nullable=True,
doc="Host where database is located",
)
database_name: Mapped[Optional[str]] = mapped_column(
String(100),
nullable=True,
doc="Name of the database backed up",
)
backup_method: Mapped[str] = mapped_column(
String(50),
default="mysqldump",
nullable=False,
doc="Method used for backup",
)
created_at: Mapped[datetime] = mapped_column(
nullable=False,
server_default=func.now(),
doc="When log entry was created",
)
# Indexes
__table_args__ = (
Index("idx_backup_type", "backup_type"),
Index("idx_backup_date", "backup_completed_at"),
Index("idx_verification_status", "verification_status"),
)
def calculate_duration(self) -> None:
"""Calculate and set the duration_seconds field."""
if self.backup_started_at and self.backup_completed_at:
delta = self.backup_completed_at - self.backup_started_at
self.duration_seconds = int(delta.total_seconds())
def __repr__(self) -> str:
"""String representation of the backup log."""
return (
f"<BackupLog(id={self.id!r}, "
f"type={self.backup_type!r}, "
f"size={self.file_size_bytes}, "
f"status={self.verification_status!r})>"
)