How to migrate data between two Cosmos DB instances using PowerShell

less than 1 minute read

Tags: , ,

Categories:

Updated:

I have previously written about Cosmos DB utility tools that you cannot miss. Cosmos DB Data Migrator is the one I use most if I have to move data around across multiple instances of CosmosDB. Here is a simple script I wrote to migrate data from the source database to a target instance of Azure Cosmos DB.


$cosmosCollections = @(
  "collection1",
  "collection2",
  "yetanothercollection",
  "ThisIsTheLastOneIPromise"
  );

### Settings for Prod
$sourceConnectionString = "AccountEndpoint=<sourceendpoint>;AccountKey=<sourcekey>;Database=<sourcedatabase>"
$targetConnectionString = "AccountEndpoint=<targetendpoint>;AccountKey=<targetkey>;Database=<targetdatabase>"

$timestamp = Get-Date -Format "MM-dd-yyyy-HH-mm-ss"

foreach ($collection in $cosmosCollections)
{
  Write-Host "Starting Migration of $collection"
  $file = "./migration-$timestamp-$collection.log"
  Start-Process -Wait -NoNewWindow -RedirectStandardOutput $file -FilePath "\path\to\executable\dt.exe" `
                -ArgumentList "/ProgressUpdateInterval:00:00:05 /s:DocumentDB /s.Collection:$collection /s.ConnectionString:$sourceConnectionString /s.InternalFields /t:DocumentDBBulk /t.DisableIdGeneration /t.Collection:$collection /t.ConnectionString:$targetConnectionString"

  Write-Host "End Migration of $collection"
}

There is no value in keeping this hidden on a folder in my hard drive, is there ? So, I made it public. I am pretty sure that I will do an internet search for this later 😎. Hopefully will find this useful as well.