Search code examples
cloneazure-sql-databaserestoresmo

use smo to clone azure SQL database?


I'm writing a program to test update scripts for Azure sql. The idea is to - first clone a database (or fill a clone with the source schema and content) - then run the update script on the clone

Locally I have this working, but for azure I have the probem that I don't see any file names. If I restore one database to another on the same azure "server", don't I have to rename the data files during restore too?

For local restore I do this:

restore.Devices.AddDevice(settings.BackupFileName, DeviceType.File);
restore.RelocateFiles.Add(new RelocateFile("<db>", Path.Combine(settings.DataFileDirectory, settings.TestDatabaseName + ".mdf")));
restore.RelocateFiles.Add(new RelocateFile("<db>_log", Path.Combine(settings.DataFileDirectory, settings.TestDatabaseName + "_1.ldf")));
restore.SqlRestore(srv);

Is something similar required for cloning a database on azure?

Lots of Greetings! Volker


Solution

  • You can create a database as a copy of [source]:

    CREATE DATABASE database_name [ COLLATE collation_name ]
    | AS COPY OF [source_server_name].source_database_name
    
    {
       (<edition_options> [, ...n]) 
    }
    
    <edition_options> ::= 
    {
          MAXSIZE = { 100 MB | 500 MB | 1 | 5 | 10 | 20 | 30 … 150…500 } GB  
        | EDITION = { 'web' | 'business' | 'basic' | 'standard' | 'premium' } 
        | SERVICE_OBJECTIVE = 
              {  'basic' | 'S0' | 'S1' | 'S2' | 'S3' 
                | 'P1' | 'P2' | 'P3' | 'P4'| 'P6' | 'P11' 
                | { ELASTIC_POOL(name = <elastic_pool_name>) } } 
    }
    [;]