> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/troubleshooting-and-resources/resources/new-user/backup-harness-in-git.md).

# Back up Harness deployments in Git using Git Experience and Terraform

Backup your Harness CD configurations to Git using the Harness Git Experience and Harness Terraform Provider.

Harness Continuous Delivery (CD) integrates with modern DevOps tools, notably Git and Terraform. This topic describes how to back up your Harness CD configurations to GitHub using the Harness Git Experience and Harness Terraform Provider.

## What you will learn from this topic

* How to set up [Harness Git Experience](#harness-git-experience-setup) to back up pipelines, templates, input sets, and policies.
* How to set up the [Harness Terraform Provider](#harness-terraform-provider-setup) to manage and back up other Harness resources.

## Before you begin

* [Harness Git Experience overview](https://developer.harness.io/harness-platform/use-harness-platform/git-experience/git-experience-overview)
* [Onboard with Terraform Provider](https://developer.harness.io/harness-platform/troubleshooting-and-resources/tutorials/onboard-terraform-provider)

## Harness Git Experience setup <a href="#harness-git-experience-setup" id="harness-git-experience-setup"></a>

Harness Git Experience allows you to treat your CD configurations as code stored in a Git repository. When integrated with GitHub, every change made in the Harness platform can be version-controlled in a GitHub repository. In addition, with bidirectional sync enabled, you can have Git Experience sync two-way (bidirectionally) between Harness and your Git repo.

Harness Git Experience supports the following key entities:

* Pipeline
* Templates
* Input Sets
* Governance Policies

To set up Harness Git Experience for backups, do the following:

1. **Create a GitHub repository:** If you have not already, set up a dedicated repository in GitHub for your Harness configurations.
2. **Connect Harness with GitHub:** Follow the steps in the [Harness Git Experience quickstart](https://developer.harness.io/harness-platform/use-harness-platform/git-experience/configure-git-experience-for-harness-entities).
3. **Sync configurations with GitHub:**
   1. In Harness, enable Git Experience for your entities.
   2. Specify the GitHub repository, branch, and file paths for synchronization.

By navigating to your account/project settings, you can enforce that all Harness project pipelines, templates, and input sets are backed up in GitHub.

![picture 0](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-754d02b2a78ca2203bfe987f010ccd16c6ce1b94%2Fb761578856e805346345effb06037ada18ed5c778465126ba728f3e6b5f27707.png?alt=media)

You can organize the repo to look like this:

```
├── Pipeline
│   ├── dev_pipeline.yaml
│   ├── qa_pipeline.yaml
│   ├── prod_pipeline.yaml
├── Templates
│   ├── Step_Templates
│   │   ├── bash_job_step.yaml
│   ├── Stage_Templates
|   |   |-- deploy_stage.yaml
│   ├── Pipeline_Templates
│   ├── Step_Group_Templates
├── Input Sets
|   |- Prod_Inputs.yaml
└── .gitignore
```

## Harness Terraform Provider setup <a href="#harness-terraform-provider-setup" id="harness-terraform-provider-setup"></a>

The Harness Terraform Provider takes backup further by allowing you to define and manage Harness resources using Terraform. This enables an Infrastructure as Code (IaC) approach.

You can manage the other Harness resources like connectors, secrets, and projects via the Terraform Provider.

Using this approach requires you to manage all resources and onboarding via Terraform. This is to ensure that the Terraform state file is the same as the Harness state.

To set up the Harness Terraform Provider for backups, do the following:

1. **Install the Harness Terraform Provider:** Add the Harness provider to your Terraform configuration. For more information, go to the [Terraform Provider docs](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_pipeline).
2. **Define Harness resources:** Write Terraform configurations that describe your Harness resources. For instance, you can define a pipeline in Harness as shown below.

**Example: Terraform provider configuration**

```json
terraform {
  required_providers {
    harness = {
      source = "harness/harness"
      version = "0.28.2"
    }
  }
}

provider "harness" {
  # Configuration options
}
```

**Example: Harness pipeline resource**

```json
resource "harness_platform_pipeline" "example" {
  identifier = "identifier"
  org_id     = "orgIdentifier"
  project_id = "projectIdentifier"
  name       = "name"
  git_details {
    branch_name    = "branchName"
    commit_message = "commitMessage"
    file_path      = "filePath"
    connector_ref  = "connectorRef"
    store_type     = "REMOTE"
    repo_name      = "repoName"
  }

  yaml = <<-EOT
      pipeline:
          name: name
          identifier: identifier
          allowStageExecutions: false
          projectIdentifier: projectIdentifier
          orgIdentifier: orgIdentifier
          tags: {}
          stages:
              - stage:
                  name: dep
                  identifier: dep
                  description: ""
                  type: Deployment
                  spec:
                      serviceConfig:
                          serviceRef: service
                          serviceDefinition:
                              type: Kubernetes
                              spec:
                                  variables: []
                      infrastructure:
                          environmentRef: testenv
                          infrastructureDefinition:
                              type: KubernetesDirect
                              spec:
                                  connectorRef: testconf
                                  namespace: test
                                  releaseName: release-<+INFRA_KEY>
                          allowSimultaneousDeployments: false
                      execution:
                          steps:
                              - stepGroup:
                                      name: Canary Deployment
                                      identifier: canaryDepoyment
                                      steps:
                                          - step:
                                              name: Canary Deployment
                                              identifier: canaryDeployment
                                              type: K8sCanaryDeploy
                                              timeout: 10m
                                              spec:
                                                  instanceSelection:
                                                      type: Count
                                                      spec:
                                                          count: 1
                                                  skipDryRun: false
                                          - step:
                                              name: Canary Delete
                                              identifier: canaryDelete
                                              type: K8sCanaryDelete
                                              timeout: 10m
                                              spec: {}
                                      rollbackSteps:
                                          - step:
                                              name: Canary Delete
                                              identifier: rollbackCanaryDelete
                                              type: K8sCanaryDelete
                                              timeout: 10m
                                              spec: {}
                              - stepGroup:
                                      name: Primary Deployment
                                      identifier: primaryDepoyment
                                      steps:
                                          - step:
                                              name: Rolling Deployment
                                              identifier: rollingDeployment
                                              type: K8sRollingDeploy
                                              timeout: 10m
                                              spec:
                                                  skipDryRun: false
                                      rollbackSteps:
                                          - step:
                                              name: Rolling Rollback
                                              identifier: rollingRollback
                                              type: K8sRollingRollback
                                              timeout: 10m
                                              spec: {}
                          rollbackSteps: []
                  tags: {}
                  failureStrategies:
                      - onFailure:
                              errors:
                                  - AllErrors
                              action:
                                  type: StageRollback
  EOT
}

```

3. **Version control with GitHub:** Push your Terraform configurations to your GitHub repository. Any change in the configurations can be tracked using git commits, pull requests, and more.

**Example: Repo setup**

```
├── project
|   |-- connectors
│   |		├── connetor_k8s.tf
│   |		|── connector_github.tf
│   |		|── connector_docker.tf
|   |--- secrets
│   ├── ssh_secret_text
│   │   ├── git_secret.tf
│   ├── ssh_key
|   |   |-- deploy_key.tf
│   ├── 
│   ├── pipelines
├	|--templates
|   |- services
|	|- environments
|	|- infrastructure_definition
└── project.tf
```

For more information, go to the [Solutions Architecture Team's repo](https://github.com/harness-community/solutions-architecture/tree/main/terraform-development-factory).

## Conclusion <a href="#conclusion" id="conclusion"></a>

Using a combination of Harness Git Experience and the Harness Terraform Provider, you can back up your projects and resources and restore what was potentially lost.

Harness recommends using Git Experience to back up and save pipelines, templates, input sets, and policies.

You can use the Terraform Provider to manage services, environments, connectors, and infrastructure definitions in Harness.

Harness CD integration with tools like Git and Terraform allows DevOps teams to manage and back up configurations with efficiency and precision. Using GitHub and the Harness Terraform Provider, you can adopt a robust version control and IaC strategy, ensuring that your CD processes are resilient, traceable, and restorable.

{% @harness-feedback/feedback %}
