# Common administrative tasks

> This section provides detailed instructions for how to perform common tasks using the Uniform CLI.

Source: https://docs.uniform.app/docs/guides/cli/common-tasks

Many of the kinds of administrative tasks you perform using the Uniform dashboard or the CLI can also be done programmatically using the Uniform APIs.

## Backup and restore

### Why backup and restore?

A common operational need is to keep multiple deployment
environments in sync with each other. For example:

| Purpose | Example |
| --- | --- |
| Separate by user | - Developer 1 environment<br>- Developer 2 environment |
| Separate by purpose | - QA environment<br>- Production environment |

For Uniform, this is done using serialization of project
assets, such as Uniform Canvas components or compositions
and Uniform Context signals and enrichments, to files on
disk. These files can then be committed to source control
and shared between environments, or used to scaffold
templates and demos.

### Which project assets can you backup & restore?

#### [Sync command](https://docs.uniform.app/docs/guides/cli/commands/sync)

There is a single command to backup & restore all project assets:

```shell
uniform sync pull --config ./uniform.config.ts
```

#### Sync specific project assets

The Uniform CLI also provides the ability to granularly backup and restore the following project assets:

| Category | Type |
| --- | --- |
| Canvas | [Assets](https://docs.uniform.app/docs/guides/cli/commands/canvas#asset-commands) |
| Canvas | [Components](https://docs.uniform.app/docs/guides/cli/commands/canvas#component-commands) |
| Canvas | [Compositions](https://docs.uniform.app/docs/guides/cli/commands/canvas#composition-commands) |
| Canvas | [Component patterns](https://docs.uniform.app/docs/guides/cli/commands/canvas#component-pattern-commands) |
| Canvas | [Composition patterns](https://docs.uniform.app/docs/guides/cli/commands/canvas#composition-pattern-commands) |
| Canvas | [Categories](https://docs.uniform.app/docs/guides/cli/commands/canvas#category-commands) |
| Canvas | [Content types](https://docs.uniform.app/docs/guides/cli/commands/canvas#content-type-commands) |
| Canvas | [Entries](https://docs.uniform.app/docs/guides/cli/commands/canvas#entry-commands) |
| Canvas | [Entry patterns](https://docs.uniform.app/docs/guides/cli/commands/canvas#entry-pattern-commands) |
| Canvas | [Data sources](https://docs.uniform.app/docs/guides/cli/commands/canvas#data-source-commands) |
| Canvas | [Data types](https://docs.uniform.app/docs/guides/cli/commands/canvas#datatype-commands) |
| Canvas | [Labels](https://docs.uniform.app/docs/guides/cli/commands/canvas#label-commands) |
| Canvas | [Locales](https://docs.uniform.app/docs/guides/cli/commands/canvas#locale-commands) |
| Canvas | [Preview URLs](https://docs.uniform.app/docs/guides/cli/commands/canvas#preview-url-commands) |
| Canvas | [Preview viewport](https://docs.uniform.app/docs/guides/cli/commands/canvas#preview-viewport-commands) |
| Canvas | [Workflow](https://docs.uniform.app/docs/guides/cli/commands/canvas#workflow-commands) |
| Context | [Audiences & intents](https://docs.uniform.app/docs/guides/cli/commands/context#aggregates) |
| Context | [Enrichments](https://docs.uniform.app/docs/guides/cli/commands/context#enrichments) |
| Context | [Quirks](https://docs.uniform.app/docs/guides/cli/commands/context#quirks) |
| Context | [Signals](https://docs.uniform.app/docs/guides/cli/commands/context#signals) |
| Context | [Tests](https://docs.uniform.app/docs/guides/cli/commands/context#tests) |
| Integration | [Definitions](https://docs.uniform.app/docs/guides/cli/commands/integration) |
| Project map | [Definitions](https://docs.uniform.app/docs/guides/cli/commands/project-map#project-map-commands) |
| Project map | [Nodes](https://docs.uniform.app/docs/guides/cli/commands/project-map#project-map-node-commands) |
| Policy documents | [Definitions](https://docs.uniform.app/docs/guides/cli/commands/policy-documents) |
| Redirect | [Definitions](https://docs.uniform.app/docs/guides/cli/commands/redirect) |
| Webhook | [Definitions](https://docs.uniform.app/docs/guides/cli/commands/webhook) |

> **Tip:**
>
> Data sources cannot be synced because they generally contain secrets that should not be written to files.
>
> Data sources may be [directly created using the CLI](https://docs.uniform.app/docs/guides/cli/commands/canvas#data-source-commands), for the purposes of scaffolding them if they do not contain secrets.

### Automate backup & restore

This section describes how to add backup and restore scripts
to a Node project. Your CI/CD process can trigger the script to automate backup & restore.

> **Prerequisites:**
>
> These instructions assume you already have a Node project and a Uniform project.

1. Make sure the following dependencies are included in your Node project. If you need to add them, you can add them as developer dependencies:

   ```
   @uniformdev/cli 
   ```
2. Make sure you have a `.env` file with the [values needed](https://docs.uniform.app/docs/guides/cli/installation-and-setup#environment-variables) to connect to your Uniform project.

   ```properties
   UNIFORM_API_KEY=<YOUR API KEY>
   UNIFORM_PROJECT_ID=<YOUR PROJECT ID>
   ```
3. Add Configuration file `uniform.config.ts` to the root of your project (you can find configuration schema [here](https://docs.uniform.app/docs/guides/cli/commands/sync#configuration)):

   `uniform.config.ts`

   ```typescript
   import type { CLIConfiguration } from '@uniformdev/cli';

   require('dotenv').config();

   const config: CLIConfiguration = {
     serialization: {
       format: "yaml",
       mode: "mirror",
       directory: "./uniform-data",
       entitiesConfig: {
         asset: {},
         composition: {
           push: {
             // May be useful to only create new compositions and not update existing ones to avoid accidental overrides
             mode: 'create'
           }
         },
         component: {},
         componentPattern: {},
         category: {},
         contentType: {},
         entry: {},         
         entryPattern: {},
         dataType: {},
         signal: {},
         test: {},
         aggregate: {},
         enrichment: {},
         locale: {},
         quirk: {},
        projectMapDefinition: {},
        projectMapNode: {},
        redirect: {},
        workflow: {},
       }
     }
   };

   module.exports = config;
   ```
4. Add the following scripts to your `package.json` file:

   `package.json`

   ```json
   {
     "name": "my-package",
     "version": "0.1.0",
     "private": true,
     "scripts": {
       "pull": "uniform sync pull",
       "push": "uniform sync push"
     },
     "devDependencies": {
       "@uniformdev/cli": "latest"
     }
   }
   ```
5. To backup the assets in your Uniform project, run the following command.

   `npm`

   ```shell
   npm run pull
   ```

   `pnpm`

   ```shell
   pnpm run pull
   ```

   `yarn`

   ```shell
   yarn pull
   ```

   > **Note:**
   >
   > A file for each project asset is created in the sub folders under the folder `data`.
6. To restore the assets to your Uniform project, run the following command:

   `npm`

   ```shell
   npm run push
   ```

   `pnpm`

   ```shell
   pnpm run push
   ```

   `yarn`

   ```shell
   yarn push
   ```

   > **Note:**
   >
   > Project assets stored in the sub folders under the folder `data` are restored to your Uniform project.

> **Tip:**
>
> Another useful technique with pushing and pulling is to perform
> bulk updates. Since the serialized files are plain YAML (or JSON),
> you can use your favorite tools such as `jq` to modify them before
> pushing them back into Uniform.

### Backup assets to a single file

For CI/CD environments it's best to produce a single
artifact instead of many serialized files to define the state of
Uniform. To support this, the Uniform CLI allows you to use the
`pull` and `push` commands directly to a package file instead of
multiple files on disk.

To use this feature, specify a filename instead of an output directory
in configuration `filename` property:

`uniform.config.js`

```js
module.exports = {
  serialization: {
    entitiesConfig: {
      composition: {},
      component: {
        directory: './uniform-data/components.yaml'
      }
    }
  }
}
```

To add multiple types of product assets to a single file, change global `directory` property to filename with `yaml` or `json` extension:

`uniform.config.js`

```js
module.exports = {
  serialization: {
    entitiesConfig: {
      composition: {},
      component: {}
    },
    directory: './uniform-data.json'
  }
}
```

## Download the manifest

[Learn more about working with the with the manifest](https://docs.uniform.app/docs/guides/classification/manifest).
