# Backup and restore

> Back up and restore the OneFuse appliance and policies

---

Site [llms.txt](/llms.txt)

---

## Back up the OneFuse appliance

The steps below assume a single OVA instance. If you use an HA setup, adapt these steps to restore the necessary files and database to the appropriate locations.

Any backup tool or strategy of the Enterprise can be used for the OneFuse appliance.

The critical data needed from the appliance are as follows:

1. Create a backup of the `cloudbolt` database:

   - PostgreSQL:

     ```bash
     # Execute the following command on the appliance
     # Replace '/tmp/' with the desired directory

     export PGPASSWORD=${postgresql_pass}
     pg_dump -h localhost -p 5432 -U cb_dba -w --clean -F t -f /tmp/cloudbolt.tar cloudbolt

     # The database will get backed up to the /tmp folder as file name: cloudbolt.tar
     ```

1. Create a backup of the following directories:

   | Path | Location (for non-monolithic installs) |
   | --- | --- |
   | `/var/opt/cloudbolt` | All nodes |
   | `/var/log/cloudbolt/jobs` | All nodes running Job Engine role |
   | `/var/www/html/cloudbolt/static` | All nodes running Web Server role |

It is ideal to keep the backups of the directories and database closely time-stamped to ensure integrity when restored (for example, Jobs in the database have corresponding Job Logs).

If your OneFuse instance is on a single appliance (monolithic), all the paths in the table above would be backed up and restored to that single appliance instance.

Refer to the [Reference architecture](/docs/onefuse/installation/reference-architecture/) documentation for more information on the distribution of Web Server and Job Engine roles.

## Restore the OneFuse appliance

In the event of a complete loss of the Virtual Appliance, a complete recovery can be made given the proper backup material outlined above.

Follow the steps below to recreate the OneFuse instance.

### Deploy the appliance

1. Deploy the same version of the OneFuse Appliance that was being used previously and follow the [install steps](/docs/onefuse/installation/install-from-ova/).
1. During setup, use the *same* FQDN/IP address(es) as used previously.
1. Copy and overwrite the following directories from backup via WinSCP or from Linux command line:

   | Path | Location (for non-monolithic installs) |
   | --- | --- |
   | `/var/opt/cloudbolt` | All nodes |
   | `/var/log/cloudbolt/jobs` | All nodes running Job Engine role |
   | `/var/www/html/cloudbolt/static` | All nodes running Web Server role |

### Restore the database

1. Copy the database backup file (for example, `cloudbolt.tar`) to `/tmp/cloudbolt.tar`.
1. SSH to the appliance and run the following commands:

   ```bash
   # Replace the following with the information from your new database: REMOTE_DB_DNS_NAME, REMOTE_DB_USER, and REMOTE_DB_PORT
   export PGPASSWORD=${REMOTE_DB_PASS}
   pg_restore -h REMOTE_DB_DNS_NAME -U REMOTE_DB_USER --no-owner --role=REMOTE_DB_USER -w -p REMOTE_DB_PORT -e --clean --if-exists -d REMOTE_DB_DNS_NAME /tmp/cloudbolt.tar

   # Connect to the "cloudbolt" database
   psql -U REMOTE_DB_USER -d cloudbolt

   # You should connect successfully

   \dt

   # You should see all tables listed

   SELECT * FROM job_log

   # You should see a list of all Job Logs

   exit
   exit
   ```

### Restart web server processes

If the database restored properly, restart Web Server processes from the command line:

```bash
service httpd restart
```

### Validate OneFuse functionality

Validate OneFuse functionality via UI:

1. Open a browser to the application(s) UI.
1. Log in to OneFuse.
1. Verify previous Policies, Jobs and/or Managed Objects are visible.
1. Verify (as a **Super Admin**) that the License file is present.

## Back up OneFuse policies with the Python module

Leverage the OneFuse Python Module to back up OneFuse Policies from one OneFuse instance and then restore policies to another instance, store them in a git repository, or to provide the policies (in JSON format) to CloudBolt Customer Success.

### Prerequisites {#prerequisites-backup}

<div class="alert alert-info" role="alert">


Identify a system to run the Python commands from. This could be your own desktop or a server in your environment. You will need to have Python 3 or greater installed on the system.
</div>


