How do I copy SQL Azure database to my local development server? - sql-server

Does anyone know how I can copy a SQL Azure database to my development machine? I'd like to stop paying to have a development database in the cloud, but it's the best way to get production data. I copy my production database to a new development database but I'd like to have that same database local.
Any suggestions?

There are multiple ways to do this:
Using SSIS (SQL Server Integration Services). It only imports data in your table. Column properties, constraints, keys, indices, stored procedures, triggers, security settings, users, logons, etc. are not transferred. However it is very simple process and can be done simply by going through wizard in SQL Server Management Studio.
Using combination of SSIS and DB creation scripts. This will get you data and all missing metadata that is not transferred by SSIS. This is also very simple. First transfer data using SSIS (see instructions below), then create DB Create script from SQL Azure database, and re-play it on your local database.
Finally, you can use Import/Export service in SQL Azure. This transfers data (with a schema objects) to Azure Blob Storage as a BACPAC. You will need an Azure Storage account and do this in Azure web portal. It is as simple as pressing an "Export" button in the Azure web portal when you select the database you want to export. The downside is that it is only manual procedure, I don't know a way to automate this through tools or scripts -- at least the first part that requires a click on the web page.
Manual procedure for method #1 (using SSIS) is the following:
In Sql Server Management Studio (SSMS) create new empty database on your local SQL instance.
Choose Import Data from context menu (right click the database -> Tasks -> Import data...)
Type in connection parameters for the source (SQL Azure). Select ".Net Framework Data Provider for SqlServer" as a provider.
Choose existing empty local database as destination.
Follow the wizard -- you will be able to select tables data you want to copy. You can choose to skip any of the tables you don't need. E.g. if you keep application logs in database, you probably don't need it in your backup.
You can automate it by creating SSIS package and re-executing it any time you like to re-import the data. Note that you can only import using SSIS to a clean DB, you cannot do incremental updates to your local database once you already done it once.
Method #2 (SSID data plus schema objects) is very simple. First go though a steps described above, then create DB Creation script (righ click on database in SSMS, choose Generate Scripts -> Database Create). Then re-play this script on your local database.
Method #3 is described in the Blog here: http://dacguy.wordpress.com/2012/01/24/sql-azure-importexport-service-has-hit-production/. There is a video clip with the process of transferring DB contents to Azure Blob storage as BACPAC. After that you can copy the file locally and import it to your SQL instance. Process of importing BACPAC to Data-Tier application is described here: http://msdn.microsoft.com/en-us/library/hh710052.aspx.

Copy Azure database data to local database:
Now you can use the SQL Server Management Studio to do this as below:
Connect to the SQL Azure database.
Right click the database in Object Explorer.
Choose the option "Tasks" / "Deploy Database to SQL Azure".
In the step named "Deployment Settings", connect local SQL Server and create New database.
"Next" / "Next" / "Finish"

In SQL Server 2016 Management Studio, the process for getting an azure database to your local machine has been streamlined.
Right click on the database you want to import, click Tasks > Export data-tier application, and export your database to a local .dacpac file.
In your local target SQL server instance, you can right click Databases > Import data-tier application, and once it's local, you can do things like backup and restore the database.

I just wanted to add a simplified version of dumbledad's answer, since it is the correct one.
Export the Azure SQL Database to a BACPAC file on blob storage.
From inside SQL Management studio, right-click your database, click "import data-tier application".
You'll be prompted to enter the information to get to the BACPAC file on your Azure blob storage.
Hit next a couple times and... Done!

