AI Coding: How to Protect Your Database Before a Migration

How to Restore Your Database After AI-Generated Code Goes Wrong

You used AI to write code that changes your database. Before running that migration or releasing the app, you need a backup, a way to test recovery, and a clear path back if the change goes wrong.

This guide explains how to back up and restore your database from the CLI when working with AI-generated code. It covers PostgreSQL, MongoDB, MySQL, and Redis through one NEXUS AI command workflow.

With NEXUS AI, the commands stay familiar whether your app uses PostgreSQL, MongoDB, MySQL, or Redis:

nexus db services
nexus db backup <service-id>
nexus db backups <service-id>
nexus db restore <service-id> <backup-id>

You choose the database service and the backup. NEXUS AI selects the database's native backup and restore tools and uses the service's stored connection settings. Your laptop needs the NEXUS AI CLI; the engine-specific tools run on the platform.

This guide shows how that simplifies release preparation and recovery, using a worked example of an orders application. The commands are real CLI commands. The application names describe an example you can adapt, rather than a reported customer incident.

Which databases this guide covers

Use this workflow for database services running alongside a deployment on NEXUS AI managed infrastructure, such as services added with --services postgres, mongodb, mysql, or redis on the docker provider.

Standalone databases and managed cloud database instances have a separate snapshot workflow under nexus managed-db. An external database connection does not automatically become a deployment-service backup target. See the database backup overview.

Before starting, you need:

  • Node.js 18 or later and a NEXUS AI account with access to the deployment.
  • A running database service visible to your current organization.
  • For the recovery rehearsal, a separate test deployment with a database of the same engine and a compatible version.

Why developers only need one command workflow

Without a shared interface, each engine introduces its own utility, connection options, credentials, backup format, and restore procedure. NEXUS AI puts those mechanics behind the database service ID.

Database What NEXUS AI uses behind the scenes Your backup command
PostgreSQL pg_dump custom-format backup nexus db backup <service-id>
MongoDB mongodump directory packaged into a compressed backup nexus db backup <service-id>
MySQL mysqldump SQL backup nexus db backup <service-id>
Redis RDB snapshot nexus db backup <service-id>

Listing, scheduling, and downloading also use the same commands. Restore uses the same CLI syntax across engines, with each engine's own data replacement behavior underneath.

You still decide what to back up, where to restore it, and whether the recovered application is correct. The CLI removes the need to assemble a different backup command for each database.

Scenario: protect an orders app before a release

Assume you maintain these two deployments:

Deployment Purpose
orders-api Production application and its PostgreSQL service
orders-recovery Isolated recovery-test application and a compatible PostgreSQL service in the same organization

Your coding AI has proposed a migration that changes how order statuses are stored. Before running that generated code, you want to save the current data, review the migration, and prove that you can restore the backup into the recovery environment.

Keep the recovery app disconnected from live payments, email delivery, and production job processing. A restored copy contains the data represented by the backup. Use an environment approved for that data, and sanitize it before broader development use.

Step 1: Install the CLI and confirm your account

npm install -g nexusapp-cli@latest
nexus --version
nexus auth login
nexus auth whoami

Confirm that the reported account and organization are the ones that own your deployments. You do not enter individual database passwords for the backup commands.

Step 2: Find the database service IDs

List deployments and their database services:

nexus deploy list
nexus db services orders-api
nexus db services orders-recovery

The service table includes SERVICE ID, TYPE, DEPLOYMENT, STATUS, AUTO-BACKUP, and RETENTION.

Copy the PostgreSQL service ID from each deployment. A service ID identifies the database service; it is different from the deployment ID.

For the commands below, set these shell variables after replacing both example values with your actual service IDs:

PROD_SERVICE_ID='REPLACE_WITH_PRODUCTION_DATABASE_SERVICE_ID'
RECOVERY_SERVICE_ID='REPLACE_WITH_RECOVERY_DATABASE_SERVICE_ID'

The variables are ordinary Bash or Zsh variables used to keep the examples readable. They are not special NEXUS AI settings.

Step 3: Take a backup before the change

nexus db backup "$PROD_SERVICE_ID"
nexus db backups "$PROD_SERVICE_ID"

