Backup & Restore
Overview
Section titled “Overview”HaruDB provides comprehensive backup and restore capabilities:
- Full Database Backup: Complete database snapshots with metadata
- Point-in-time Recovery: Restore from any backup
- Backup Information: Detailed backup metadata and statistics
- Backup Management: List and manage backup files
- Compressed Storage: Efficient tar.gz backup format
Backup Commands
Section titled “Backup Commands”BACKUP TO
Section titled “BACKUP TO”Create a full database backup to a specified location.
-- Basic backupBACKUP TO ./backup.db
-- Backup with descriptionBACKUP TO ./backup.db DESCRIPTION "Daily backup"
-- Backup to specific directoryBACKUP TO ./backups/weekly_backup.db DESCRIPTION "Weekly backup"
-- Backup with timestampBACKUP TO ./backups/backup_2024-01-15.db DESCRIPTION "Backup from 2024-01-15"
Notes:
- Only admin users can create backups
- Backup files are compressed tar.gz format
- Includes all tables, indexes, and metadata
- Backup path must be writable
LIST BACKUPS
Section titled “LIST BACKUPS”Display all available backups in a directory.
-- List backups in current directoryLIST BACKUPS
-- List backups in specific directoryLIST BACKUPS ./backups
-- List backups with detailsLIST BACKUPS ./backups DETAILED
Output example:
Backups in ./backups:- backup_2024-01-15.db (2024-01-15 10:30:00, Size: 2.5MB, Tables: 5)- backup_2024-01-14.db (2024-01-14 10:30:00, Size: 2.3MB, Tables: 5)- backup_2024-01-13.db (2024-01-13 10:30:00, Size: 2.1MB, Tables: 4)
BACKUP INFO
Section titled “BACKUP INFO”Get detailed information about a specific backup.
-- Get backup informationBACKUP INFO ./backups/backup_2024-01-15.db
-- Get backup information with statisticsBACKUP INFO ./backups/backup_2024-01-15.db STATS
Output example:
Backup Information:File: ./backups/backup_2024-01-15.dbCreated: 2024-01-15 10:30:00Size: 2.5MBDescription: Daily backupTables: 5- users (100 rows)- products (50 rows)- orders (200 rows)- customers (75 rows)- inventory (25 rows)Indexes: 8WAL Entries: 150
Restore Commands
Section titled “Restore Commands”RESTORE FROM
Section titled “RESTORE FROM”Restore database from a backup file.
-- Restore from backupRESTORE FROM ./backups/backup_2024-01-15.db
-- Restore with confirmationRESTORE FROM ./backups/backup_2024-01-15.db CONFIRM
-- Restore to specific stateRESTORE FROM ./backups/backup_2024-01-15.db TO_STATE "clean"
Notes:
- Only admin users can restore backups
- Current database will be completely replaced
- All existing data will be lost
- Backup file must be readable and valid
Complete Backup Example
Section titled “Complete Backup Example”-- Login as adminLOGIN admin admin123
-- Create sample dataCREATE TABLE users (id, name, email, created_at);CREATE TABLE products (id, name, price, category);CREATE TABLE orders (id, user_id, product_id, quantity, total);
-- Insert sample dataINSERT INTO users VALUES (1, 'Alice', 'alice@example.com', '2024-01-15');INSERT INTO users VALUES (2, 'Bob', 'bob@example.com', '2024-01-15');INSERT INTO products VALUES (1, 'Laptop', '999.99', 'Electronics');INSERT INTO products VALUES (2, 'Mouse', '29.99', 'Electronics');INSERT INTO orders VALUES (1, 1, 1, 1, '999.99');INSERT INTO orders VALUES (2, 2, 2, 2, '59.98');
-- Create indexesCREATE INDEX ON users (email);CREATE INDEX ON products (category);CREATE INDEX ON orders (user_id);
-- Create backupBACKUP TO ./backups/daily_backup_2024-01-15.db DESCRIPTION "Daily backup with sample data"
-- List backupsLIST BACKUPS ./backups
-- Get backup informationBACKUP INFO ./backups/daily_backup_2024-01-15.db
-- Add more dataINSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com', '2024-01-15');INSERT INTO products VALUES (3, 'Keyboard', '79.99', 'Electronics');
-- Create another backupBACKUP TO ./backups/evening_backup_2024-01-15.db DESCRIPTION "Evening backup with additional data"
-- List all backupsLIST BACKUPS ./backups
Restore Example
Section titled “Restore Example”-- Login as adminLOGIN admin admin123
-- Check current dataSELECT * FROM users;SELECT * FROM products;
-- List available backupsLIST BACKUPS ./backups
-- Restore from earlier backupRESTORE FROM ./backups/daily_backup_2024-01-15.db
-- Verify restoreSELECT * FROM users;SELECT * FROM products;
-- Check that newer data is goneSELECT * FROM users WHERE name = 'Charlie';-- Should return empty (Charlie was added after the backup)
Backup Strategies
Section titled “Backup Strategies”Daily Backups
Section titled “Daily Backups”-- Create daily backup script-- Run this daily at 2 AMLOGIN admin admin123BACKUP TO ./backups/daily_$(date +%Y-%m-%d).db DESCRIPTION "Daily backup $(date)"LOGOUT
Weekly Backups
Section titled “Weekly Backups”-- Create weekly backup script-- Run this weekly on Sunday at 3 AMLOGIN admin admin123BACKUP TO ./backups/weekly_$(date +%Y-%m-%d).db DESCRIPTION "Weekly backup $(date)"LOGOUT
Monthly Backups
Section titled “Monthly Backups”-- Create monthly backup script-- Run this monthly on the 1st at 4 AMLOGIN admin admin123BACKUP TO ./backups/monthly_$(date +%Y-%m).db DESCRIPTION "Monthly backup $(date)"LOGOUT
Automated Backup Scripts
Section titled “Automated Backup Scripts”Bash Script Example
Section titled “Bash Script Example”#!/bin/bashBACKUP_DIR="./backups"DATE=$(date +%Y-%m-%d_%H-%M-%S)BACKUP_FILE="$BACKUP_DIR/backup_$DATE.db"
# Create backup directory if it doesn't existmkdir -p "$BACKUP_DIR"
# Create backupecho "Creating backup: $BACKUP_FILE"haru-cli << EOFLOGIN admin admin123BACKUP TO $BACKUP_FILE DESCRIPTION "Automated backup $DATE"LOGOUTEOF
# Check if backup was successfulif [ $? -eq 0 ]; then echo "Backup completed successfully: $BACKUP_FILE"else echo "Backup failed!" exit 1fi
# Clean up old backups (keep last 30 days)find "$BACKUP_DIR" -name "backup_*.db" -mtime +30 -deleteecho "Old backups cleaned up"
Cron Job Setup
Section titled “Cron Job Setup”# Add to crontab for daily backups at 2 AM0 2 * * * /path/to/backup_script.sh
# Add to crontab for weekly backups on Sunday at 3 AM0 3 * * 0 /path/to/backup_script.sh
# Add to crontab for monthly backups on 1st at 4 AM0 4 1 * * /path/to/backup_script.sh
Backup Management
Section titled “Backup Management”Backup Rotation
Section titled “Backup Rotation”-- List all backupsLIST BACKUPS ./backups
-- Get backup sizesBACKUP INFO ./backups/backup_2024-01-15.db STATSBACKUP INFO ./backups/backup_2024-01-14.db STATSBACKUP INFO ./backups/backup_2024-01-13.db STATS
-- Manual cleanup (delete old backups)-- Note: HaruDB doesn't have a DELETE BACKUP command-- Use system commands to remove old backup files
Backup Verification
Section titled “Backup Verification”-- Verify backup integrityBACKUP INFO ./backups/backup_2024-01-15.db STATS
-- Test restore to temporary location-- (This would require restoring to a test database)
Disaster Recovery
Section titled “Disaster Recovery”Complete Database Recovery
Section titled “Complete Database Recovery”-- In case of complete database loss-- 1. Start fresh HaruDB instance./harudb --data-dir ./data
-- 2. Login as adminLOGIN admin admin123
-- 3. Restore from latest backupRESTORE FROM ./backups/latest_backup.db
-- 4. Verify data integritySELECT COUNT(*) FROM users;SELECT COUNT(*) FROM products;SELECT COUNT(*) FROM orders;
Point-in-time Recovery
Section titled “Point-in-time Recovery”-- Restore to specific point in time-- 1. List available backupsLIST BACKUPS ./backups
-- 2. Choose backup closest to desired timeBACKUP INFO ./backups/backup_2024-01-15.db
-- 3. Restore from that backupRESTORE FROM ./backups/backup_2024-01-15.db
-- 4. Verify restored stateSELECT * FROM users;SELECT * FROM products;
Best Practices
Section titled “Best Practices”Backup Frequency
Section titled “Backup Frequency”- Production databases: Daily backups minimum
- Development databases: Weekly backups
- Test databases: Before major changes
Backup Storage
Section titled “Backup Storage”- Local storage: Keep recent backups locally
- Remote storage: Store backups off-site
- Multiple locations: Use different storage systems
Backup Testing
Section titled “Backup Testing”- Regular restore tests: Verify backups work
- Disaster recovery drills: Practice recovery procedures
- Backup integrity checks: Validate backup files
Security
Section titled “Security”- Encrypt backups: Use encryption for sensitive data
- Access control: Limit backup file access
- Secure storage: Store backups securely
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Backup failed:
-- Check permissions-- Ensure backup directory is writable-- Verify admin user has backup permissions
Restore failed:
-- Check backup file exists-- Verify backup file is not corrupted-- Ensure admin user has restore permissions
Backup file not found:
-- Check file path-- Verify backup directory exists-- List available backupsLIST BACKUPS ./backups
Error Messages
Section titled “Error Messages”- “Backup failed”: Insufficient permissions or disk space
- “Restore failed”: Backup file corrupted or not found
- “Invalid backup file”: Backup file format is invalid
- “Permission denied”: User doesn’t have admin privileges
For more information about HaruDB’s data integrity features, see the Data Integrity Guide and WAL documentation.