SQL Server初始备份还原有效,其余的则不起作用

时间:2019-12-18 14:08:27

标签: sql sql-server backup

我正在尝试在Sql Server 2014中备份和还原数据库。 初始备份还原有效。 但是当我对数据库进行一些更改并重复备份/重新加载过程时 我得到了丢失最新更改的第一个备份的数据。 下面的脚本说明了我面临的问题

CREATE DATABASE sample;

CREATE TABLE list (
    id INT, 
    name VARCHAR(50)
);

--first record is inserted

BACKUP DATABASE sample to DISK='D:\Backup\sample.bak';

truncate table list;


GO
USE master;
GO
ALTER DATABASE sample
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE sample FROM DISK='D:\Backup\sample.bak' with REPLACE;
GO
ALTER DATABASE sample
SET MULTI_USER;
GO


--restored database contains one record
use sample;
select * from list;

--second record is inserted
insert into list values(2,'item_2');
select * from list;


BACKUP DATABASE sample to DISK='D:\Backup\sample.bak';

GO
USE master;
GO
ALTER DATABASE sample
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE sample FROM DISK='D:\Backup\sample.bak' with REPLACE;
GO
ALTER DATABASE sample
SET MULTI_USER;
GO

--restored database STILL contains one record
use sample;
select * from list;

3 个答案:

答案 0 :(得分:2)

WITH INIT语句中使用BACKUP重新初始化备份文件。

在我的Sandbox实例上,以下内容:

CREATE DATABASE [sample];
GO

USE [sample];
GO
CREATE TABLE dbo.list (i int,
                       s varchar(6));
GO

USE master;
GO

BACKUP DATABASE [sample]
    TO  DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SANDBOX\MSSQL\Backup\sample.bak'
    WITH INIT;
GO
USE [sample];
GO
TRUNCATE TABLE dbo.list;

GO
USE master;
GO
ALTER DATABASE [sample] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE [sample]
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SANDBOX\MSSQL\Backup\sample.bak'
    WITH REPLACE;
GO
ALTER DATABASE [sample] SET MULTI_USER;
GO


--restored database contains no rows (none have been inserted yet
USE [sample];
GO
SELECT *
FROM dbo.list;

--first row is inserted
INSERT INTO dbo.list
VALUES (2, 'item_2');
SELECT *
FROM dbo.list;
GO
USE master;
GO
BACKUP DATABASE [sample]
    TO  DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SANDBOX\MSSQL\Backup\sample.bak'
    WITH INIT;

GO
ALTER DATABASE [sample] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE [sample]
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SANDBOX\MSSQL\Backup\sample.bak'
    WITH REPLACE;
GO
ALTER DATABASE [sample] SET MULTI_USER;
GO

--restored database STILL contains one record
USE [sample];
SELECT *
FROM dbo.list;
GO

USE master;
GO

DROP DATABASE [sample];

在下面创建数据集:

enter image description here

如您所见,现在最后一个SELECT保留了新行。

否则,如果在INIT流程中省略了BACKUP,则需要在第二个RESTORE中提供职位。在这种情况下,它将是文件2:

RESTORE DATABASE [sample]
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SANDBOX\MSSQL\Backup\sample.bak'
    WITH REPLACE, FILE = 2;

动画GIF演示(您可能要在新窗口中打开):

Animated gif

答案 1 :(得分:1)

当您使用同一文件在其中执行backup而未在backup命令中指定with init时,新的备份将附加到该文件的末尾。这意味着在第二次备份之后,您将在同一备份文件中拥有2个备份。

当您从包含多个restore的文件中backup时,应指定with file以及此文件中备份的名称或编号,否则应指定第一个{{1} }会被聋哑人使用

答案 2 :(得分:1)

问题是默认情况下,您的backup database sample to disk='...'语句将新备份追加到备份设备(备份文件)中。结果,您将在备份文件中存储多个备份。 还原时,您正在还原第一个备份。

要解决此问题,您可以使用“ WITH INIT”参数指定覆盖备份文件的内容:

BACKUP DATABASE sample to DISK='D:\Backup\sample.bak' WITH INIT;

可以找到更多文档,例如。 here