In this post, we will understand how can to export the SQL Server database via the command line and copy it to Azure Blob storage.
It is a very common practice to schedule daily nightly backups of the SQL Server database to prevent the application's data from any hardware failure or any other human-made error.
In this post, we will be storing these automated backup files in your Azure Storage account. Storing data in an Azure Storage account is cheap as compared to the hard disk that we purchase. Also, Azure is more reliable as your data is stored in the cloud and you can access it at any time.
So, now let's start.
Our first task is to export the database
.bacpac file for our SQL Server database.
For this, follow the below steps:
Locate the SQLPackage.exe file on your system. On my system, it was located at
C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin.
If it is not on your system, you can download and install it. Click here to Download SQLPackage.exe
Run the below command to export the database into a .bacpac file.
cd "C:" cd "C:\Program Files\Microsoft SQL Server\150\DAC\bin" sqlpackage.exe /Action:Export /SourceServerName:.\SQLEXPRESS /SourceDatabaseName:InventoryDB /TargetFile:D:\DatabaseBackups\InventoryDB.bacpac
All parameters are self-explanatory.
Now, to copy the files to Azure Blob Storage, we will follow the below steps.
AzCopy is a command-line utility that you can use to copy blobs or files to or from a storage account. 👉 Click here to download AzCopy
After downloading, copy it somewhere on your system. I copied it inside the C:\azcopy directory.
Before you copy the files to Azure, make sure you are done below steps:
Run the below command to copy the .bacpac file to the Azure Storage account.
cd "C:" cd "C:\azcopy" azcopy.exe copy "D:\DatabaseBackups\InventoryDB.bacpac" "https://[account].blob.core.windows.net/[container]/[path/to/blob]?[SASToken]"
To automate this process, you can do the following:
This way, you will be able to export your daily SQL backups in Azure Storage.