[Yandex Cloud documentation](../index.md) > [Architecture solutions](index.md) > Fault tolerance > Transferring a disk image to external S3 storage without an intermediate VM

# Transferring a Yandex Compute Cloud disk image to an S3-compatible storage of a third-party provider without an auxiliary VM

You can save a backup of your Yandex Compute Cloud [disk](../compute/concepts/disk.md) in another provider's object storage. Such a backup allows you to keep your data outside Yandex Cloud and use it for recovery.

In this tutorial, you will export a disk [snapshot](../compute/concepts/snapshot.md) or [image](../compute/concepts/image.md) to an S3-compatible storage without provisioning an auxiliary VM. You will then use the `yc-nbs-export-unpack` utility to restore the image into a `.raw` file or onto a block device. To start the VM within another cloud provider’s infrastructure, simply import the image by following the relevant provider guides.

To transfer a disk image to an S3-compatible storage:

1. [Prepare a bucket and set up bucket access](#prepare-bucket).
1. [Submit the request and export the snapshot or image](#export-snapshot).
1. [Set up the utility](#prepare-tool).
1. [Set up storage access](#configure-access).
1. [Prepare the encryption key](#prepare-encryption-key) if the disk is encrypted.
1. [Restore the image](#restore-image).

## Prepare a bucket and set up bucket access {#prepare-bucket}

You can export the snapshot or image to either a third-party provider's S3-compatible storage or to Yandex Object Storage.

To use Yandex Object Storage, [create a bucket](../storage/quickstart.md) and generate a [static access key](../iam/operations/authentication/manage-access-keys.md#create-access-key) for a service account assigned the `storage.admin` [role](../storage/operations/buckets/iam-access.md) for this bucket.

To export into a bucket hosted by a third-party provider, e.g., Selectel:

1. [Create](https://docs.selectel.ru/en/s3/buckets/create-bucket/) a bucket in Selectel:

   {% note info %}

   The example used a private [Selectel](https://selectel.ru/en/) bucket in the `ru-7` region. For a different S3-compatible storage, replace the connection properties and check out the provider's requirements related to access and object sizes.

   {% endnote %}

   * Specify a name, e.g., `disk-snapshot-dr-bucket`. If this name is taken, pick another one and replace it in the commands below.
   * Select private access.
   * Select the `Path-Style` addressing type.
   * Select the cold storage class.

     {% note warning %}

     Prior to uploading, read the [pricing policy](https://docs.selectel.ru/en/s3/about/payment/#storage-types), including as pertains to data reading and minimum retention period.

     {% endnote %}

1. On the **Connection** tab, [create](https://docs.selectel.ru/en/access-control/users/manage-panel-users/edit-panel-user-data/) an S3 key named `disk-snapshot-bucket-key`. In the key owner field, select **Me** for a one-time transfer or an individual [Selectel service user](https://docs.selectel.ru/access-control/manage/add-user/#add-service-user) for regular use.
1. Grant the service user full access to the bucket, including the following operations:

   * Object reading and writing.
   * Viewing bucket contents.
   * _Multipart upload_ operations.

1. Save `Access key` and `Secret key` to a secure location. You will need them to fill out the export form and set up the utility.

## Submit the request and export the snapshot or image {#export-snapshot}

1. In the management console, open the [disk snapshot or image export form](https://console.yandex.cloud/compute-snapshot-backup-request) and follow the on-screen instructions.
1. Wait until the files are exported to the target bucket.
1. In the bucket, locate the folder named after your snapshot or image and check that it holds the `manifest.json` file. The commands below refer to this folder as `<disk_folder>`.

If the export fails with an error or the files do not appear in the bucket, [contact support](https://center.yandex.cloud/support), specifying the export operation ID. You can look it up in the [Yandex Compute Cloud operations list](../compute/operations/operation-logs.md#get-operations).

## Set up the utility {#prepare-tool}

1. Download the archive [here](https://storage.yandexcloud.net/yc-export-tools/yc-nbs-export-tool.1.0.tar.gz).
1. Unpack the archive and navigate to the `yc-nbs-export-unpack-<version>` folder.

   Here is an example for version 1.0:

   ```bash
   tar -xzf yc-nbs-export-tool.1.0.tar.gz
   cd yc-nbs-export-unpack-0.1.0
   ```

1. Select a binary file matching your system architecture. For a list of available platforms, see `BINARIES.json`.

   #|
   || System | x64 (Intel/AMD) | ARM64 ||
   || Linux | `bin/linux-x64/` | `bin/linux-arm64/` ||
   || macOS | `bin/macos-x64/` | `bin/macos-arm64/` ||
   || Windows | `bin/windows-x64/` | — ||
   |#

   For example, on Linux x64, run the following command:

   ```bash
   ./bin/linux-x64/yc-nbs-export-unpack --help
   ```

   For Windows, use PowerShell and the relevant `.exe` file:

   ```powershell
   .\bin\windows-x64\yc-nbs-export-unpack.exe --help
   ```

   The commands below are for Linux x64. If using a different platform, replace `./bin/linux-x64/yc-nbs-export-unpack` with the path to your binary file.

   {% cut "If there is no binary for your platform" %}

   Set up a Python environment and install the dependencies:

   ```bash
   python3 -m venv .venv
   source .venv/bin/activate
   python3 -m pip install -r requirements.txt
   ```

   Check that the utility starts correctly:

   ```bash
   python3 yc-nbs-export-unpack --help
   ```

   In the commands below, replace `./bin/linux-x64/yc-nbs-export-unpack` with `python3 yc-nbs-export-unpack`.

   {% endcut %}

## Set up storage access {#configure-access}

Set the S3 access keys using the following environment variables:

```bash
export AWS_ACCESS_KEY_ID='<key_ID>'
export AWS_SECRET_ACCESS_KEY='<secret_key>'
# For temporary credentials, also set AWS_SESSION_TOKEN
```

Substitute `<key_id>` and `<secret_key>` with the actual ID and secret key used for bucket access.

{% cut "Other key provisioning methods" %}

You can save the keys to the `~/.aws/credentials` file.

```ini
[default]
aws_access_key_id = <key_ID>
aws_secret_access_key = <secret_key>
```

For temporary keys, add `aws_session_token` to the same section. To select a named profile, you can use the `--s3-profile NAME` parameter or the `AWS_PROFILE` environment variable. To specify a different file path, use the `AWS_SHARED_CREDENTIALS_FILE` variable.

When using these methods, omit `--s3-credentials-file`. Unless you explicitly specify a profile with `--s3-profile`, environment variables will override the `~/.aws/credentials` file.

Alternatively, you can create a JSON file:

```json
{
  "id": "<key_ID>",
  "secret": "<secret_key>"
}
```

Provide the path to this file using `--s3-credentials-file`. The JSON file takes precedence over environment variables and cannot be used simultaneously with `--s3-profile`.

{% endcut %}

## Prepare the encryption key {#prepare-encryption-key}

This step is for [encrypted disks](../compute/concepts/encryption.md) only.

1. Download the `<disk_directory>/encrypted.dek` and `<disk_directory>/kek.id` files from the bucket. They contain the encrypted data encryption key (DEK) and the ID of the key used to encrypt it.
1. [Decrypt the DEK](../kms/operations/symmetric-encryption.md#decryption) using the [Yandex Cloud CLI](../cli/quickstart.md#install). Your user account must have the `kms.keys.decrypter` [role assigned](../kms/operations/key-access.md#add-access-binding) for the specified key.

   ```bash
   yc kms symmetric-crypto decrypt \
     --id <kek_id> \
     --profile <profile_name> \
     --ciphertext-file encrypted.dek \
     --plaintext-file decrypted.dek
   ```

   Where `<kek_id>` is the ID from the `kek.id` file, and `<profile_name>` is the name of your CLI profile.

During the image restoration step, provide the Base64-encoded contents of the `decrypted.dek` file to the `--decryption-key` parameter. For example, you can encode it with [OpenSSL](https://docs.openssl.org/3.5/man1/openssl-enc/) by running the `openssl base64 -A -in decrypted.dek` command.

## Restore the image {#restore-image}

The utility will read the manifest file and the snapshot or image data from your S3 storage, decrypt them if necessary, and restore the disk contents. You can save the output as a `.raw` disk image file for import, or write it onto a block device.

To save the image to a file using access keys provided via environment variables, run the following command:

```bash
AWS_ACCESS_KEY_ID='<key_ID>' \
AWS_SECRET_ACCESS_KEY='<secret_key>' \
./bin/linux-x64/yc-nbs-export-unpack \
  --s3-endpoint <S3_endpoint> \
  --s3-region <storage_region> \
  --s3-bucket <bucket_name> \
  --manifest-key <disk_folder>/manifest.json \
  --output <image_file_path> \
  --workers 10
```

Where:

* `AWS_ACCESS_KEY_ID`: Bucket access key ID.
* `AWS_SECRET_ACCESS_KEY`: Bucket secret access key.
* `--s3-endpoint`: S3 storage endpoint, e.g., `https://storage.yandexcloud.net` for Yandex Object Storage or `https://s3.ru-7.storage.selcloud.ru` for a Selectel bucket.
* `--s3-region`: Storage region, e.g., `ru-central1` for Yandex Object Storage or `ru-7` for a Selectel bucket.
* `--s3-bucket`: Name of the bucket to which the snapshot or image was exported.
* `--manifest-key`: Path to the `manifest.json` file relative to the bucket. Replace `<disk_folder>` with the path to the folder containing the exported files.
* `--output`: Path to the file to save the image to, e.g., `my_disk.raw`. Make sure this file does not already exist.

  Instead of a file, you can [export the image to a block device](#restore-to-device).

* `--workers`: Number of parallel workers.

For temporary keys, also provide `AWS_SESSION_TOKEN`.

### Examples {#examples}

#### Saving the image to a file with keys from a JSON file {#restore-with-json}

```bash
./bin/linux-x64/yc-nbs-export-unpack \
  --s3-endpoint https://s3.ru-7.storage.selcloud.ru \
  --s3-region ru-7 \
  --s3-credentials-file my_cred.json \
  --s3-bucket disk-snapshot-dr-bucket \
  --manifest-key my_disk/manifest.json \
  --output my_disk.raw \
  --workers 10
```

Where `--s3-credentials-file` is the path to the JSON file with the access keys.

#### Saving the image to a file using a named profile {#restore-with-profile}

Add a `recovery` profile to `~/.aws/credentials`:

```ini
[recovery]
aws_access_key_id = <key_ID>
aws_secret_access_key = <secret_key>
```

Provide the profile name to the command:

```bash
./bin/linux-x64/yc-nbs-export-unpack \
  --s3-endpoint https://s3.ru-7.storage.selcloud.ru \
  --s3-region ru-7 \
  --s3-profile recovery \
  --s3-bucket disk-snapshot-dr-bucket \
  --manifest-key my_disk/manifest.json \
  --output my_disk.raw \
  --workers 10
```

Where `--s3-profile` is the name of the profile with the access keys in `~/.aws/credentials`. Instead of `--s3-profile recovery`, you can set the `AWS_PROFILE=recovery` environment variable if neither `AWS_ACCESS_KEY_ID` nor `AWS_SECRET_ACCESS_KEY` is defined. Otherwise, the keys from the environment variables will be used.

#### Restoring the image to a block device {#restore-to-device}

{% note warning %}

Restoration to a block device is supported only on Linux. All existing data on the target block device will be overwritten.

{% endnote %}

[Use](#restore-with-profile) the `recovery` profile and run this command, specifying the target device:

```bash
./bin/linux-x64/yc-nbs-export-unpack \
  --s3-endpoint https://s3.ru-7.storage.selcloud.ru \
  --s3-region ru-7 \
  --s3-profile recovery \
  --s3-bucket disk-snapshot-dr-bucket \
  --manifest-key my_disk/manifest.json \
  --device /dev/vdc \
  --overwrite-device \
  --workers 10
```

Where:

* `--s3-profile`: Name of the profile with the access keys in `~/.aws/credentials`.
* `--device`: Path to the target block device, e.g., `/dev/vdc`.
* `--overwrite-device`: Permission to overwrite the device data.

#### Saving an encrypted disk image to a file {#restore-encrypted-disk}

Use the [`recovery` profile](#restore-with-profile) and [prepare the encryption key](#prepare-encryption-key). Run this command:

```bash
./bin/linux-x64/yc-nbs-export-unpack \
  --s3-endpoint https://s3.ru-7.storage.selcloud.ru \
  --s3-region ru-7 \
  --s3-profile recovery \
  --s3-bucket disk-snapshot-dr-bucket \
  --manifest-key my_disk/manifest.json \
  --output my_disk.raw \
  --decryption-key "$(openssl base64 -A -in decrypted.dek)" \
  --workers 10
```

Where `--decryption-key` is the Base64-encoded content of the `decrypted.dek` file, generated with [OpenSSL](https://docs.openssl.org/3.5/man1/openssl-enc/).

#### Useful links {#see-also}

[Migrating a disk image using an auxiliary VM](../compute/operations/disk-control/disk-migration-to-external-s3.md).