Learn how to backup and restore your nself database with automatic versioning, point-in-time recovery, and safe rollback procedures.
nself provides comprehensive backup and restore capabilities to protect your data and enable safe database operations. Every migration automatically creates a backup, and you can create manual backups at any time.
Backups are automatically created during:
nself db update
nself db run
Backups are stored in bin/dbsyncs/
with timestamps:
bin/dbsyncs/
├── 2025-08-06_14-30-00/
│ ├── backup.sql
│ └── metadata.json
└── 2025-08-06_15-45-30/
├── backup.sql
└── metadata.json
Create a manual backup at any time:
# Create a timestamped backup
nself db backup
# Backup with custom name
nself db backup --name before-major-change
Quickly revert to the most recent backup:
# Restore to last backup
nself db revert
# This is useful after a failed migration or incorrect data change
Restore from a specific backup file:
# List available backups
ls bin/dbsyncs/
# Restore from specific backup
nself db restore bin/dbsyncs/2025-08-06_14-30-00/backup.sql
Check your database and backup status:
# View database status
nself db status
# Example output:
# Database Status
# Schema file: schema.dbml
# Hash: b4dc5d616f59...
# Migrations: 1
# Latest migrations:
# - 1_init
# Backups: 3
# PostgreSQL: Running
For production environments:
# Set up automated daily backups via cron
0 2 * * * cd /path/to/project && nself db backup --name "daily-$(date +%Y%m%d)"
# Keep only last 30 backups
0 3 * * * find bin/dbsyncs -type d -mtime +30 -exec rm -rf {} +
# Fix permissions
chmod 755 bin/dbsyncs
chown -R $(whoami) bin/dbsyncs
# Ensure PostgreSQL is running
nself up postgres
# Wait for PostgreSQL to be ready
sleep 5
# Retry restore
nself db revert