Create Volume Snapshots

Learn how to create volume snapshots and use them for backup and recovery.

Using the TypeScript SDK

Volume snapshots allow you to create point-in-time backups of your volumes. Snapshots capture the entire state of a volume at a specific moment, enabling you to restore data, clone volumes, or create multiple sandboxes with identical data.

Benefits of Volume Snapshots

  • Backup and Recovery: Create backups before applying major changes or updates.

  • Point-in-Time Recovery: Revert volumes to an earlier state if issues occur.

  • Volume Cloning: Create new volumes from snapshots to reuse the same data across multiple sandboxes.

  • Experiment Safely: Capture snapshots before running experiments to enable quick rollback if needed.

  • Data Sharing: Share volume states by creating snapshots that others can reuse.

1

Create a Snapshot

Create a snapshot from an existing volume.

create-snapshot.ts
import SandboxSDK from '@dvmcodes/sandbox-sdk';

const client = new SandboxSDK({
  apiKey: process.env['SANDBOX_SDK_API_KEY'],
});

// Create a snapshot of an existing volume
const snapshot = await client.volumes.createSnapshot('vol_x1y2z3a4b5c6d7e8', {
  name: 'my-volume-backup',
});

console.log('Snapshot created:', snapshot.id);
console.log('Snapshot name:', snapshot.name);
console.log('Source volume:', snapshot.volume_id);
2

Use Snapshot to Create Sandbox

A snapshot ID can be used as the volume_id during sandbox creation, which creates a new volume from the snapshot and attaches it automatically.

sandbox-from-snapshot.ts
// Create a sandbox with a volume created from the snapshot
const sandbox = await client.sandboxes.create({
  name: 'Sandbox with Snapshot Data',
  volumes: [
    {
      volume_id: snapshot.id, // Snapshot ID - automatically creates volume from snapshot
      mount_path: '/data',
    },
  ],
});

console.log('Sandbox created with volume from snapshot:', sandbox.id);

Every sandbox created from a snapshot ID receives its own isolated copy of the data, enabling volume cloning across multiple sandboxes.

3

Complete Example: Backup and Restore Workflow

Below is a full example illustrating a backup and restore workflow.

backup-and-restore.ts
import SandboxSDK from '@dvmcodes/sandbox-sdk';

const client = new SandboxSDK({
  apiKey: process.env['SANDBOX_SDK_API_KEY'],
});

async function backupAndRestoreWorkflow() {
  // 1. Create a volume and add some data
  const originalVolume = await client.volumes.create({
    name: 'my-data-volume',
    size: '10Gi',
  });

  // 2. Create a sandbox with the volume
  const sandbox = await client.sandboxes.create({
    name: 'Data Sandbox',
    volumes: [
      {
        volume_id: originalVolume.id,
        mount_path: '/data',
      },
    ],
  });

  // 3. Add some important data
  await client.sandboxes.execute(sandbox.id, {
    command: 'echo "Important data" > /data/important.txt',
  });

  // 4. Create a snapshot before making changes
  const snapshot = await client.volumes.createSnapshot(originalVolume.id, {
    name: 'pre-change-backup',
  });

  console.log(`Snapshot created: ${snapshot.id}`);

  // 5. Make some changes (simulating a risky operation)
  await client.sandboxes.execute(sandbox.id, {
    command: 'rm /data/important.txt', // Oops!
  });

  // 6. Restore from snapshot by creating a new volume
  const restoredSandbox = await client.sandboxes.create({
    name: 'Restored Sandbox',
    volumes: [
      {
        volume_id: snapshot.id, // Create volume from snapshot
        mount_path: '/data',
      },
    ],
  });

  // 7. Verify the data is restored
  const result = await client.sandboxes.execute(restoredSandbox.id, {
    command: 'cat /data/important.txt',
  });

  console.log('Restored data:', result.stdout);
  // Output: "Important data"
}
4

Use Case: Cloning Volumes for Parallel Processing

Snapshots are helpful when you need to spin up multiple sandboxes with the same data for parallel workloads.

clone-for-parallel.ts
async function cloneVolumeForParallelProcessing() {
  // 1. Create a volume with your dataset
  const sourceVolume = await client.volumes.create({
    name: 'dataset-volume',
    size: '50Gi',
  });

  const setupSandbox = await client.sandboxes.create({
    name: 'Setup Sandbox',
    volumes: [{ volume_id: sourceVolume.id, mount_path: '/data' }],
  });

  // 2. Download or prepare your dataset
  await client.sandboxes.execute(setupSandbox.id, {
    command: 'cd /data && wget https://example.com/large-dataset.zip',
    timeout: 600,
  });

  // 3. Create a snapshot
  const snapshot = await client.volumes.createSnapshot(sourceVolume.id, {
    name: 'dataset-snapshot',
  });

  // 4. Create multiple sandboxes from the snapshot for parallel processing
  const numWorkers = 10;
  const workerSandboxes = await Promise.all(
    Array.from({ length: numWorkers }, (_, i) =>
      client.sandboxes.create({
        name: `Worker ${i + 1}`,
        volumes: [
          {
            volume_id: snapshot.id, // Each sandbox gets its own copy from snapshot
            mount_path: '/data',
          },
        ],
      })
    )
  );

  console.log(`Created ${numWorkers} sandboxes with identical data`);
}

Best Practices

  • Snapshot Before Major Changes: Create snapshots before applying major changes to your data.

  • Regular Backups: Set up regular snapshots for volumes that are critical.

  • Name Your Snapshots: Use clear, descriptive names to indicate the purpose of each snapshot.

  • Clean Up Old Snapshots: Remove unused snapshots to keep storage costs under control.

  • Test Restores: Regularly test snapshot restores to verify they function correctly.

Response Structure

The createSnapshot() method returns a VolumeCreateSnapshotResponse object. Example:

Parameters

name string required

A descriptive name for your snapshot

Next Steps

  • View available volumes and their associated snapshots Learn how to retrieve volume listings and inspect existing snapshots.

  • Create sandboxes backed by snapshot volumes Explore how to launch new sandbox environments using snapshot-based storage.

  • Review the snapshot API documentation Refer to the API reference for a complete overview of supported snapshot operations.

Last updated