Skip to main content

How to manual create and restore a database from SQL Azure

In ultima perioada totul se automatizeaza, iar in multe cazuri in loc sa scriem cod, putem sa ne definim un proces prin intermediul unei interfete grafice. Acest lucru se intampla si pentru mecanismul de back-up (replicare) a unei baza de date SQL Azure.
In momentul de fata portalul ne permite sa facem usor back-up automat la date, la un anumit interval de timp. Acest lucru poate sa ne fie foarte folositor cand baza noastra de date devine corupta. Si nu va ganditi la cazuri precum cineva a spart Windows Azure si acuma poate sa faca ce vrea sau un hacker a pus mana pe string-ul de conexiune si isi face de cap. Exista cazuri mult mai simple cand baza voastra de date poate sa ajunga corupta, de exemplu un user sterge sau face update la anumite date, iar aplicatia voastra are un bug care cauzeaza ca datele sa fie intr-o stare inconsistenta.
Remarks: Sa nu uitati ca orice tranzactie, transfer de date, ... etc costa, iar backup-ul unei baza de date sau sincronizarea a doua sau mai multe baze de date costa. Daca masina era la noi in parcare ne costa curent si altfel de resurse. Iar Microsoft ne garanteaza ca orice baza de date SQL Server este replicata in 3 copii in acelasi data center.
In aceasta era, cand totul se poate configura printr-o interfata grafica extrem de prietenoasa, sunt cazuri cand trebuie sa face acest proces manual (din cod). In urmatoarele randuri o sa va prezint cum se poate face acest lucru.
Daca vrem sa facem back-up la o baza de date in acelasi data center avem la dispozitie comanda CREATE DATABASE, care in combinatie cu AS COPY OF poate sa faca minuni. Aceasta o sa genereze o noua baza de date, reprezentand copia fidela a bazei de date date ca sursa. Aceasta copie nu o sa fie 1 la 1 cu cea care exista in momentul in care apelati comanda ci o sa fie o copie fidela cu datele care erau in momentul cand crearea bazei de date a luat sfarsit (acest proces este tranzactional). Noua baza de date o sa fie accesibila dupa ce s-a facut commit la crearea acesteia. In timpul procesului de creare, noua baza de date nu este accesibila.
CREATE DATABASE [myNewDatabase]
AS COPY OF [sourceServerName].[sourceDatabaseName]
Comanda trebuie sa fie executata pe serverul SQL Azure unde vrem sa cream o backup-ul.
Procesul de back-up intr-o alta baza de date poate sa difere o perioada destul de lunga (in functie de dimensiunea bazei de date), din aceasta cauza avem la dispozitie o comanda care poate sa fie executata pe serverul SQL Azure unde se face noua copie pentru a putea progresul la backup.
SELECT [db].[name], [c].*
FROM sys.dm_database_copies c
JOIN sys.databases db
ON c.database_id = db.database_id
Daca partea de restore se face destul de simplu, apare o problema in momentul in care avem o baza de date corupta si vrem sa facem switch la baza de date pe care o avem in backup. Acest lucru se face destul de usor daca redenumim baza de date cu numele original, iar baza de date corupta primeste un alt nume. Acest caz o sa functioneze data cele doua baze de date sunt pe aceiasi instanta SQL Azure.
ALTER DATABASE [MyDatabase]
MODIFY NAME = [MyDatabase_Invalid]
GO
WAITFOR DELAY '00:01:00'
GO
ALTER DATABASE [MyDatabase_Backup]
MODIFY NAME = [MyDatabase]
GO
Ati avut nevoie manual sa faceti acest lucru? Eu am avut nevoie partea de restore sa o fac manual si sa o integrez intr-un tool. Pe partea de creare de backup am lasat totul sa fie configurat din portal.

Comments

Popular posts from this blog

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Can User Technical Profile Influence AI Architecture Decisions?

In the last few days, I ran a small experiment on Devin/Windsurf/Cascade, BMAD, and LLM-based architecture decisions. The initial concern was simple: if an AI coding assistant knows something about the user, for example, that the user is a Java developer or a .NET developer, can this information influence the architecture and technology stack that the AI will propose? This question is important because many companies are starting to use AI tools not only for coding but also for product discovery, architecture, documentation, and technical decision-making. If the AI is influenced by the developer's personal profile, the result may not be fully neutral. It may look like a business or architecture decision, but in reality, it may be partially shaped by the context of the person using the tool. The test was done using BMAD with Devin/Windsurf/Cascade. The scenario was a unified field service operations platform. The task was to let BMAD refine the product, define the architecture and s...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...