The first command creates the backup and prints its ID, engine type, size, and creation time. The second lists the service's backups, including their status.

Identify the backup you just created and check that its status is completed. Save that specific ID:

PRE_RELEASE_BACKUP_ID='REPLACE_WITH_COMPLETED_BACKUP_ID'

Keep the ID in your release notes with the service and release you are protecting. A backup taken before the change and a backup taken after a failure serve different purposes.

If you need a precise application-wide checkpoint, coordinate writes while taking it. Backing up several services one after another does not produce an atomic snapshot across all of them.

Step 4: Download a copy if you need separate retention

nexus db backup-download "$PROD_SERVICE_ID" "$PRE_RELEASE_BACKUP_ID"

The CLI saves the backup locally using its original filename. For a custom PostgreSQL backup path:

nexus db backup-download "$PROD_SERVICE_ID" "$PRE_RELEASE_BACKUP_ID" \
  --out ./backups/orders-before-status-change.dump

The default-filename command works across all four engines and keeps the engine's appropriate file extension. Store downloaded files in your approved backup location, outside your source repository.

Downloading creates a local copy. It does not test that the backup can be restored.

Step 5: Rehearse recovery in the separate database

If the recovery database contains data you may need, back it up first:

nexus db backup "$RECOVERY_SERVICE_ID"

Then restore the production backup into that recovery database:

nexus db restore-to "$RECOVERY_SERVICE_ID" "$PRE_RELEASE_BACKUP_ID"

Read the confirmation prompt and confirm the target. restore-to writes into the selected target service. The source database is not the restore target.

The source backup and target service must belong to the same organization and use the same database engine. Use a compatible engine version as well. This command does not convert a PostgreSQL backup into MySQL or another engine.

Check the recovery app:

nexus deploy status orders-recovery
nexus deploy logs orders-recovery --type runtime --lines 100

Open its URL and verify the application behavior. For this orders example:

  1. Open several known orders and confirm their customer, total, and status.
  2. Confirm the expected orders appear in the relevant filtered lists.
  3. Perform a controlled test write with external side effects disabled.
  4. Refresh the page and confirm the test change persists.

Database restore success confirms the restore operation finished. These application checks confirm the recovered data works with the app version you intend to run.

Checkpoint: you have a completed pre-release backup and a verified PostgreSQL recovery copy. You can now test the status migration in recovery before releasing it to production.

Step 6: Recover production if the release needs a data rollback

Suppose the migration changes existing statuses incorrectly. First stop application writes and any workers that could modify the affected data, using your application's maintenance controls. Prepare the application version compatible with the old schema and data before reopening traffic.

Do not assume that stopping an entire deployment leaves its database available. Backup and restore need a running database container.

Preserve the current production state before replacing it:

nexus db backup "$PROD_SERVICE_ID"
nexus db backups "$PROD_SERVICE_ID"

Record this new backup ID separately. It preserves the post-change state for investigation or later reconciliation.

Then restore the pre-release backup into its original service:

nexus db restore "$PROD_SERVICE_ID" "$PRE_RELEASE_BACKUP_ID"

Confirm the prompt only after checking the service and backup IDs. In-place restore can overwrite or remove current data. Changes made after the chosen backup may need reconciliation, including orders accepted between that backup and maintenance mode.

Restore the compatible application release through your normal release procedure, check logs, and repeat the order checks before resuming writes. A database restore does not roll back application code, and a code rollback does not restore database data.

The NEXUS AI CLI reference documents the same-service restore and separate-target restore-to commands.

Apply the same commands to MongoDB, MySQL, and Redis

MongoDB restore caveat: source verification for this draft identified a path mismatch when restoring platform-created MongoDB backups. Resolve and test that restore path before relying on it for MongoDB recovery. The CLI command names below are supported, but this guide does not claim a successful live restore test for every engine. The worked recovery example above uses PostgreSQL.

Redis backup caveat: the current implementation waits a fixed interval after requesting a background save, rather than checking that the save completed. Verify snapshot freshness and recovery on a representative dataset before relying on that backup path.

For a different engine, run nexus db services and copy that database's service ID. The core sequence stays the same:

SERVICE_ID='REPLACE_WITH_DATABASE_SERVICE_ID'

