Skip to content

Scheduled Restores

Restores can run on a Muppy CRON, typically to refresh a development or staging environment every night.

The restore Task

mpy_pg_restore, in the script muppy_postgresql_base.scripts.pg_dump, restores a pg_dump onto a PostgreSQL cluster.

Parameters that matter for automation

Parameter Description
source_database_obj Restore the latest dump of this database. Use it instead of pg_dump_obj, which pins one specific dump. The two are mutually exclusive.
force_restore Drop and recreate the target database if it already exists. Guarded — see below.
qualifier_obj Qualifier assigned to the restored database once the restore completes.
pg_cluster_obj Optional when the Host runs a single cluster. Required when it runs several.

source_database_obj is what makes the schedule work: the Task Run stays valid forever because it names a database, not a dump that gets purged.

Example: refresh a dev environment nightly

Goal — restore the latest production backup onto the development environment every night.

Create a Task Run on mpy_pg_restore with:

  • source_database_obj — the production database
  • db_name — the target database name, e.g. myapp_dev
  • force_restoreTrue, to overwrite yesterday's copy
  • qualifier_obj — the dev (or staging) qualifier

Then schedule it as described in Scheduled Backups: test with Launch as Job, click Create Cron, and set the recurrence (for example daily at 03:00).

Execution flow

mpy_pg_restore(source_database_obj=prod_db, force_restore=True, qualifier_obj=dev_qualifier)
├── Resolve the latest dump of prod_db
├── Check the target database is not 'prod'
├── Download the dump from S3
├── Close connections to the existing database
├── Drop and recreate the database
├── Restore the data
└── Assign the 'dev' qualifier

Guards against overwriting the wrong database

force_restore drops a database. Three rules constrain it:

  1. Production is protected. A database carrying a prod qualifier can never be overwritten.
  2. A qualifier is required. A database with no qualifier cannot be overwritten, unless you pass qualifier_obj — which states which environment you believe you are writing to.
  3. Restore cannot create production. qualifier_obj may not be a prod qualifier.

One-off restores

For a single restore that does not overwrite anything, use the restore wizard, or leave force_restore at its default.