Project backup and restore
Project backup and restore turns a project and its linked automation resources into portable JSON. Export keeps names and relationships that can be rebuilt in another project, while restore checks the file, creates a new project, reconnects those relationships, and reports items that need user action. This page complements Project configuration and content, Persistence and domain models, and Repository and automation content.
Portable project format
The portable project format is a versioned JSON document that represents project configuration rather than database identity. It includes project metadata and the resources needed to run automation, such as repositories, host mappings, credentials, inventories, environments, templates, schedules, secret stores, roles, runners, integrations, and workflows. Project-local IDs are not the portable contract; links are carried mainly by resource names so a target system can assign new IDs. (services/project/types.go#L45-L115, services/project/types.go#L117-L230)
The format does not make a project self-contained in every sense. Credential values and runner authentication data stay out of the file, while task-only secret keys and MCP server tokens are excluded. A restored credential is therefore an empty placeholder, while a restored runner is unregistered until it receives new authentication data. (services/project/backup.go#L167-L181, services/project/restore.go#L672-L685, services/project/backup_mcp_test.go#L14-L52, services/project/backup_marshal_test.go#L40-L64, services/project/backup_test.go#L1104-L1170)
References: services/…/types.go• L45-115 services/…/backup.go• L167-181 services/…/restore.go• L672-685 services/…/backup_mcp_test.go• L14-52 services/…/backup_marshal_test.go• L40-64 services/…/backup_test.go• L1104-1170
Resource references
Resource references preserve the relationships that make a project usable after IDs change. The export converts links such as a template's repository, inventory, environment, view, vault key, or build template into names. Restore resolves those names against resources it has already created in the new project. (services/project/backup.go#L281-L445, services/project/restore.go#L366-L517)
This name-based model also covers schedules, integrations, workflow nodes, host mappings, and secret storage links. It requires names to be unique within each resource group, so export adds random suffixes to duplicate names before writing the file. (services/project/backup.go#L53-L137, services/project/backup.go#L290-L335, services/project/restore.go#L146-L215, services/project/restore.go#L638-L725)
The restore order follows these dependencies. Each stage creates the resources needed by the next stage, then name-based links are changed to IDs in the new project.
flowchart TD
A[Project metadata] --> B[Secret stores and roles]
B --> C[Environments and views]
C --> D[Credentials]
D --> E[Repositories and host mappings]
E --> F[Inventories]
F --> G[Non-deploy templates]
G --> H[Deploy templates]
H --> I[Runners]
I --> J[Workflows]
J --> K[Schedules and integrations]
References: services/…/backup.go• L53-137 services/…/backup.go• L281-553 services/…/restore.go• L146-215 services/…/restore.go• L366-725 services/…/restore.go• L859-999
Serialization and compatibility
Serialization uses backup-specific field tags to define the JSON contract. Fields marked for exclusion do not enter the file, while database field names provide the default JSON keys for portable values. Special values such as survey defaults keep their string or list shape through the round trip. (services/project/backup_marshal.go#L11-L35, services/project/backup_marshal.go#L37-L125)
Deserialization accepts a JSON object and fills the versioned backup structure without requiring every field to be present. This lets older files omit newer optional objects, but type errors and invalid shapes stop the restore before resource creation. (services/project/backup.go#L592-L617, services/project/backup_marshal.go#L201-L280, services/project/backup_test.go#L234-L338)
Format versions
The format has an explicit version. The current format is version 4. Older files without roles remain readable, while files that use old role or grant permission masks are adjusted during restore. Files newer than the supported version are rejected because the server cannot know how to interpret their fields. (services/project/types.go#L45-L47, services/project/restore.go#L778-L789, services/project/restore.go#L77-L87, services/project/restore.go#L542-L561, services/project/restore.go#L749-L768, services/project/backup_test.go#L1073-L1102, services/project/backup_test.go#L1212-L1305)
Compatibility is limited to known changes in the format. A file that uses roles or grants but has no version, or a pre-version-2 file that contains those newer structures, must be exported again by a compatible Semaphore version. (services/project/restore.go#L778-L789)
References: services/…/backup.go• L592-617 services/…/backup_marshal.go• L11-35 services/…/backup_marshal.go• L201-280 services/…/types.go• L45-47 services/…/restore.go• L77-87 services/…/restore.go• L542-561 services/…/restore.go• L749-789 services/…/backup_test.go• L1073-1102
Export interfaces
Export exposes the same portable format through the project API and the command line. Both paths load the project, include workflow data through the workflow store when available, serialize it as indented JSON, and leave the result as a file or response for the operator to keep. (services/project/backup.go#L584-L606, api/projects/backup_restore.go#L106-L133, cli/cmd/project_export.go#L49-L98)
The API export is scoped to the project in the request context and records an audit event. The command line export selects one project by ID or case-insensitive name and writes to standard output unless an output file is given. (api/projects/backup_restore.go#L106-L133, api/router.go#L397-L400, cli/cmd/project_export.go#L21-L25, cli/cmd/project_export.go#L33-L75)
References: services/…/backup.go• L584-606 api/…/backup_restore.go• L106-133 api/router.go• L397-400 cli/…/project_export.go• L21-25 cli/…/project_export.go• L28-98
Restore flow
Restore is a create-new-project operation. It reads the JSON, checks its shape and relationships, applies access and feature gates, creates the project with the restoring user as owner, and then rebuilds dependent resources in order. A project with the same name is refused. (api/projects/backup_restore.go#L135-L182, services/project/restore.go#L859-L892)
The operation is all-or-cleanup: if a later resource fails, the system tries to delete the new project. If cleanup also fails, the result identifies that the project remains and needs manual removal. (services/project/restore.go#L883-L890, api/projects/backup_restore.go#L182-L197)
The restore process follows a structured sequence:
- Parse the JSON into the portable format.
- Validate versions, duplicates, references, permissions, and resource-specific rules.
- Check access and plan features for workflow integrations, workflow schedules, and workflow grants.
- Create the project and assign the restoring user as its owner.
- Create resources in dependency order and replace portable names with new project IDs. Deploy templates are restored after the templates they use as build templates.
- Return the restored project and any role grants that could not be assigned.
References: api/…/backup_restore.go• L135-226 services/…/restore.go• L778-856 services/…/restore.go• L859-999
Credential and role handling
Restore carries credential definitions without their secret values. The resulting empty keys are visible to the user so they can enter the missing values before running automation. Runners also lose their token and must be registered again. (services/project/restore.go#L672-L685, services/project/backup_test.go#L1104-L1170, web/src/App.vue#L1730-L1743, web/src/components/RestoreProjectResult.vue#L12-L22)
Role grants can remain unresolved when a role is not present in the file or when a non-built-in shared role uses the same slug. Restore still creates the project and returns these grants with their affected templates or workflows. The web result view lets the user map each unresolved role to an existing role or create a new one, then applies the saved grants. (services/project/types.go#L62-L95, services/project/restore.go#L77-L113, services/project/restore.go#L492-L515, services/project/restore.go#L570-L581, services/project/restore.go#L699-L718, web/src/components/RestoreProjectResult.vue#L24-L109)
References: services/…/types.go• L62-95 services/…/restore.go• L77-113 services/…/restore.go• L492-515 services/…/restore.go• L570-581 services/…/restore.go• L672-685 services/…/restore.go• L699-718 web/…/App.vue• L1730-1743 web/…/RestoreProjectResult.vue• L12-22 web/…/RestoreProjectResult.vue• L24-109
CLI batch import
The command line importer restores one file or all likely backup files in a directory. Directory entries are sorted before processing, so repeated imports have a stable order. Each file is parsed and verified before restore, and a failed file does not stop later files from being attempted. (cli/cmd/project_import.go#L69-L119, cli/cmd/project_import.go#L147-L164)
The importer selects an existing administrator as the restore owner, or the first user when no administrator exists. A single-file import can override the project name; directory imports keep the names in their files. Unassigned role grants are printed for later action in the UI. (cli/cmd/project_import.go#L50-L67, cli/cmd/project_import.go#L123-L145, cli/cmd/project_import.go#L159-L187)
References: cli/…/project_import.go• L19-31 cli/…/project_import.go• L34-119 cli/…/project_import.go• L123-187
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
BackupFormatVersion, the version checks, and permission upgrades aligned when role or grant semantics change. - Keep every exported name reference and its restore lookup aligned when a project resource changes its relationship to another resource.
- Keep restore order aligned with new dependencies so all referenced resources exist before their links are rebuilt.
- Keep host mapping validation aligned with the normal host mapping rules because imported mappings are checked before they are saved.
- Keep excluded credential, runner, and MCP token fields excluded when new secret-bearing fields are added to persisted models.
- Keep API feature gates aligned with the workflow integration, workflow schedule, and workflow grant data that a backup can contain.
- Keep the restore result contract aligned with the web role-assignment flow when new unresolved restore cases are added.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Backup refers to a missing resource name | Verification rejects the file before project creation | Validate all portable references in BackupFormat.Verify |
| Backup version is newer than the supported format | Restore stops because field meaning is unknown | Compare the file version with BackupFormatVersion |
| Credential values are expected to travel in the file | Restored keys are empty and automation cannot authenticate | Re-enter secret values after restore |
| A runner is expected to work after restore | The runner has no token and is not registered | Register the runner again |
| An MCP server token is expected to travel with the project | The server token is not restored as a project key | Keep the token with its MCP server and provide it again after restore |
| A role slug is already owned by a shared role | The project is restored without that role grant | Review and assign the reported unassigned role |
| An imported host mapping fails normal validation | Restore stops before that mapping is saved | Use valid host and credential mappings |
| Resource creation fails after the project is created | The system tries to delete the partial project | Investigate a CleanupError and remove the project manually if cleanup failed |
| Workflow integrations, workflow schedules, or workflow grants lack the required plan feature | Restore is refused before any resource is created | Restore with the needed feature enabled or remove those entries |
Key files
| File | Why to read it |
|---|---|
| services/…/types.go | Defines the portable format, version, relationship wrappers, and restore result. |
| services/…/backup.go | Shows how project resources are collected, named, linked, and serialized. |
| services/…/backup_marshal.go | Defines the field-selection and type-shape rules for JSON. |
| services/…/backup_mcp_test.go | Confirms that MCP server tokens do not enter the project backup. |
| services/…/restore.go | Shows validation, compatibility handling, dependency order, cleanup, host mapping checks, and unresolved roles. |
| api/…/backup_restore.go | Defines the API export and restore boundaries, access checks, feature gates, and audit records. |
| cli/…/project_export.go | Shows the command line export entry point. |
| cli/…/project_import.go | Shows single-file and directory import behavior. |
| web/…/RestoreProjectResult.vue | Shows how users resolve role grants left unassigned by restore. |
| web/…/App.vue | Shows how the web client reports empty restored keys and opens the restore result view. |