Overview
Location:services/backups.py
This module handles:
- On-demand database backups
- Automated daily backup scheduling
- Backup file management and cleanup
- Database restoration from backup files
- Backup listing and metadata
Configuration
Database file path from
data.db.session.DB_PATHMaximum number of backup files to retain
Timezone for backup timestamps
Backup files are stored in a
backups/ directory alongside the database file. The directory is automatically created if it doesn’t exist.Functions
create_backup_now
Creates an immediate backup of the database using SQLite’s backup API. Parameters: None Returns:Optional[Path] - Path to the created backup file, or None if backup failed
create_backup_if_needed
Creates a backup only if one hasn’t been created today. Parameters: None Returns:Optional[Path] - Path to the created backup file, or None if backup wasn’t needed or failed
This function checks the
last_backup_at timestamp in settings.json. If a backup already exists for the current day (Hermosillo timezone), it skips creating a new one.list_backups
Returns a list of all available backup files with metadata. Parameters: None Returns:List[Dict] - List of backup information dictionaries
Each dictionary contains:
path(Path): Full path to the backup filename(str): Filenamedisplay(str): Formatted timestamp (dd/mm/yyyy hh:mm)size(int): File size in bytes
Backups are sorted by modification time in descending order, so the most recent backup appears first.
restore_backup
Restores the database from a backup file.Path to the backup file to restore from
bool - True if restoration succeeded, False otherwise
get_last_backup_display
Returns a formatted string showing when the last backup was created. Parameters: None Returns:str - Formatted timestamp or ”—” if no backup exists
Backup Management
Automatic Cleanup
The service automatically maintains the backup directory:- Keeps only the most recent
MAX_BACKUPSfiles (default: 20) - Older backups are automatically deleted
- Cleanup runs after each new backup creation
Backup File Naming
Backup files follow this naming pattern:backup_20260304_153045.db- March 4, 2026 at 15:30:45backup_20260303_030000.db- March 3, 2026 at 03:00:00
Storage Location
Backups are stored in:/data/studio.db, backups will be in /data/backups/.
Backup Strategy
Automatic daily backups
Call
create_backup_if_needed() during application startup or via scheduled task.Manual backups before critical operations
Use
create_backup_now() before major data migrations or system changes.Verify backups regularly
Use
list_backups() to confirm backups are being created and stored properly.Best Practices
Backup frequency
Backup frequency
The default daily backup schedule is suitable for most tattoo studios. For high-volume studios, consider creating manual backups during peak hours or before closing.
Off-site backups
Off-site backups
The backup service stores files locally. Implement additional off-site backup mechanisms by copying the backup files to cloud storage or external drives.
Retention policy
Retention policy
The default 20 backup retention allows for approximately 3 weeks of daily backups. Adjust
MAX_BACKUPS based on your storage capacity and compliance requirements.Database size monitoring
Database size monitoring
Monitor the size of backup files using
list_backups(). Significant size increases may indicate data accumulation that needs attention.Error Handling
All functions handle errors gracefully:
create_backup_now()returnsNoneon failure and cleans up incomplete filesrestore_backup()creates a safety backup before attempting restoration- Failed backups don’t update the
last_backup_attimestamp - Database connections are properly closed even on exceptions
Related
Backup & Restore Guide
User guide for backup and restoration procedures
Database Session
Database connection and session management