# Database Backups Directory

Backup and dump files for database management and recovery.

## Files in This Directory

### SQL Backups
- `backup-19-10.sql` - Database backup from October 19
- `dumpdata.sql` - SQL dump of database structure and data

### JSON Data Dumps
- `datadump.json` - Django data dump in JSON format
- `update_data.json` - Data update script/fixture
- `backup-data19-10` - Data backup directory

---

## Backup & Restore Procedures

### Creating a Backup

#### Method 1: Django Dump (JSON)
```bash
python manage.py dumpdata > backups/datadump-$(date +%Y-%m-%d).json
```

#### Method 2: SQLite Dump
```bash
sqlite3 db.sqlite3 .dump > backups/backup-$(date +%Y-%m-%d).sql
```

#### Method 3: Full Database Backup
```bash
# Linux/macOS
sqlite3 db.sqlite3 ".backup backups/backup-$(date +%Y-%m-%d).db"

# Windows PowerShell
$date = Get-Date -Format "yyyy-MM-dd"
sqlite3 db.sqlite3 ".backup backups/backup-$date.db"
```

### Restoring a Backup

#### From Django JSON Dump
```bash
# Load data back into database
python manage.py loaddata backups/datadump.json
```

#### From SQL File
```bash
# Restore SQLite from SQL dump
sqlite3 db.sqlite3 < backups/backup-19-10.sql
```

#### From Database File
```bash
# Replace current database with backup
cp backups/backup-2024-10-19.db db.sqlite3
```

---

## Backup Strategy

### When to Backup
- Before major code changes
- Before deploying to production
- Before running migrations
- Before bulk data operations
- Regularly (daily/weekly depending on usage)

### Backup Naming Convention
```
backup-YYYY-MM-DD.{sql|json|db}
```

Example: `backup-2024-04-21.sql`

### Retention Policy
- Keep last 7 daily backups
- Keep last 4 weekly backups
- Keep monthly backups for 1 year
- Delete old backups to save space

---

## Automated Backups (Linux/macOS)

### Cron Job for Daily Backups
```bash
# Edit crontab
crontab -e

# Add this line for daily backup at 2 AM
0 2 * * * cd /path/to/website-bmc && sqlite3 db.sqlite3 ".backup backups/backup-$(date +\%Y-\%m-\%d).db"
```

### Cron Job for Weekly JSON Dump
```bash
# Every Sunday at 3 AM
0 3 * * 0 cd /path/to/website-bmc && python manage.py dumpdata > backups/dump-$(date +\%Y-\%m-\%d).json
```

---

## Important Notes

### ⚠️ Before Restoring
1. **Always backup current database first** (in case something goes wrong)
2. Stop the Django application
3. Verify backup file integrity
4. Test restore in development first

### 🔒 Security
- **Never commit** backup files to git
- Store backups in secure location (encrypted drive/cloud)
- Restrict file permissions: `chmod 600 backup-*.db`
- Keep `.gitignore` updated to ignore backup files

### 💾 Storage
- SQL files are smaller but database-specific
- JSON dumps are portable across database systems
- Database files (.db) are exact copies, largest size
- Consider gzip compression: `gzip backup-*.sql`

---

## Common Scenarios

### Complete Data Loss
```bash
# 1. Stop Django
# 2. Restore latest backup
sqlite3 db.sqlite3 < backups/backup-latest.sql
# 3. Verify data
python manage.py dbshell "SELECT COUNT(*) FROM BmcBase_sanpham;"
# 4. Restart Django
python manage.py runserver
```

### Need Specific Table
```bash
# Extract specific table from SQL dump
grep "CREATE TABLE sanpham" backups/backup-19-10.sql
grep "INSERT INTO sanpham" backups/backup-19-10.sql > /tmp/table_sanpham.sql
```

### Version Control for Data
```bash
# Store JSON dump in git for version control (not recommended for large data)
git add backups/datadump.json
git commit -m "Data snapshot: product catalog backup"
```

---

## Tools

### SQLite Command Line
```bash
# Open database
sqlite3 db.sqlite3

# List tables
.tables

# Schema for specific table
.schema sanpham

# Row count
SELECT COUNT(*) FROM sanpham;

# Export to CSV
.mode csv
.output products.csv
SELECT * FROM BmcBase_sanpham;
.output stdout
```

### Django Management Commands
```bash
# Dump specific app
python manage.py dumpdata BmcBase > backups/bmc_base_dump.json

# Dump specific model
python manage.py dumpdata BmcBase.SanPham > backups/products_dump.json

# Load data
python manage.py loaddata backups/bmc_base_dump.json

# Clear database
python manage.py flush --noinput
```

---

## Troubleshooting

### "Database is locked" error
```bash
# SQLite file is locked, probably Django is still running
# Stop Django first
# Then try backup again
```

### Corrupted backup file
```bash
# Verify SQLite database integrity
sqlite3 backup-19-10.sql "PRAGMA integrity_check;"
```

### Import errors after restore
```bash
# Migration history might be out of sync
python manage.py migrate --fake-initial
# Then reload data
python manage.py loaddata backup.json
```

---

## Related Documentation

- [Deployment Guide](../docs/DEPLOYMENT_GUIDE.md) - Production backup strategies
- [Developer Guide](../docs/DEVELOPER_GUIDE.md) - Development workflows
- Django Documentation: [Data Migrations](https://docs.djangoproject.com/en/stable/topics/migrations/)
