@weirdscience/dbx
v0.2.6
Published
CLI tool for provisioning MongoDB instances on remote VPS infrastructure
Downloads
128
Maintainers
Readme
dbx
Self-hosted database provisioning CLI — Deploy MongoDB or PostgreSQL on any VPS with a single command. No Kubernetes, no cloud lock-in.
A CLI tool for provisioning database instances (MongoDB or PostgreSQL) on remote VPS infrastructure via SSH and Docker.
Overview
dbx automates the deployment and management of database instances on any VPS. It handles:
- Provisioning isolated MongoDB or PostgreSQL containers with auto-generated credentials
- Multi-environment support (dev, staging, production, etc.)
- Automatic port allocation to avoid conflicts
- Backup and restore operations
- State synchronization between local and remote
No Kubernetes. No cloud lock-in. Just SSH, Docker, and your database.
Installation
npm install -g @weirdscience/dbxRequirements:
- Node.js >= 18
- A VPS with Docker installed
- SSH key-based authentication to your VPS
Quick Start
# Initialize a new project
dbx init
# Provision a database instance
dbx up
# Get the connection URL
dbx url --show-passwordCommands
dbx init
Interactive project initialization. Creates dbx.config.json and .dbx/state.json.
dbx initdbx up [environment]
Provisions a database instance on your VPS (MongoDB or PostgreSQL based on config).
dbx up # Uses default environment from config
dbx up staging # Provisions a staging instance
dbx up -q # Quiet modedbx list
Lists all provisioned environments with connection details.
dbx listOutput:
ENV HOST PORT DB NAME ENGINE STATUS
------- ------------- ----- ---------- -------- -------
dev 192.168.1.100 27018 my-app_dev mongodb unknown
staging 192.168.1.100 27019 my-app_stg mongodb unknowndbx url [environment]
Retrieves the database connection URI.
dbx url # Masked password
dbx url --show-password # Full URI with password
dbx url staging # Specific environmentOutput (MongoDB):
mongodb://my-app_dev:***@192.168.1.100:27018/my-app_dev?authSource=adminOutput (PostgreSQL):
postgresql://dbx_dev:***@192.168.1.100:5433/my-app_devdbx logs [environment]
Streams MongoDB container logs.
dbx logs # Default environment
dbx logs staging # Specific environment
dbx logs -f # Follow mode (live tail)
dbx logs --tail 50 # Last 50 linesdbx backup [environment]
Creates a database backup on the VPS (uses mongodump for MongoDB, pg_dump for PostgreSQL).
dbx backup # Backup default environment
dbx backup staging # Backup stagingBackups are stored in /var/lib/dbx/backups/ on the VPS.
dbx restore <backup-file> [environment]
Restores a database backup to an instance (uses mongorestore for MongoDB, pg_restore for PostgreSQL).
dbx restore my-app_dev-2024-01-15T10-30-00.dump
dbx restore my-app_staging-2024-01-15.dump stagingdbx sync
Reconciles local state with remote VPS state. Remote is treated as source of truth.
dbx syncdbx destroy [environment]
Destroys a database instance and removes all associated resources.
dbx destroy # Destroy default environment
dbx destroy staging # Destroy staging
dbx destroy --purge # Also remove backup filesRequires confirmation by typing the environment name.
Configuration
dbx.config.json
Choose either mongodb or postgresql section (not both):
MongoDB Configuration
{
"project": "my-app",
"defaultEnv": "dev",
"vps": {
"host": "192.168.1.100",
"user": "ubuntu",
"sshKeyPath": "~/.ssh/id_ed25519",
"port": 22
},
"mongodb": {
"version": "7",
"basePort": 27018
}
}PostgreSQL Configuration
{
"project": "my-app",
"defaultEnv": "dev",
"vps": {
"host": "192.168.1.100",
"user": "ubuntu",
"sshKeyPath": "~/.ssh/id_ed25519",
"port": 22
},
"postgresql": {
"version": "16",
"basePort": 5433
}
}| Field | Description |
| --------------------- | ---------------------------------------------- |
| project | Project namespace for instances |
| defaultEnv | Default environment when not specified |
| vps.host | VPS hostname or IP address |
| vps.user | SSH username |
| vps.sshKeyPath | Path to SSH private key (supports ~) |
| vps.port | SSH port (default: 22) |
| mongodb.version | MongoDB version (e.g., "7", "6.0") |
| mongodb.basePort | Starting port for MongoDB (auto-increments) |
| postgresql.version | PostgreSQL version (e.g., "16", "15.2") |
| postgresql.basePort | Starting port for PostgreSQL (auto-increments) |
State Files
.dbx/state.json- Local state tracking provisioned instances/var/lib/dbx/state.json- Remote state on VPS (source of truth)
State files contain sensitive credentials and are created with 600 permissions.
How It Works
- SSH Connection - Connects to your VPS using key-based authentication
- Docker Provisioning - Creates a database container with:
- Unique container name:
dbx_<project>_<env> - Dedicated Docker volume for data persistence
- Auto-allocated port starting from
basePort - Auto-generated secure credentials
- Unique container name:
- State Management - Tracks instances locally and remotely for multi-machine workflows
- Backup/Restore - Uses native tools (
mongodump/mongorestoreorpg_dump/pg_restore)
Programmatic Usage
The library exports modules for programmatic use:
import { loadConfig, validateConfig } from "@weirdscience/dbx";
import { SSHClient } from "@weirdscience/dbx";
import { readState, getInstance } from "@weirdscience/dbx";
// Load and validate configuration
const config = await loadConfig();
// Read instance state
const instance = await getInstance("my-app", "dev");
// SSH operations
const ssh = new SSHClient({
host: config.vps.host,
port: config.vps.port || 22,
username: config.vps.user,
privateKeyPath: config.vps.sshKeyPath,
});
await ssh.connect();
const result = await ssh.exec("docker ps");
ssh.disconnect();VPS Setup
Your VPS needs:
Docker installed and running
curl -fsSL https://get.docker.com | shSSH key authentication configured
ssh-copy-id user@your-vpsUser with Docker permissions
sudo usermod -aG docker $USER
dbx will automatically create required directories on first use.
Security
- Credentials are auto-generated with cryptographically secure random values
- State files use restrictive permissions (
600for files,700for directories) - SSH key-based authentication only (no password support)
- MongoDB instances are configured with authentication enabled
Note: Credentials are stored in plaintext in state files. Ensure your VPS and local machine are properly secured.
License
MIT
