SQLite provides multiple backup and recovery methods:
-
File Copy Backup
- Simplest backup method: directly copy the database file
- Need to ensure no write operations are in progress
- Suitable for small databases or offline backups
bashcp database.db database_backup.db -
Online Backup API
- Use SQLite's backup API for online backup
- No need to stop database service
- Supports incremental backup
sql-- Command line tool sqlite3 source.db ".backup backup.db" -
VACUUM INTO Backup
- Use VACUUM INTO command to create a database copy
- Simultaneously performs database optimization and defragmentation
sqlVACUUM INTO 'backup.db'; -
Export SQL Script
- Use
.dumpcommand to export as SQL script - Can selectively export specific tables or data
bashsqlite3 database.db ".dump" > backup.sql - Use
-
Recovery Methods
- Recover from file backup: directly copy backup file
- Recover from SQL script: execute exported SQL script
bashsqlite3 database.db < backup.sql -
Incremental Backup
- When using WAL mode, can backup WAL files
- Perform full backup after regular checkpoints
- Combine with WAL files for point-in-time recovery
-
Backup Best Practices
- Regularly perform automatic backups
- Keep multiple historical backup versions
- Verify backup file integrity
- Test recovery process to ensure availability
- Consider encrypting sensitive data
-
Cross-Platform Backup
- SQLite database files are cross-platform
- Can copy database files between different operating systems
- Pay attention to file paths and permission issues
Choosing the appropriate backup strategy depends on the importance of the application, frequency of data changes, and recovery time requirements.