1. Verify **Python** is installed

    <div class="alert alert-info" role="alert">


    [Python 2 has sunset.](https://www.python.org/doc/sunset-python-2/) If previously installed and on Python 2, upgrade to the latest Python.
    </div>


    - If **Python** is not installed (or upgrading), [download](https://www.python.org/downloads/) the latest Python

1. Verify **pip** is installed

    - [Install pip](https://pip.pypa.io/en/stable/installation/) if it does not exist

1. Review Python's [Installing Packages](https://packaging.python.org/tutorials/installing-packages/)

### Install the OneFuse Python package

OneFuse Python Module is distributed as a Python Package. Install the OneFuse Python Package using `pip`.

<div class="alert alert-info" role="alert">


It is recommended to install the latest OneFuse Python Package that matches the OneFuse Appliance version.
</div>


<div class="alert alert-info" role="alert">


If the OneFuse Python Module is already installed, skip to [Upgrade the OneFuse Python package](#upgrade-the-onefuse-python-package).
</div>


1. Open a **Command Prompt** or **Terminal** window
1. Install OneFuse Python Package using instructions below:

#### Option 1: Install the latest version

```bash
pip install onefuse
```

#### Option 2: Install a specific version

Optionally, list the available OneFuse Python package versions. Then, install with a specific version:

```bash
pip install onefuse==<version>
```

#### Upgrade the OneFuse Python package

Upgrade to the latest OneFuse Python Package using `pip`.

1. Open a **Command Prompt** or **Terminal** window
1. Install OneFuse Python Package using the command below:

    ```bash
    pip install --upgrade onefuse
    ```

### Back up OneFuse policies

Once the OneFuse Python Module is installed, use the Python console to execute the commands to create backup of OneFuse Policies.

1. Open a **Command Prompt** or **Terminal** window
1. Enter the **Python Console** via command:

    ```bash
    python
    ```

1. **Import the necessary modules** via commands:

    ```python
    from onefuse.admin import OneFuseManager
    from onefuse.backups import BackupManager
    ```

1. **Connect to the OneFuse instance**, replacing `username`, `password` and `OneFuse FQDN` appropriately, via command:

    ```python
    # Replace <username>, <password> and <OneFuse FQDN> with your OneFuse information below:
    ofm = OneFuseManager("<username>", "<password>", "<OneFuse FQDN>")

    # Example
    # ofm = OneFuseManager("admin", "admin", "onefuse-staging.company.com")
    ```

1. **Instantiate the Backup module** via command:

    ```python
    backups = BackupManager(ofm)
    ```

1. **Run the Backup**, replacing the path specified accordingly, via command:

    ```python
    # Select your own backup path for the following command.
    # Windows Example: you will want to use double backslashes:
    backups.backup_policies('C:\\Temp\\onefuse_backups\\')

    # Linux Example:
    backups.backup_policies('/tmp/onefuse_backups/')
    ```

    - **Note:** For security reasons, **passwords** will *not* be exported.

1. **Verify backup:** the backup will create a file structure in the folder specified from **Step 6**. View each of the policies under their respective folders.

    <div class="alert alert-info" role="alert">


    Find and provide the `JSON` files of the Policies in question along with the Module Endpoints, if any, to Customer Success.
    </div>


If desired, [restore OneFuse policies](#restore-onefuse-policies-with-the-python-module).

## Restore OneFuse policies with the Python module

Leverage the OneFuse Python Module to restore OneFuse Policies.

### Prerequisites {#prerequisites-restore}

Verify the [prerequisites](#prerequisites-backup) were performed and the [OneFuse Python Module is installed](#install-the-onefuse-python-package) on the server you are performing restore Python commands on.

### Restore OneFuse policies

Once the OneFuse Python Module is installed, use the Python console to execute the commands to restore OneFuse Policies.

1. Open a **Command Prompt** or **Terminal** window.
1. Enter the **Python Console** via command:

    ```bash
    python
    ```

1. **Import the necessary modules** via commands:

    ```python
    from onefuse.admin import OneFuseManager
    from onefuse.backups import BackupManager
    ```

1. **Connect to the OneFuse instance**, replacing `username`, `password` and `OneFuse FQDN` appropriately, via command:

    ```python
    # Replace <username>, <password> and <OneFuse FQDN> with your OneFuse information below:
    ofm = OneFuseManager("<username>", "<password>", "<OneFuse FQDN>")

    # Example
    # ofm = OneFuseManager("admin", "admin", "onefuse-staging.company.com")
    ```

1. **Instantiate the Backup module** via command:

    ```python
    backups = BackupManager(ofm)
    ```

1. **Verify backup policies exist:** the backup folder that the OneFuse Policies are getting restored from should contain the folder structure created by the backup, with each policy under its respective folder.
1. **Run the Restore command**, replacing the path specified accordingly, via command:

    ```python
    # Select your own restore path for the following command.
    #Windows Example: you will want to use double backslashes:
    backups.restore_policies_from_file_path('C:\\Temp\\onefuse_backups\\')

    #Linux Example:
    backups.restore_policies_from_file_path('/tmp/onefuse_backups/')
    ```

1. After restoring OneFuse Policies, log in to the restored OneFuse Instance and update all of the **passwords** for each **Module Credential**. The OneFuse Backup process does not export passwords.
