我目前正在尝试使用T-SQL通过以下代码还原我的数据库:
RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH
MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf',
MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf',
GO
但是,该方法将其还原到4月4日,这是它的第二次备份。
但是,当我SSMS并还原数据库时,它会将其还原到我想要的最新备份时间。
我如何获得T-SQL语句执行相同的操作?
您可以通过以下脚本找到备份信息,然后可以还原所需的正确备份文件。
SELECT physical_device_name
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7)
AND msdb.dbo.backupset.database_name ='database_name'
ORDER BY
msdb.dbo.backupset.backup_finish_date DESC
请不要忘记BACKUP FILES是虚拟磁盘,并且在磁盘中可以放置多个BACKUPS。默认情况下,将BACKUP备份到文件(如果存在),则将新备份附加到文件中。我认为您可以在执行以下命令时看到主键:
RESTORE HEADERONLY FROM DISK = 'D:\Backup\database_name.bak'
因此,当您使用此命令还原时:
RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH
MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf',
MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
GO
您只需还原到第一个最早的备份,这等效于此命令:
RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH
MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf',
MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
WITH FILE = 1
GO
通过使用SSMS助手尝试获取最新的数据库备份,并且一定要执行此命令:
RESTORE DATABASE database_name
FROM DISK = 'D:\Backup\database_name.bak'
WITH
MOVE 'dandenong' TO 'C:\Users\alans\Desktop\Database Files\database_name.mdf',
MOVE 'dandenong_log' TO 'C:\Users\alans\Desktop\Database Files\database_name.ldf'
WITH FILE = 2
GO
您可以看一下SSMS助手在执行之前将抛出的脚本,我总是建议您使用该脚本!