简体   繁体   中英

Backup and restore depending databases in SQL Server

I have 2 SQL Server databases, Parent and Child . Both are not always in the same server, but there is always a reference to Parent in the Child database using linked server. Child database makes changes to Parent database through stored procedures and synonyms, so both databases always are in the same 'state'.

The problem is that sometimes a client of my system makes a backup of only one of this databases, make some changes (that affects both databases), and then restore this backup. In that moment, both databases are not in the same 'state'.

I would like to know if this is happening, so when a user open the software using the Child database, I can show an error message.

I thought about setting up a Job, writing some kind of version number of both databases every n seconds, but this doesn't work, because this version number is base on time, not in changes made to the databases.

I was thinking too about a version number based on the replication log of the database, but I don't know much about the subject.

If you can write a job for them that does the restore and train them to run your job instead of a manual restore using SMS, then you can add a final step in the job that updates a "LastRestoreDateTime" column in a new table that you create... perhaps named "RestoreHistory".

Then when they launch the application you can check that value and know if you already handled this restore. Throw the error if you haven't. Continue as normal if you have.

This is just one of many possible ways.

That option does not check for data differences, however.

Option 2 would be to turn on CDC for Child database. Then in you application startup routine for your app, you could grab the last record for each of your _CDC tables, and write some logic to compare with correcsponding Parent tables.

This is not entirely practical, but it achieves the request.

For restoring the databases in the same consistent state, you need to backup the transaction files along with the data files. This can be done using custom scripts or any 3rd party SQL backup tool like Acronis or Cloudberry backup for SQL servers which will allow you to take the system as well as DB backups for your server with ease.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM