real-world tde database migrations

In today’s post, I will tell you about how we managed to successfully complete a migration during a ~12 hour maintenance window. This could have taken a LOT longer if didn’t have dbatools to automate several of the steps.

Although I will not go in to every detail about our process, I want to emphasize the areas were we chose to use dbatools to make our lives easier.

the goal

Recently, we got the green light for upgrading to SQL Server 2016 and we were ready to roll. Our task was to migrate multiple servers, each having several TDE encrypted databases on them. All the databases were mirrored on SQL Servers hosted in a different datacenter.

When protecting data using TDE, special care must be taken when it comes to migrations. We had two primary options for migrating TDE protected databases.

first option

One option would be decrypt the databases on the old servers prior to the migration. This can take a while depending on the database size as the process would touch every single data page on disk. Same would be true once we encrypted the databases on the new servers.

If you ever decide to take this route during your migration, make sure you follow the correct and complete process to disable TDE. Otherwise you can lock your data if you don’t have the certificates and keys backed up somewhere else, ready to be restored in case of emergency. And perhaps most importantly, make sure you test this process!

second option

The second option (and the one we chose) was to leave the encryption enabled. In order to be able to attach the files, or to do restores from the backups you need to have the same certificate that was used for encryption. This certificate is protected by the master key.

To accomplish this:

  1. Make backups of the master key and the certificates
  2. Restore the key and certificates on the new principal and mirror pairs

Be aware that each database can have its own certificate! You must be sure which database is protected by which certificate. Failing to have this sorted out will leave you with some files you cannot attach or restore anywhere. Basically, you’d lose the data 😢

Need help figuring all of this out? Check out Microsoft’s article Move a TDE Protected Database to Another SQL Server. As I mentioned before make sure you test this process ahead of time!

preparation

In preparation for the migration day, we built all the new servers (primaries and mirrors) ahead of time and configured them based on our requirements.

A key point is to restore the keys used for encryption to the new servers. From this point on you won’t have to worry too much about TDE.

Next is where dbatools comes into play:

Doesn’t matter how many servers you have in your environment, doing it like this saves you a lot of time and you can be sure you have the same configuration for all of them.

Next, we created our DBA toolkit database where we keep all the handy stuff:

Couldn’t be easier than this!

moving forward

Next, we created our backup jobs:

For auditing purposes we even saved the old server configuration and compare it to the new ones, all with only a few lines of code:

Save $propcompare to a database using Write-DbaDataTable and you’re set.

Transfering the logins from the old server is now easier than ever (no more sp_help_revlogin):

Beat that if you can! This way we ensured that old logins will work as soon as we start the applications.

We started to build the mirrors ahead of time (we have some big databases). We got the details for the last full backups on the old servers:

Based on the output of the above we fired up a quick PowerShell script that got everything copied over to a network share. From there, restoring the backups to the new mirrors was simple as:

I can do this all day long, especially when piping it directly to the Backup-DbaDatabase 🙂

finalizing the migration

During the maintenance window we just got a last DIFF for each database and restored that to the new mirrors. Some manual growth on some of the databases and the restore of the DIFFs took us the longest time (those contained several days of data for each server).

To shorten the time for the principals, we used detach/attach of the data and log drives approach which went pretty smooth with no unexpected incidents.

And, in case you’re wondering, we do have valid backups. We even restore and test those automatically on a separate server using dbatools as well. These jobs take around 17 hours per day.

I must confess, we did a bit of T-SQL to bring the mirroring up and now we’re back in business, HA included.

post migration

Now to the post migration stuff.

Once we’re running on the new instances, we made sure we enable all the jobs we already created ahead of time:

Alternatively, piping support was added to today’s release, 0.9.191. With this release and above, you can use the following syntax:

Once we’re running on the new instances, we made sure we enable all the jobs we already created ahead of time:

We decided we need some more RAM on a few of the servers and after we added it, we just executed the following to make SQL Server aware of the change.

Now, the moment of truth. Application started and what do you know, everything just works!

  • No orphaned logins
  • No SID mismatches for the logins
  • No error messages.

<insert happy tears here>

automation is awesome

As you can see, a lot of steps were automated using dbatools and this saved us a lot of time overall, making this multi-terabyte database migration across 5 environments very smooth.

There is still room for a lot more automation, for even more efficiency. I’m sure we’ll do better next time.

Thanks again to all of you contributing to this project and Chrissy, thanks for starting all this. Can’t wait to see what the future will bring for dbatools and I hope I’ll be able to contribute more.

Happy migrations everyone!

- Viorel 🇧🇪

2 thoughts on “real-world tde database migrations

  1. Clive Strong Reply

    Awesome post – Using dbatools for our migration too and it makes life so much easier.

  2. Seth Reply

    Excellent! Thank you for your post. There are a couple of tricks in there I hadn’t learned yet.

Leave a Reply

Your email address will not be published. Required fields are marked *