Create Volume Snapshots
Learn how to create volume snapshots and use them for backup and recovery.
Using the TypeScript SDK
Benefits of Volume Snapshots
1
Create a Snapshot
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
// 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);3
Complete Example: Backup and Restore Workflow
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
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
Response Structure
Parameters
Next Steps
Last updated
