Backup & Restore
Complete guide for backing up and restoring your SaleSpider database.
Overview
SaleSpider uses pgBackRest for reliable PostgreSQL backups with:
- Full and incremental backups
- Point-in-time recovery
- Automated scheduling
- Backup verification
INFO
This guide applies to self-hosted deployments. For hosted databases (Neon, Supabase, etc.), use your provider's backup tools.
Quick Start
Create Backup
# Using Make
make backup
# Using Docker Compose
docker-compose exec postgres pgbackrest backup --stanza=main --type=fullRestore Backup
# Restore latest backup
make restore
# Restore to specific time
make restore-pitr TIME="2024-11-20 14:30:00"Backup Operations
Manual Backup
Create a backup on demand:
# Full backup
make backup
# View backup info
make backup-infoAutomated Backups
Backups run automatically when configured:
# In .env
SETUP_BACKUP="true"
BACKUP_FULL_SCHEDULE="0 2 * * 0" # Weekly
BACKUP_INCR_SCHEDULE="0 2 * * 1-6" # DailyBackup Types
Full Backup
- Complete database copy
- Required for restore
- Run weekly
Incremental Backup
- Only changes since last backup
- Faster and smaller
- Run daily
Differential Backup
- Changes since last full backup
- Middle ground option
Restore Operations
Full Restore
Restore complete database:
# Stop application
docker-compose stop app
# Restore database
make restore
# Start application
docker-compose start appPoint-in-Time Recovery
Restore to specific timestamp:
# Restore to exact time
docker-compose exec postgres pgbackrest restore \
--stanza=main \
--type=time \
--target="2024-11-20 14:30:00" \
--deltaSelective Restore
Restore specific database:
# Restore single database
docker-compose exec postgres pgbackrest restore \
--stanza=main \
--db-include=salespiderBackup Management
List Backups
View available backups:
# Show backup information
make backup-info
# Detailed backup list
docker-compose exec postgres pgbackrest info --stanza=mainVerify Backups
Check backup integrity:
# Verify backup
docker-compose exec postgres pgbackrest check --stanza=mainDelete Old Backups
Remove expired backups:
# Expire based on retention policy
docker-compose exec postgres pgbackrest expire --stanza=mainBackup Storage
Local Storage
Default backup location:
/var/lib/pgbackrest/
├── archive/
└── backup/Cloud Storage
Configure S3 or Azure storage:
# S3 Configuration
PGBACKREST_REPO1_TYPE="s3"
PGBACKREST_REPO1_S3_BUCKET="my-backups"
PGBACKREST_REPO1_S3_REGION="us-east-1"External Storage
Mount external drive:
# Mount backup drive
mount /dev/sdb1 /mnt/backups
# Update backup path
PGBACKREST_REPO1_PATH="/mnt/backups/pgbackrest"Disaster Recovery
Recovery Steps
Assess situation
- Identify what needs recovery
- Determine recovery point
Prepare for restore
bashdocker-compose stop appRestore database
bashmake restoreVerify data
bashdocker-compose exec postgres psql -U postgres -d salespider -c "\dt"Restart services
bashdocker-compose start app
Recovery Scenarios
Complete Data Loss
- Restore from latest full backup
- Apply incremental backups
- Verify data integrity
Accidental Deletion
- Use point-in-time recovery
- Restore to before deletion
- Verify specific data
Corruption
- Restore from last known good backup
- Check backup integrity first
- Test in staging if possible
Monitoring
Backup Status
Check backup health:
# View backup logs
docker-compose logs backup
# Check last backup
docker-compose exec postgres pgbackrest infoBackup Logs
Log locations:
/var/log/pgbackrest/
├── main-backup.log
├── main-archive.log
└── main-restore.logAlerts
Monitor for:
- Backup failures
- Storage space issues
- Backup age warnings
Best Practices
Regular Testing
- Test restore monthly
- Verify backup integrity weekly
- Document restore procedures
- Train team on recovery
Backup Schedule
- Full backup: Weekly
- Incremental: Daily
- Test restore: Monthly
- Off-site copy: Weekly
Storage Management
- Monitor disk space
- Set retention policies
- Clean old backups
- Use compression
Security
- Encrypt backups
- Secure storage location
- Restrict access
- Rotate encryption keys
Troubleshooting
Backup Fails
Check logs:
docker-compose logs backup
cat /var/log/pgbackrest/main-backup.logCommon issues:
- Disk space full
- Permission denied
- Database connection failed
- Log file permission errors
Solutions:
- Free up disk space
- Check file permissions
- Verify database is running
- Restart backup service:
docker-compose restart backup
Permission Denied on Log Files:
If you see permission errors, restart the backup service:
# Restart the backup service
docker-compose restart backup
# Verify logs are working
docker-compose logs -f backupRestore Fails
Verify backup:
docker-compose exec postgres pgbackrest checkCommon issues:
- Backup corrupted
- Insufficient space
- Wrong backup selected
Solutions:
- Use different backup
- Free up space
- Check backup integrity
Slow Backups
Optimize:
- Use compression
- Incremental backups
- Faster storage
- Network optimization
Advanced Topics
Parallel Backup
Speed up backups:
# Use multiple processes
PGBACKREST_PROCESS_MAX=4Backup Encryption
Encrypt backups:
PGBACKREST_REPO1_CIPHER_TYPE="aes-256-cbc"
PGBACKREST_REPO1_CIPHER_PASS="secure-passphrase"Archive Management
Configure WAL archiving:
# Archive retention
PGBACKREST_RETENTION_ARCHIVE=2