I think it is a lot easier now.
Launch SQL Management Studio
Right Click on "Databases" and select "Import Data-tier application..."
The wizard will take you through the process of connecting to your Azure account, creating a BACPAC file and creating your database.
Additionally, I use Sql Backup and FTP (https://sqlbackupandftp.com/) to do daily backups to a secure FTP server. I simply pull a recent BACPAC file from there and it import it in the same dialog, which is faster and easier to create a local database.

You can also check out SQL Azure Data Sync in the Windows Azure Management Portal. It allows you to retrieve and restore an entire database, including schema and data between SQL Azure and SQL Server.

Using msdeploy.exe
Caveat: msdeploy.exe fails to create the destination database on its own, so you need to create it manually first.
Copy the connection string on the database properties page. Adjust it so that it contains a correct password.
Get the connection string for the destination DB.
Run msdeploy.exe like this:
"c:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -dest:dbDacFx="destination_DB_connection_string",dropDestinationDatabase=true -source:dbDacFx="azure_DB_connection_string",includeData=true -verbose
Using SqlPackage.exe
Export the azure DB to a bacpac package.
"c:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe" /a:Export /ssn:"azure_db_server" /sdn:"azure_db_name" /su:"user_name" /sp:"password" /tf:"file.bacpac"
Import the package to a local DB.
"c:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe" /a:Import /SourceFile:"file.bacpac" /TargetServerName:".\SQLEXPRESS" /TargetDatabaseName:CopyOfAzureDb

It's pretty easy. This worked for me...in terms of getting an Azure SQL database down onto your local machine...:
Open your SQL Management Studio and connect to your Azure SQL Server.
Select the database you would like to get down onto your local machine, and right-click...select "Generate Scripts". Follow the prompts...
BUT, be careful in that if you ALSO want the DATA, as well as the scripts, be sure to check the Advanced Options before beginning the generating...scroll down to "Types of data to script", and make sure you have "Schema and data"...or whatever is appropriate for you.
It will give you a nice SQL script file which can then be run on your local machine and it will create the database as well as populate it with all the data.
Bare in mind that in my case, I have no FK or other constraints...also, it wasn't a lot of data.
I don't recommend this as a backup mechanism in general...

In SQL Server Management Studio
Right click on the database you want to import, click Tasks > Export data-tier application, and export your database to a local .dacpac file.
In your local target SQL server instance, you can right click Databases > Import data-tier application, and once it's local, you can do things like backup and restore the database.

I couldn't get the SSIS import / export to work as I got the error 'Failure inserting into the read-only column "id"'. Nor could I get http://sqlazuremw.codeplex.com/ to work, and the links above to SQL Azure Data Sync didn't work for me.
But I found an excellent blog post about BACPAC files: http://dacguy.wordpress.com/2012/01/24/sql-azure-importexport-service-has-hit-production/
In the video in the post the blog post's author runs through six steps:
Make or go to a storage account in the Azure Management Portal.
You'll need the Blob URL and the Primary access key of the storage
account.
The blog post advises making a new container for the bacpac file and
suggests using the Azure Storage
Explorer for that. (N.B.
you'll need the Blob URL and the Primary access key of the storage
account to add it to the Azure Storage Explorer.)
In the Azure Management Portal select the database you want to
export and click 'Export' in the Import and Export section of the
ribbon.
The resulting dialogue requires your username and password for the
database, the blob URL, and the access key. Don't forget to include
the container in the blob URL and to include a filename (e.g.
https://testazurestorage.blob.core.windows.net/dbbackups/mytable.bacpac).
After you click Finish the database will be exported to the BACPAC
file. This can take a while. You may see a zero byte file show up
immediately if you check in the Azure Storage Explorer. This is the
Import / Export Service checking that it has write access to the
blob-store.
Once that is done you can use the Azure Storage Explorer to download
the BACPAC file and then in the SQL Server Management Studio
right-click your local server's database folder and choose Import
Data Tier Application that will start the wizard which reads in the
BACPAC file to produce the copy of your Azure database. The wizard
can also connect directly to the blob-store to obtain the BACPAC
file if you would rather not copy it locally first.
The last step may only be available in the SQL Server 2012 edition of the SQL Server Management Studio (that's the version I am running). I do not have earlier ones on this machine to check. In the blog post the author uses the command line tool DacImportExportCli.exe for the import which I believe is available at http://sqldacexamples.codeplex.com/releases

Regarding the " I couldn't get the SSIS import / export to work as I got the error 'Failure inserting into the read-only column "id"'.
This can be gotten around by specifying in the mapping screen that you do want to allow Identity elements to be inserted.
After that, everything worked fine using SQL Import/Export wizard to copy from Azure to local database.
I only had SQL Import/Export Wizard that comes with SQL Server 2008 R2 (worked fine), and Visual Studio 2012 Express to create local database.

The accepted answer is out of date. I found a better answer: Use Import Data-tier Application
More detailed information please see this article:
Restoring Azure SQL Database to a Local Server

You can try with the tool "SQL Database Migration Wizard".
This tool provide option to import and export data from azure sql.
Please check more details here.
https://sqlazuremw.codeplex.com/

I always use Import/Export Feature which seems to be the easiest one among all.
Step 1:
Get the backup from the azure instance as follows, Select the database → Right click → Tasks → Export Data Tier Application.
Step 2:
Give a specific name for the backup file and save it in your desired location
Step 3: That's it you have taken a backup of the database from sql instance to your local. Lets restore it to the local. Copy the backed up database to your C drive. Now open the PowerShell with administrator rights and navigate to C drive
Step 4: Lets download the powershell script to remove the master keyRemoveMasterKey.ps1 have the script on the same drive in this case its C.
Step 5 : Run the script as follows,
.\RemoveMasterKey.ps1 -bacpacPath "C:\identity.bacpac"
That's it, now you can restore it on MSSQL 2017 in your local environment.
Step 6: Connect to your local server, and click Databases → Import-Data-Tier-Application
Step 7 : Give a name for your database to restore.
Now you will see everything in green!
Read my blog with diagrams.

Using SSMS v18.9+, you can use the Deploy Database to Microsoft Azure SQL Database.
It's not really intuitive, but the wizard allow you to select a local db even if the name of the task is Deploy Database to Microsoft Azure SQL Database.
Connect to your Azure Database using SSMS
Right-click on the database, select Tasks > Deploy Database to Microsoft Azure SQL Database.
Select your local SQL server as the target connection.
Follow the additional steps.

You can use the new Azure Mobile Services to do a nightly backup export from SQL Azure to a .bacpac file hosted in Azure Storage. This solution is 100% cloud, doesn't require a 3rd party tool and doesn't require a local hosted SQL Server instance to download/copy/backup anything.
There's about 8 different steps, but they're all easy:
http://geekswithblogs.net/BenBarreth/archive/2013/04/15/how-to-create-a-nightly-backup-of-your-sql-azure.aspx

Looks like the functionality is missing from Management Studio in 2022. Here is how I do it using Azure Data Studio:
Install Azure Data Studio
Open Azure Data Studio
Install the extension called Admin Pack for SQL Server
Restart Azure Data Studio
Setup connections for both source database (Azure) and destination (local machine)
Execute statement in your local DB Engine on master database:
sp_configure 'contained database authentication', 1;
GO
RECONFIGURE;
GO
Right click on source database and select Data-tier Application Wizzard
Select Export to .bacpac file [Export bacpac] (4th, last option in my current version)
Finish the wizard
Right click on destination database and select Data-tier Application
Wizzard
Select Import from .bacpac [Import bacpac] (3rd option in my version)
Select the .bacpac file previously created and finish the wizard
After importing the logins and users are created, but their default schema isn’t set. If default shcema is required that needs to be handled manually. In that case:
Open an admin connection to the local copy and run:
USE [imported-db]; ALTER USER imported-user WITH DEFAULT_SCHEMA = whatever;

Download Optillect SQL Azure Backup - it has 15-day trial, so it will be enough to move your database :)

The trick for me was to start replicating PKs/FKs/constraints on empty DB, then temporarily disable constraints while importing data (see https://stackoverflow.com/a/161410).
More precisely:
Create empty target DB manually;
Right-click source DB > Tasks > Generate Scripts;
Run script file on empty target DB (now DB has correct PKs/FKs/constraints, but no data);
Disable all constraints;
Import data (Right-click target DB > Tasks > Import Data);
Re-enable constraints.
Hope this helps!

Now you can use the SQL Server Management Studio to do this.
Connect to the SQL Azure database.
Right click the database in Object Explorer.
Choose the option "Tasks" / "Deploy Database to SQL Azure".
In the step named "Deployment Settings", select your local database connection.
"Next" / "Next" / "Finish"...

Use the Import/Export service in SQL Azure to create a .bacpac file.
Then take a look at this method in another Stack Overflow article.
Azure SQL Database Bacpac Local Restore

If anyone has a problem to import a Bacpac of a DB that uses Azure SQL Sync, Sandrino Di Mattia developed a great simple application to solve this.
Export a Bacpac of your DB
Dowload Di Mattia's binary
With this console app repair the downloaded Bacpac
Lauch SSMS
Right Click on "Databases" and select "Import Data-tier Application"
Select the repaired Bacpac.

If anyone wants a free and effective option (and don't mind doing it manually) to backup database to Local then use schema and data comparison functionality built into the latest version Microsoft Visual Studio 2015 Community Edition (Free) or Professional / Premium / Ultimate edition. It works like a charm!
I have BizPark account with Azure and there is no way to backup database directly without paying. I found this option in VS works.
Answer is taken from https://stackoverflow.com/a/685073/6796187

Hi I'm using the SQLAzureMW tool for SQLAzure DB migration and management. Very useful one. It was downloaded from codeplex, but currently it's not available the codeplex will be going to shutdown, the same application tool is now available in GttHub. This below link is explain how to use this tool and also available the Application for download.
https://github.com/twright-msft/azure-content/blob/master/articles/sql-database/sql-database-migration-wizard.md

Related

Export table from Azure & Backup DB locally

I am new to Azure and I have a few doubts I'm having trouble with.
I purchased a DB for which I pay just a few bucks a month for testing purposes. I was able to replicate and connect to the DB just fine.
My problem comes in two scenarios
I cannot find a way, at the SQL Server Management Studio, to back up the DB to my VPS (also at Azure).
Is there a way to create a script to duplicate the database tables and indexes structure in order to copy that database to another machine?
A data-tier application (DAC) is a logical database management entity that describes all SQL Server objects connected with a user's database, such as tables, views, and instance objects, including logins. A DAC package, also known as a DACPAC, is a self-contained unit of SQL Server database deployment that allows data-tier developers and database administrators to package SQL Server objects into a portable artefact called a DAC package.
A BACPAC is a similar artefact that contains both the database schema and the data contained in it.
Using sqlpackage.exe may be run from the command line. There
appear to be two options:
To include data in the extraction, use the ExtractAllTableData option
to extract a DACPAC and then publish it to Azure.
After exporting a BACPAC, import it into Azure.
To more about the operations could be performed using BAPAC and DACPAC, here the MSFT Document.

Azure SQL Server restore using .bak file

I have created a SQL Server in Azure and have downloaded the Azure Data Studio. I have a SQL Server database backup file (.bak) and connected to the sever that I created from Azure Data Studio and I am not seeing an option to restore the database.
I just have the back up file available and don't have any on perm database available and I want to directly restore the database to the Azure SQL Server.
Azure SQL database currently does not support this feature. The only way to restore a SQL database in Azure is to import from a BACPAC file as #Larnu said. You can follow this guide to use a BACPAC file to migrate to an Azure SQL database.
Another option:
If you get an account to connect to the local server.You can use SQL Management Studio to migrate it directly to Azure.
You can use the Azure Data Migration Assistant to perform an assessment and migrate to Azure SQL from a local database.
While both replies before mine are correct I want to add something.
You have 3 ways to migrate with a right click:
SSMS: Task > Deploy Database to SQL Azure , which will create a BACPAK file, not a .bak file.
DMA: Assess and migrate
Azure Data Studio: I discovered in this video at minute 00:35 that you can actually use Azure Data Studio with some preview functionality that will help you migrate. I haven't found any plugin and in the video there is a Microsoft e-mail so I asked to try the preview. I don't expect to be better than DMA which is the go-to tool for migration.
EDIT: I sent an e-mail to Microsoft which replied to me
"Hi Francesco,
Thanks for reaching out. We are currently in private preview in East US2 region. Will that work for you ? We will soon be expanding to other regions, so please stay tuned."
So one day migration through Azure Data Studio will be possible but for now it's only to East US2 region

Uploading database to Azure SQL

I need to upload an MDF file to Azure SQL. According to Is it possible to attach a MDF file to an Azure SQL database? the correct way to do this is to import the MDF to SQL Server Management Studio and then right click and export it to Azure SQL.
However all I'm getting is an error claiming that:
I have no idea what this actually means nor why I am unable to upload the database to Azure. Any suggestions?
In order to import a Database to Azure, you will need to create a BACPAC file. Instructions can be found in this MSDN blog post.
And just in case you run into it, this question has some good links for where to get the SQL Server Data Tools required to import a BACPAC from Azure. You probably need to get those installed before being able to export to BACPAC.
Update: Adding this so you can see my export options:
This message means that you will need to create a Windows VM within Azure that has a full install of SQL Server on it.
https://msdn.microsoft.com/en-us/library/azure/jj823132.aspx
Another way is using this tool.
http://sqlazuremw.codeplex.com/
It will generate the script from your local database and execute it on Azure Sql.

When Deleting/Re-importing a .bacpac file from v12 Azure SQL Database in to the same server tables are not imported

Here is what I did:
Created export (.bacpac saved to storage account) of "my-db" Azure SQL Database using (new) management portal.
Deleted the database "my-db" (using new Azure Portal) from "my-v12-azure-db-server".
Again from the new Azure portal, attempted to import the .bacpac file from step one into "my-v12-azure-db-server".
Result: The database with the correct name is created, but it is empty. There are no errors returned through the UI. It contains none of the tables from the original database.
Other Things Tried: I was able to successfully import the same .bacpac file into other v12 db servers on the same Azure subscription. I also tried deleting the database server and re-creating it, but the same thing happens when I bring it back. In addition, I attempted deleting the db server using powershell, but after recreating it I had the same result.
Ideally I'd like to keep the same db server name and database name so that I do not have to change connection strings in source. Am I missing something?
It looks as though the problem could be a bug (or feature unknown to me) within the azure portal.
As a workaround, I was able to update a local installation of SQL Server Management Studio 2014 to the newest patch and then import from my box using SSMS. I will try to remember to update my response if I get a better response from Microsoft Premier Support.
Cumulative update package 6 for SQL Server 2014:
http://support.microsoft.com/en-us/kb/3031047
To perform the import from local SSMS to the Azure v12 Database:
Azure SQL Database Bacpac Local Restore
Did you checked the import/export status in the SQL DB server after ordering bacpac restore? You can do it in the new portal.
Also, I found that one time the restore operation had restored the backup into a database with the .bacpac name and not the name I choose in the portal

How to export the localDb to an online Sql Server Database?

I used visual studio 2013 for developing a web application and used a localDB in development phase, now I want to go to production phase and want to export the sql server localDb to the online SQL server database, but I cannot find any option to script out the localDb or migrate it?
Which tools I can use to do this ? or what I must do?
There are atleast two routes; you could publish and recover as Data Tier Application:
Or, the easier-but-lengthier route would be to:
Create a new DB in the SQL Server Management Studio (Naming it the same as LocalDB is helpful if you just want to change the connection string later)
Right-click database on SQL Server Object Explorer and select Schema Compare
Click Select Target and navigate through Select Target Schema(Dialog) > Database > Click on Select Connection > Connect(Dialog) > Browse > Local > Database Name > Select the Databse you created in Step-1. Then, click OK
Click Compare or, hit Shift+Alt+C
Then, the Schema Comparator tells you whats different between the two databases (the one from MSSqlLocalDB and the one you just created in Step-1). It is important to remember unchecking ANY entries for delete operations, if you are simply transferring data and schema. Keep add operations as it is.
Then, click on ↑UPDATE (the option is located right beside Compare)
Your Target database should be updated
You can attach the localDb to SQLServer 2005 or higher and then you can generate scripts or export data to remote server.
Or
If LocalDb is SQL Server 2012, use SSMS 2012 or later for this scripting task.
Unfortunately VS2013 does not has any feature to script out a localDb, I waste so many time to copy table by table scripts, and run it at remote db. my local db id sql v11.0 (2012 I think). and the remote db was sql server 2008. so one way exist is to install an instance of sql server 2012 and sql server management studio to script out my db. maybe another tools also exist it is very sad that vs has not this feature.

Resources