nexus db backup "$SERVICE_ID"
nexus db backups "$SERVICE_ID"

Copy a completed backup ID from the output:

BACKUP_ID='REPLACE_WITH_COMPLETED_BACKUP_ID'

nexus db backup-download "$SERVICE_ID" "$BACKUP_ID"

When you intend to restore that service, after coordinating writes and preserving any current state you need:

nexus db restore "$SERVICE_ID" "$BACKUP_ID"

You do not add a database-engine flag to these commands. NEXUS AI resolves the engine from the service.

What still differs between engines

Engine What to account for during restore
PostgreSQL Restore resets the target's public schema before loading the backup. Objects and data can be removed.
MongoDB Restore uses collection drop behavior for collections being restored. Check the recovered documents through the app.
MySQL Restore imports SQL into the target database. Objects not addressed by that dump can remain.
Redis Restore replaces the RDB file and stops and starts the Redis container. Allow for interruption and reconnects.

The shared interface does not make every restore an identical byte-for-byte replacement of the whole service. Keep recovery targets compatible, and test the data your application depends on.

For Redis used as a job queue, restoring an earlier dataset can bring back jobs that already ran. Coordinate workers and account for duplicate side effects. If Redis only stores disposable cache data, decide whether recovery should rebuild that cache instead.

Enable daily backups with the same command

After the release, enable a daily backup policy for the chosen service:

nexus db backup-schedule "$SERVICE_ID" --enable --retention 14
nexus db services

Check that the service reports automatic backups as on and retention as 14d. Enable the policy separately for each database service that needs it.

Scheduled backups use a daily interval, with a default retention of seven days and a minimum of seven days. The command above raises retention to fourteen days. It does not set a particular wall-clock execution time.

While the schedule is enabled, the retention window applies to NEXUS AI-created backups, including on-demand backups. Download release checkpoints that you need to retain beyond that window. Uploaded backups are excluded from that automatic rotation.

These are scheduled and on-demand recovery points, rather than continuous point-in-time recovery. Take an on-demand backup before a risky change even when daily backups are enabled. See the backup schedule and retention documentation.

To disable future scheduled backups:

nexus db backup-schedule "$SERVICE_ID" --disable

Other useful commands

Inspect backups as JSON

nexus db services --json
nexus db backups "$SERVICE_ID" --json
nexus db backup "$SERVICE_ID" --json

The last command creates a new backup and returns its record as JSON. It is not a read-only inspection command.

Generate a temporary download link

nexus db backup-download "$SERVICE_ID" "$BACKUP_ID" --share --ttl 900

This creates a link lasting fifteen minutes. Anyone with the link can download the file until it expires. The supported lifetime is 30 to 3,600 seconds.

Skip restore confirmation in a reviewed automation job

nexus db restore "$SERVICE_ID" "$BACKUP_ID" --yes

--yes skips the interactive confirmation. It does not verify that you chose the intended data. Use it only when your automation has already established the service, backup, and maintenance conditions.

Troubleshooting

Problem What to check
A command or flag is missing Update nexusapp-cli, then run nexus db --help.
No database services appear Confirm the organization with nexus auth whoami. Check whether the database is a deployment service or a standalone managed database.
Service has no running container The database must be running for backup and restore. Restore database availability before retrying.
Backup not found Check the organization, source service, and backup ID. In-place restore needs a backup belonging to that service.
Engine mismatch during restore-to Choose a target service of the same engine as the backup.
A downloaded file is missing later Download again while the platform backup still exists. The local download location and platform retention are separate.
Restore finishes but the app fails Check the app's connection settings, schema compatibility, and runtime logs. A restore does not change the deployed code version.

Quick command reference

The angle-bracket values below are placeholders, not literal shell input.

nexus db services [deployment-name-or-id]
nexus db backup <service-id>
nexus db backups <service-id>
nexus db backup-download <service-id> <backup-id>
nexus db restore <service-id> <backup-id>
nexus db restore-to <target-service-id> <backup-id>
nexus db backup-schedule <service-id> --enable --retention 14

For developers moving between Python, Node.js, SQL databases, document storage, and Redis, this is the practical benefit: one backup workflow to learn and reuse. Find the service, create the backup, retain the recovery point, and rehearse the restore through your application.