static void Main(string[] args) { Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); if (args.Length == 0) { Console.WriteLine(@" Windows Azure SQL Database Copy Utility This utility will take a copy of your SQL Database, and once the copy has completed, will make a backup of the database to blob storage using the bacpac format. Please supply for following command line arguments: -server [SQL Database server (without .database.windows.net)] -database [database to back up] -databasecopy (optional) [The name of the temporary copy database, defaults to database_copy] -user [SQL Database username] -pwd [SQL Database password] -storagename [Blob Storage account name] -storagekey [Blob Storage account key] -container (optional) [Blob storage container to use, defaults to sqlbackup] -datacenter [The data center where the database (not storage account) is located] (westeurope | southeastasia | eastasia | northcentralus | northeurope | southcentralus | eastus | westus) -cleanup (optional) [Remove temporary copy database after successful backup] Example usage: SQLDatabaseBackup.exe -server nevixxs -database mydb -user username -pwd password -storagename storageaccount -storagekey dmASdd1mg/qPeOgGmCkO333L26cNcnUA1uMcSSOFM... -datacenter eastasia -cleanup "); return; } if (!CheckSwitches("-server", "-database", "-user", "-pwd", "-storagename", "-storagekey", "-datacenter")) { return; } var server = GetSwitch("-server"); // i.e. the first part of xxx.database.windows.net var database = GetSwitch("-database"); // name of the database you want to back up var backupDatabase = database + "_copy"; // name for the backup database (it will create) if (GetSwitch("-databasecopy") != null) { backupDatabase = GetSwitch("-databasecopy"); } var username = GetSwitch("-user"); // database username var password = GetSwitch("-pwd"); // database password var blobAccount = GetSwitch("-storagename"); // storage account var blobKey = GetSwitch("-storagekey"); // storage key var container = "sqlbackup"; if (GetSwitch("-container") != null) { container = GetSwitch("-container"); } var dataCenterUri = ResolveUri(GetSwitch("-datacenter")); if (string.IsNullOrWhiteSpace(dataCenterUri)) { return; } using (var copier = new DatabaseCopier(CreateConnection(server, username, password))) { copier.Copy(database, backupDatabase); } var blobName = database + "-backup-" + DateTime.UtcNow.ToString("yyyy-MM-dd_hh-mm"); var exporter = new DatabaseExporter(server + ".database.windows.net", backupDatabase, username, password, string.Format("https://{0}.blob.core.windows.net/{1}/{2}.bacpac", blobAccount, container, blobName), blobKey, dataCenterUri); exporter.Export(); if (Environment.GetCommandLineArgs().Contains("-cleanup") && //be extra careful !backupDatabase.Equals(database, StringComparison.CurrentCultureIgnoreCase)) { using (var copier = new DatabaseCopier(CreateConnection(server, username, password))) { copier.DropDatabase(backupDatabase); } } Console.WriteLine("Database backed up to {0}/{1}", container, blobName); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(@" Windows Azure SQL Database Copy Utility This utility will take a copy of your SQL Database, and once the copy has completed, will make a backup of the database to blob storage using the bacpac format. Please supply for following command line arguments: -server [SQL Database server (without .database.windows.net)] -database [database to back up] -databasecopy (optional) [The name of the temporary copy database, defaults to database_copy] -user [SQL Database username] -pwd [SQL Database password] -storagename [Blob Storage account name] -storagekey [Blob Storage account key] -container (optional) [Blob storage container to use, defaults to sqlbackup] -datacenter [The data centre that both the database and storage account are located] (westeurope | southeastasia | eastasia | northcentralus | northeurope | southcentralus | eastus | westus) -cleanup (optional) [Remove temporary copy database after successful backup] Example usage: SQLDatabaseBackup.exe -server nevixxs -database mydb -user username -pwd password -storagename storageaccount -storagekey dmASdd1mg/qPeOgGmCkO333L26cNcnUA1uMcSSOFM... -datacenter eastasia -cleanup "); return; } if (!CheckSwitches("-server", "-database", "-user", "-pwd", "-storagename", "-storagekey", "-datacenter")) { return; } var server = GetSwitch("-server"); // i.e. the first part of xxx.database.windows.net var database = GetSwitch("-database"); // name of the database you want to back up var backupDatabase = database + "_copy"; // name for the backup database (it will create) if (GetSwitch("-databasecopy") != null) { backupDatabase = GetSwitch("-databasecopy"); } var username = GetSwitch("-user"); // database username var password = GetSwitch("-pwd"); // database password var blobAccount = GetSwitch("-storagename"); // storage account var blobKey = GetSwitch("-storagekey"); // storage key var container = "sqlbackup"; if (GetSwitch("-container") != null) { container = GetSwitch("-container"); } var dataCenterUri = ResolveUri(GetSwitch("-datacenter")); if (string.IsNullOrWhiteSpace(dataCenterUri)) { return; } using (var copier = new DatabaseCopier(CreateConnection(server, username, password))) { copier.Copy(database, backupDatabase); } var blobName = database + "-backup-" + DateTime.UtcNow.ToString("yyyy-MM-dd_hh-mm"); var exporter = new DatabaseExporter(server + ".database.windows.net", backupDatabase, username, password, string.Format("https://{0}.blob.core.windows.net/{1}/{2}.bacpac", blobAccount, container, blobName), blobKey, dataCenterUri); exporter.Export(); if (Environment.GetCommandLineArgs().Contains("-cleanup") && //be extra careful !backupDatabase.Equals(database, StringComparison.CurrentCultureIgnoreCase)) { using (var copier = new DatabaseCopier(CreateConnection(server, username, password))) { copier.DropDatabase(backupDatabase); } } Console.WriteLine("Database backed up to {0}/{1}", container, blobName); }