# Backup & Restore Workflow {#backup--restore-workflow}


Step-by-step backup and recovery procedures.


## Scheduled Backups {#scheduled-backups}



### Daily Automated Backup {#daily-automated-backup}


```bash
#!/bin/bash
# backup.sh - run via cron daily at 2 AM
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backups/gauzy"

# PostgreSQL backup
pg_dump -h $DB_HOST -U $DB_USER -d gauzy \
  -F custom -Z 9 \
  -f "$BACKUP_DIR/gauzy_$DATE.dump"

# Upload to S3
aws s3 cp "$BACKUP_DIR/gauzy_$DATE.dump" \
  s3://your-backup-bucket/gauzy/

# Cleanup local (keep 7 days)
find $BACKUP_DIR -name "*.dump" -mtime +7 -delete

echo "Backup completed: gauzy_$DATE.dump"
```


### Cron Setup {#cron-setup}


```bash
# crontab -e
0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
```


## Manual Backup {#manual-backup}


```bash
# Full database dump
pg_dump -h localhost -U postgres -d gauzy -F custom -f gauzy_manual.dump

# Schema only
pg_dump -h localhost -U postgres -d gauzy --schema-only -f schema.sql

# Data only
pg_dump -h localhost -U postgres -d gauzy --data-only -f data.sql
```


## Restore Procedures {#restore-procedures}



### Full Restore {#full-restore}


```bash
# Create fresh database
createdb -h localhost -U postgres gauzy_restored

# Restore from dump
pg_restore -h localhost -U postgres -d gauzy_restored gauzy_backup.dump

# Verify
psql -h localhost -U postgres -d gauzy_restored \
  -c "SELECT count(*) FROM employee;"
```


### Point-in-Time Recovery {#point-in-time-recovery}


Requires WAL archiving to be enabled:

```bash
# Restore to specific timestamp
recovery_target_time = '2025-03-05 15:00:00'
```


## Verification {#verification}


| Check            | Command                       |
| ---------------- | ----------------------------- |
| Record counts    | Compare key table counts      |
| Recent data      | Verify latest timestamps      |
| Relationships    | Check FK integrity            |
| Application test | Start API against restored DB |


## Related Pages {#related-pages}


- [Database Backup Strategies](https://sourcey-gauzy.pages.dev/devops/database-backup/index.md) — backup methods
- [Disaster Recovery](https://sourcey-gauzy.pages.dev/devops/disaster-recovery/index.md) — DR planning
- [Database Schema](https://sourcey-gauzy.pages.dev/database/schema-overview/index.md) — schema
