简体   繁体   English

备份和还原SQL Server中的相关数据库

[英]Backup and restore depending databases in SQL Server

I have 2 SQL Server databases, Parent and Child . 我有2个SQL Server数据库, ParentChild Both are not always in the same server, but there is always a reference to Parent in the Child database using linked server. 两者并不总是位于同一台服务器中,但是在使用链接服务器的Child数据库中始终有对Parent的引用。 Child database makes changes to Parent database through stored procedures and synonyms, so both databases always are in the same 'state'. Child数据库通过存储过程和同义词对Parent数据库进行更改,因此两个数据库始终处于相同的“状态”。

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. 我想知道这是否正在发生,因此,当用户使用Child数据库打开软件时,我会显示一条错误消息。

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. 我考虑过设置一个Job,每n秒编写两个数据库的某种版本号,但这是行不通的,因为该版本号是基于时间的,而不是基于对数据库所做的更改。

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". 如果您可以为他们写一个执行还原的作业,并训练他们运行该作业,而不是使用SMS进行手动还原,那么您可以在该作业中添加最后一步,以更新新表中的“ LastRestoreDateTime”列创建...也许命名为“ 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. 选项2是打开CDC for Child数据库。 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. 然后,在应用程序的应用程序启动例程中,您可以获取每个_CDC表的最后一条记录,并编写一些逻辑以与对应的父表进行比较。

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. 可以使用自定义脚本或任何第三方SQL备份工具(例如SQL Server的Acronis或Cloudberry备份)来完成,这将使您轻松地为服务器获取系统以及数据库备份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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