第9章 数据库备份与恢复 371
9.1. 备份概述 372
9.1.1. 数据备份分类
·
根据文件范围分
o
完整数据库(full backup)- 不包括Log backup
o
文件
o
Partial backup (under the simple recovery model )– 不备份那些read-only files
·
根据备份所有数据分
o
Full
o
Differential
·
根据是否会影响还原分
o
影响
o
不影响(Copy_Only)
9.1.2. 日志备份分类
o
一般:BACKUP LOG database_name TO <backup_device>
o
BACKUP LOG database_name TO <backup_device> … WITH COPY_ONLY
9.1.3. 常见备份类型
o
Full Database Backup
BACKUP DATABASE xxx TO <backup_device>
o
Differential Database Backup
BACKUP DATABASE xxx TO <backup_device> WITH DIFFERENTIAL
o
Full File Backup–备份一个或多个文件或文件组中的所有数据!
BACKUP DATABASE database
{ FILE = logical_file_name |
FILEGROUP = logical_filegroup_name } [ ,...f ]
TO backup_device [ ,...n ]
[ WITH with_options [ ,...o ] ] ;
--Backup the files in the SalesGroup1
secondary filegroup.
BACKUP DATABASE
Sales
FILE = 'SGrp1Fi2',
FILE = 'SGrp2Fi2'
TO DISK = 'G:\SQL Server
Backups\Sales\SalesGroup1.bck'
GO
o
Differential File Backup
--Creates a differential file backup
of every file in both of the secondary filegroups.
BACKUP DATABASE
Sales
FILEGROUP = 'SalesGroup1',
FILEGROUP = 'SalesGroup2'
TO DISK = 'C:\MySQLServer\Backups\Sales\SalesFiles.bck'
WITH DIFFERENTIAL,
o
Log Backup
9.2. 选择备份策略和恢复模式 375
9.2.1. 简单恢复模式下的备份 376
·
Option 1 - Full backup
·
Option 2 - Full backup
+ Differential backup
9.2.2. 完整恢复模式下的备份 377
·
Option 1 - Full + Logs
·
Option 2 - Full +
Differential + Logs
·
可以用以下的脚本查看一个数据库的所有的备份记录。注意2点:(1)Type - 标识数据库备份的类型: D (数据库) ,
L (日志)
,
I (差异数据库)
,F
(文件或文件组)
;(2)Log backup的LSN总是连续的,而Full或Differential的不一定连续。
SELECT DISTINCT
s.first_lsn
,
s.last_lsn
,
s.database_backup_lsn
,
s.backup_finish_date
,
s.type ,
y.physical_device_name
FROM msdb..backupset
AS s
INNER JOIN msdb..backupfile AS f ON f.backup_set_id = s.backup_set_id
INNER JOIN msdb..backupmediaset AS m ON s.media_set_id = m.media_set_id
INNER JOIN msdb..backupmediafamily AS y ON m.media_set_id = y.media_set_id
WHERE ( s.database_name = 'AdventureWorks' )
ORDER BY s.backup_finish_date
DESC;
9.2.3. 文件或文件组备份 381
·
File/FileGroup backup/restore in full recovery model is complex
and hard to manage. They are only used in some extra large databases. For more
information, see http://technet.microsoft.com/en-us/library/ms189860(v=sql.105).aspx
·
Under the full recovery
model, you 不仅需要文件/文件组备份,你还需要备份 transaction log。恢复时,不但需要文件组备份本身,还需要依次恢复从上一次完整数据库备份到恢复的目标时间点为止的所有日志备份。
·
Example:The
following illustration shows a strategy in which a full database backup is
taken (at time t1) soon after the database is created (at time t0). This first
database backup enables transaction log backups to start. Transaction log
backups are scheduled to occur at set intervals. File backups occur at whatever
interval best meets the business requirements for the database. This
illustration shows each of the four filegroups being backed up one at a time.
The order in which they are backed up (A, C, B, A) reflects the business
requirements of the database.
9.3. 选择数据库还原方案 382
·
数据库完整还原
还原整个数据库,将从完整数据库备份开始,然后还原差异数据库备份(和日志备份)。
·
文件还原
文件还原的目标是还原一个或多个损坏的文件,而不还原整个数据库。
在简单恢复模式下,仅只读文件支持文件备份。在还原数据库备份或部分备份时,将始终一同还原主文件组和读/写辅助文件组。具体例子见下节。
在完整恢复模式或大容量加载恢复模式下包含多个文件或文件组的数据库相关。文件还原方案由复制、前滚和恢复相应数据的单一还原顺序组成。
- 如果正在还原的文件组为读/写文件组,则在还原上一次数据备份或差异备份以后必须应用连续的日志备份链。这样才会使文件组记录到日志文件的当前活动日志记录中。恢复点通常靠近日志的末端,但并非总是如此。
- 如果正在还原的文件组为只读文件组,则通常不需要应用日志备份,并且会跳过该操作。如果在文件变成只读后进行了备份,则该备份为要还原的最后备份。前滚在目标点停止。
·
页面还原
还原单个页面。 页面还原仅在完整恢复模式和大容量日志恢复模式下可用。
·
段落还原 (Piecemeal Restore) –从主文件组开始,按照重要性分阶段还原数据库。段落还原的主要优点是能帮助让最重要的数据最先上线。
从主文件组和一个或多个辅助文件组开始,分阶段还原数据库。 段落还原将从 RESTORE DATABASE 开始,使用
PARTIAL
选项并指定一个或多个要还原的辅助文件组。
表 9-3 不同恢复模式所支持的各种还原方案* - Enterprise Edition only
还原方案 在简单恢复模式下 在完整/大容量日志恢复模式下 数据库完整还原 这是基本的还原策略。 数据库完整还原可能涉及完整数据库备份的简单还原和恢复。另外,完整的数据库还原还可能涉及还原完整数据库备份,以及还原和恢复差异备份 这是基本的还原策略。数据库完整还原涉及还原完整数据库备份或差异备份(如果有),以及还原所有后续日志备份(按顺序)。通过恢复并还原上一次日志备份(RESTORE WITH RECOVERY),完成数据库完整还原 文件还原* 只能还原损坏的只读文件,但不还原整个数据库。所以实用性不是很强 能够还原一个或多个文件,而不还原整个数据库。可以在数据库处于离线状态或数据库保持在线状态(对于SQL Server 2005的某些版本)时执行文件还原。在文件还原过程中,包含正在还原的文件的文件组一直处于离线状态。其它文件组有可能被访问 页面还原 不适用 还原损坏的页面。可以在数据库处于离线状态或数据库保持在线状态(对于SQL Server 的企业版)时执行页面还原。在页面还原过程中,正在还原的页面一直处于离线状态。 必须具有完整的日志备份链(包含当前日志文件),并且必须恢复所有这些日志备份,以使页面与当前日志文件保持一致 段落还原* 按文件组级别并从主文件组和所有读写辅助文件组开始,分阶段还原和恢复数据库 按文件组级别并从主文件组开始,分阶段还原和恢复数据库
无论以何种方式还原数据,在恢复数据库前,SQL Server数据库引擎都会保证整个数据库在逻辑上的一致性。例如,还原一个文件以后,必须恢复完整的一套日志文件备份,以便将该文件里的事务前滚足够长度,与数据库保持一致,才能恢复该文件并使其在线。
9.3.1. 数据库完整还原 384
·
Simple Recovery Model
o
Restore Full
o
Restore Full + Differential
·
Full or Bulk_Logged
o
Restore Full + Logs
o
Restore Full +
Differential + Logs
o
基本步骤
ü
备份尾日志
ü
还原最新完整数据库备份WITH NORECOVERY。
ü
还原最新的差异备份WITH NORECOVERY。
ü
依次还原日志包括尾日志WITH NORECOVERY
ü
还原数据库WITH RECOVERY
USE master;
--Create
tail-log backup.
BACKUP LOG AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2FullRM.bak'
WITH NORECOVERY;
GO
--Restore
the full database backup (from backup set 1).
RESTORE DATABASE
AdventureWorks2008R2
FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2FullRM.bak'
WITH FILE=1,
NORECOVERY;
--Restore
the regular log backup (from backup set 2).
RESTORE LOG AdventureWorks2008R2
FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2FullRM.bak'
WITH FILE=2,
NORECOVERY;
--Restore
the tail-log backup (from backup set 3).
RESTORE LOG AdventureWorks2008R2
FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2FullRM.bak'
WITH FILE=3,
NORECOVERY;
GO
--recover
the database:
RESTORE DATABASE
AdventureWorks2008R2 WITH RECOVERY;
GO
ü
也可恢复数据库到某个时间点(RESTORE DATABASE database_name WITH STOPAT='????????', RECOVERY)。这一步也可以与还原上一次日志备份一起使用。
RESTORE LOG AdventureWorks
FROM DISK ='Z:\SQLServerBackups\AdventureWorks.bak'
WITH FILE=3, STOPAT='XXXXxx:xx:xx', RECOVERY;
·
The above Tail Log /Full
[Differential]/Log/Tail Log approach is
o The most popular
practice. Good for many small and medium databases
o But
for large databases, the restore could take a day (remember, when you restore
the database, the database is unavailable).
o Can
you do online restore? Yes, you can, but it is not for the entire database. It
is for a file, page, or FG. More specifically,
§ Online
restore is supported only on SQL Server Enterprise edition. In this edition, a
file, page, or piecemeal restore is online by default. This topic is relevant
for databases that contain multiple files or filegroups (and, under the simple
recovery model, only for read-only filegroups).
§ Restoring
data while the database is online is called an online restore. A database is
considered to be online whenever the primary filegroup is online, even if one
or more of its secondary filegroups are offline. Under any recovery model, you
can restore a file that is offline while the database is online. Under the full
recovery model, you can also restore pages while the database is online.
9.3.2. 文件还原 386 – Applicable to a database with multiple files or FGs
·
Simple Mode - 只能还原损坏的只读文件,但不还原整个数据库。所以实用性不是很强。Applicable to the database
with one or more read-only secondary filegroups.
The primary filegroup and read/write secondary filegroups are always restored
together, by restoring a database or partial backup (not by file restore)。
o
基本步骤 – 其实本质上就一步,不需要Log
ü
Restore each damaged file
from its most recent file backup.
ü
Restore the most recent
differential file backup for each restored file and recover the database (if
any)
o
Offline restore -3 read-only
files, but in two different backup devices.
RESTORE DATABASE database FILE = name_of_file_A
FROM file_backup_of_file_A
WITH NORECOVERY;
RESTORE DATABASE database FILE = name_of_file_B, name_of_file_C
FROM file_backup_of_files_B_and_C
WITH RECOVERY;
o
Online restore – 只有A
read-only file in filegroup B, b1, 需要恢复.
RESTORE DATABASE adb FILE='b1' FROM filegroup_B_backup
WITH RECOVERY
·
Full or Bulk_Logged Mode - 能够还原一个或多个文件,而不还原整个数据库。可以在数据库处于离线状态或数据库保持在线状态时执行文件还原。在文件还原过程中,包含正在还原的文件的文件组一直处于离线状态。其它文件组有可能被访问.
o
离线还原 – two secondary
R/W files, A and B,
ü
创建活动事务日志的尾日志备份。
--Take the file offline.
ALTER DATABASE database_name MODIFY FILE SET OFFLINE;
-- Back up the currently active transaction log.
BACKUP LOG database_name
TO <tail_log_backup>
WITH NORECOVERY;
GO
ü
从每个损坏的文件的最新文件备份还原相应文件
-- Restore the files.
RESTORE DATABASE database_name FILE=name
FROM <file_backup_of_file_A>
WITH NORECOVERY;
RESTORE DATABASE database_name FILE=<name> ......
FROM <file_backup_of_file_B>
WITH NORECOVERY;
ü
针对每个还原的文件,还原最近的差异文件备份(如果有)。
ü
按顺序还原事务日志备份,从覆盖最早还原文件的备份开始,到在步骤1中创建的尾日志备份结束。
-- Restore the log backups.
RESTORE LOG database_name FROM <log_backup>
WITH NORECOVERY;
RESTORE LOG database_name FROM <log_backup>
WITH NORECOVERY;
RESTORE LOG database_name FROM <tail_log_backup>
WITH RECOVERY;
o
离线还原(主文件组损坏) – In this example, the primary filegroup and the
read-only filegroup B are damaged, but R/W filegroups A and C are intact. ,
ü
创建活动事务日志的尾日志备份 with NO_TRUNCATE 。
NO_TRUNCATE Specifies that the log not be truncated
and causes the Database Engine to attempt the backup regardless of the state of
the database. Consequently, a backup taken with NO_TRUNCATE might have
incomplete metadata. This option allows backing up the log in situations where
the database is damaged.)
BACKUP LOG adb TO tailLogBackup WITH NORECOVERY, NO_TRUNCATE
ü
Restore the primary filegroup
and filegroup B as follows
RESTORE DATABASE adb FILEGROUP='Primary' FROM backup1 WITH NORECOVERY
RESTORE DATABASE adb FILEGROUP='B' FROM backup2 WITH NORECOVERY
RESTORE LOG adb FROM backup3 WITH NORECOVERY
RESTORE LOG adb FROM backup4 WITH NORECOVERY
RESTORE LOG adb FROM backup5 WITH NORECOVERY
RESTORE LOG adb FROM tailLogBackup WITH RECOVERY
RESTORE DATABASE adb FILEGROUP='B' FROM backup2 WITH NORECOVERY
RESTORE LOG adb FROM backup3 WITH NORECOVERY
RESTORE LOG adb FROM backup4 WITH NORECOVERY
RESTORE LOG adb FROM backup5 WITH NORECOVERY
RESTORE LOG adb FROM tailLogBackup WITH RECOVERY
o
在线还原 – one R/W file, a1
ü
Online
restore of file a1.
RESTORE DATABASE adb FILE='a1' FROM backup
WITH NORECOVERY;
At this point, file a1 is in the RESTORING state, and filegroup
A is offline.
ü
Create a log backup - After
restoring the file, the database administrator takes a new log backup to make
sure that the point at which the file went offline is captured.
BACKUP LOG adb TO log_backup3;
现在应该可以理解这句话是什么意思:
对于离线文件还原,在文件还原之前必须始终先进行一次尾日志备份。对于在线文件还原,在文件还原之后必须始终先进行一次日志备份。此日志备份对于将文件恢复到与数据库的其余部分一致的状态至关重要。如果因为日志已损坏而无法执行此操作,则文件还原无法进行,必须还原整个数据库。
For an offline file restore, you
must always take a tail-log backup before the file restore. For an online file
restore, you must always take the log backup after the file restore. This log
backup is necessary to allow for the file to be recovered to a state consistent
with the rest of the database.
If the log has been damaged, a
tail-log backup cannot be created, and you must restore the whole database.
ü
Online restore of log
backups.
The administrator restores all
the log backups taken since the restored file backup, ending with the latest
log backup (log_backup3, taken
in step 2). After the last backup is restored, the database is recovered.
RESTORE LOG adb FROM log_backup1 WITH NORECOVERY;
RESTORE LOG adb FROM log_backup2 WITH NORECOVERY;
RESTORE LOG adb FROM log_backup3 WITH NORECOVERY;
RESTORE LOG adb WITH RECOVERY;
File a1 is now online.
o
在线还原 – a read-only file(由于不需要Log,
与Simple下的一样)
ü
Online
restore of file a1.
RESTORE DATABASE adb FILE='b1' FROM filegroup_B_backup
WITH RECOVERY
Filegroup B is now online.
9.3.3. 页面还原 387
·
Simple Mode – Not
Applicable
·
Full or Bulk_Logged
Mode -还原损坏的页面。可以在数据库处于离线状态或数据库保持在线状态(对于SQL
Server 的企业版)时执行页面还原。在页面还原过程中,正在还原的页面一直处于离线状态。
·
When to use it?
o
A page restore is intended
for repairing isolated damaged pages.
·
Limitations
o
Page restore applies to SQL
Server databases that are using the full or bulk-logged recovery models.
o
Page restore is supported
only for read/write filegroups.
o
Only data pages can be
restored. Page restore cannot be used to restore other pages such as
transaction log, the GAM pages, the SGAM pages, the file boot page), page 1:9
(the database boot page) etc.
·
The basic steps to restore damaged pages
o
Obtain the File IDs and Page IDs in one of the following ways
ü
From the suspect_pages table in the
msdb: msdb..suspect_pages
ü
SQL Server error
Log
ü
Events Traces
ü
DBCC
ü
WMI Provider
o
Start a page restore with a full database, file, or filegroup
backup that contains the page using the PAGE clause to list the page IDs of all
of the pages to be restored.
o
Apply the most recent differentials.
o
Apply the subsequent log backups.
o
Create a new log backup of the database that includes the
final LSN of the restored pages, that is, the point at which the last restored
page is taken offline.
o
Restore the new log backup. After this new log backup is
applied, the page restore is completed and the pages are now usable.
·
Example:
The following example restores four damaged pages of file B with NORECOVERY.
Next, two log backups are applied with NORECOVERY,
followed with the tail-log backup, which is restored with RECOVERY.
This example performs an online restore. In the example, the file ID of file B is 1, and the page IDs of the
damaged pages are 57, 202, 916, and 1016.
RESTORE DATABASE <database> PAGE='1:57, 1:202, 1:916,
1:1016'
FROM <file_backup_of_file_B>
WITH NORECOVERY;
RESTORE LOG <database> FROM <log_backup>
WITH NORECOVERY;
RESTORE LOG <database> FROM <log_backup>
WITH NORECOVERY;
BACKUP LOG <database> TO <new_log_backup>;
RESTORE LOG <database> FROM <new_log_backup> WITH RECOVERY;
GO
9.3.4. 段落还原 390
·
基本概念
o
What is for? - allows databases that contain multiple filegroups to be
restored in stages.
o
Which
recovery model can use it? – All three, but more flexible for the Full and
Bulk_Logged than for the Simple model.
o
Which edition
can use it? – almost all, but enterprise supports online restore
o
What is the
basic procedure? - A partial restore sequence
starts with a RESTORE DATABASE statement that restores a full backup and
specifies the PARTIAL option. You must specify PARTIAL only one time in the
initial statement of the partial-restore sequence. A piecemeal restore can
include several restore sequences.
·
Simple -按文件组级别并从主文件组和所有读写辅助文件组开始,分阶段还原和恢复数据库
o
Restore steps
ü
Step 1 -
Initial stage (restore and recover the primary filegroup and all read/write
filegroups)
ü
Step 2 -
restore and recover the read-only files.
o
Example:
Filegroups A and C are read/write, and filegroup B is read-only. The most recent partial backup
contains the primary filegroup and the read/write secondary filegroups, A and C.
ü
Step1 - Partial restore of
the primary and filegroups A and C.
RESTORE DATABASE adb FILEGROUP='A',FILEGROUP='C'
FROM partial_backup WITH PARTIAL, RECOVERY;
At this point, the primary and filegroups A and C are online. All files in filegroup B are recovery pending, and
the filegroup B is offline.
ü
Step 2 - Online restore of
filegroup B.
RESTORE DATABASE adb FILEGROUP='B' FROM backup WITH RECOVERY;
All filegroups are now online.
All filegroups are now online.
·
Full or Bulk_Logged
Mode - 按文件组级别并从主文件组开始,分阶段还原和恢复数据库
o
Steps
ü
Tail log backup
ü
Start a partial restore
sequence by restoring a partial backup with the primary FG.
ü
Filegroup-restore sequences
bring additional filegroups online to a point consistent with the database.
ü
Applying Log Backups
(Optional)
o
Examples
9.3.5. 还原方案小结 393
9.3.6. 孤立用户故障排除 394
·
对于Windows登录账号,如果新的服务器和原先的服务器在同一个域里,
sys.database_principals里的数据库用户和master数据库sys.server_principals里的登录账号可以自动匹配,Windows登录账号可以自动继续使用用户数据库。如果两者SID不同,说明域也发生了变化。SQL Server认为这两个Windows登录账号不是同一个。Windows服务器登录账户和数据库用户是不能强制链接在一起的。
·
对于SQL登录账号,数据库sys.database_principals里的SID和master数据库sys.server_principals里的SID一般是不同的。
o
检测孤立用户: sp_change_users_login @Action='Report';
o
人工强制匹配:sp_change_users_login@Action='update_one',@UserNamePattern='<database_user>',@LoginName='<login_name>';
o
另一方法就是备份和恢复Master
9.4. 系统数据库备份与恢复 395
9.4.1. master数据库 396 – 注意以下几点
·
还原master数据库后,SQL Server实例将自动停止。
·
如果我们将master数据库恢复到一台新的服务器上,难免文件地址会有所变化。这时SQL Server将找不到其它数据库。需要使用单用户模式启动SQL Server,将master数据库里的信息修改成新的地址。
·
如果决定以单用户模式重新启动服务器,应首先停止所有SQL Server服务(服务器实例本身除外),并停止所有SQL Server实用工具(如SQL Server代理,报表服务器等)。停止服务和实用工具可以防止它们尝试访问服务器实例。否则单用户启动后,它们会占用这个用户连接,管理员反而连不进去了。
After you restore master,
the instance of SQL Server is stopped automatically. If you have to make
additional repairs and want to prevent more than a single connection to the
server, restart the server in single-user mode. Otherwise, the server can be
restarted regularly. If you decide to restart the server in single-user mode, first stop all SQL Server
services, except the server instance itself, and stop all SQL Server
utilities, such as SQL Server Agent. By stopping the services and utilities,
you prevent them from trying to access the server instance.
9.4.2. Model数据库 397- 需要备份, Simple即可,tempdb要用它,与其它User DB的完整还原一样。
9.4.3. msdb数据库 397 –默认是Simple,但建议使用Full。
9.4.4. tempdb和资源数据库 398 – 这2个都不需要备份。但后者也可做文件备份以防硬盘错误。
9.5. 带有filestream功能的数据库备份和恢复 399 - 对于有FILESTREAM功能的数据库,在进行备份和恢复时,会自动包含FILESTREAM数据。并不需要额外做文件级别的备份和恢复。
9.6. 应对由于备份损坏导致的还原错误 401
9.6.1. 为什么备份文件也是坏的?
·
备份文件和数据库放在同一个(或一组)物理硬盘上。硬盘出故障,备份也保不住。
·
备份介质损坏;或者做的是网络备份,数据在网络传输中发生了损坏。
·
数据库在做完整备份、文件备份或者文件组备份的时候,里面的内容就已经有了损坏。
9.6.2. 怎么办?
·
修复硬件错误并重新尝试还原操作。- 很少成功
·
忽略错误,继续还原操作,并在还原完成后修复数据库。- 不是万能的
o
RESTORE DATABASE database_name FROM backup_device WITH CONTINUE_AFTER_ERROR, [NORECOVERY ]
o
如果错误发生在一些比较关键的地方,比如某个数据文件的文件头信息,那么恢复还是有可能完全失败。
o
在忽略错误继续执行的还原顺序结束时,可以使用DBCCCHECKDB修复数据库。建议在DBCC CHECKDB命令中使用WITH TABLOCK选项。
DBCC CHECKDB(GPOSDB,REPAIR_ALLOW_DATA_LOSS) WITH tablock
·
放弃还原操作,改用备用还原计划。-事先预备一台备用机,将做好的备份使用Log Shipping的机制在备用服务器上预先恢复好。
9.7. 实例:将数据库系统在一台新服务器上恢复 403
原先服务器出现硬件故障,已经无法启动。需要整个SQL系统紧急
迁移到一台备用服务器上(备用服务器名字叫sql2005pc,SQLSERVER是默认实例)
备用服务器和原服务器不同名,SQL安装的路径也不一样。(备用服务器安装路径为
C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL)
因为良好的备份策略,现在手头有最新的master,msdb,model数据库备份,以及其它所有用户数据库备份
现在需要将系统数据库恢复,以还原所有数据库系统信息(用户,密码,任务等)
然后才能恢复用户数据库。
9.7.1. 确认备用服务器的SQL Server版本和原服务器一致。
因为我们需要恢复系统数据库,需要保证我们恢复的master和msdb要能够和备用机的resource数据库一致。否则SQL Server将不能正常工作。所谓版本一致,指的是“select @@version”返回的号码必须完全一样。
因为我们需要恢复系统数据库,需要保证我们恢复的master和msdb要能够和备用机的resource数据库一致。否则SQL Server将不能正常工作。所谓版本一致,指的是“select @@version”返回的号码必须完全一样。
9.7.2. 在备用服务器的命令行窗口,用指令以单用户模式启动SQL Server服务。
NET START MSSQLSERVER /m
NET START MSSQLSERVER /m
9.7.3. 在命令行窗口,用sqlcmd这个命令行工具连接SQL Server。
sqlcmd -E -S sql2005pc
sqlcmd -E -S sql2005pc
9.7.4. 在sqlcmd的那个连接里,运行下面恢复语句(假设备份文件为’c:\lab\master.bak’)。
restore database master from disk = ‘c:\lab\master.bak’
go
restore database master from disk = ‘c:\lab\master.bak’
go
SQL Server服务自动停止了。
9.7.5. 由于恢复的master数据库里记载的其它数据库的路径和现在的路径不一致,这时候重新启动SQL Server会失败。必须要用trace flag 3608来启动。
net start MSSQLSERVER /f /m /T3608
net start MSSQLSERVER /f /m /T3608
9.7.6.
用sqlcmd连接修改其它数据库的文件路径到现有的正确路径(’C:\Program Files\Microsoft SQL
Server\MSSQL.4\MSSQL\Data\’)。
sqlcmd -E -S sql2005pc
sqlcmd -E -S sql2005pc
alter database mssqlsystemresource modify file (name =data,
filename=’C:\Program Files\Microsoft SQL
Server\MSSQL.4\MSSQL\Data\mssqlsystemresource.mdf’);
go
go
alter database mssqlsystemresource modify file (name =log,
filename=’C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\mssqlsystemresource.ldf’);
go
go
alter
database msdb modify file (name =MSDBData, filename=’C:\Program Files\Microsoft
SQL Server\MSSQL.4\MSSQL\Data\msdbdata.mdf’);
go
go
alter
database msdb modify file (name =MSDBLog, filename=’C:\Program Files\Microsoft
SQL Server\MSSQL.4\MSSQL\Data\msdblog.ldf’);
go
alter
database model modify file (name =modeldev, filename=’C:\Program
Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\model.mdf’);
go
go
alter
database model modify file (name =modellog, filename=’C:\Program
Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\modellog.ldf’);
go
go
alter
database tempdb modify file (name =tempdev, filename=’C:\Program
Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\tempdb.mdf’);
go
go
alter
database tempdb modify file (name =templog, filename=’C:\Program
Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\templog.ldf’);
go
go
9.7.7.
全部修改完毕后,运行“exit”命令退出sqlcmd连接。
net stop MSSQLSERVER
9.7.8.
用正常模式启动SQL Server。
net start MSSQLSERVER
这时,SQL Server可以正常启动。但是它使用的系统数据库除了master以外,都是原先备用服务器自己的。我们要用生产服务器上的备份来替换它们。
net start MSSQLSERVER
这时,SQL Server可以正常启动。但是它使用的系统数据库除了master以外,都是原先备用服务器自己的。我们要用生产服务器上的备份来替换它们。
9.7.9.
恢复msdb, model.
在运行下面命令之前,要先关闭SQL Server Agent服务。然后用restore命令恢复mdsb,将其指向新的文件路径。
restore database msdb from disk = ‘c:\lab\msdb.bak’
with move ‘MSDBData’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\msdbdata.mdf’,
move ‘MSDBLog’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\msdblog.ldf’, replace
在运行下面命令之前,要先关闭SQL Server Agent服务。然后用restore命令恢复mdsb,将其指向新的文件路径。
restore database msdb from disk = ‘c:\lab\msdb.bak’
with move ‘MSDBData’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\msdbdata.mdf’,
move ‘MSDBLog’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\msdblog.ldf’, replace
restore
database model from disk = ‘c:\lab\model.bak’
with move ‘modeldev’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\model.mdf’,
move ‘modellog’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\modellog.ldf’, replace
with move ‘modeldev’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\model.mdf’,
move ‘modellog’ to ‘C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\Data\modellog.ldf’, replace
9.7.10.
修改服务器名称
运行下面的语句你会发现,返回的还是原先的服务器。这时因为master是从那台机器来的。
Select @@servername
Select @@servername
运行下面语句修改服务器名。
Sp_dropserver
‘<原先服务器名>‘
Go
Sp_addserver ‘SQL2005PC’, ‘local’
Go
Go
Sp_addserver ‘SQL2005PC’, ‘local’
Go
9.7.11.
重启SQL服务,再运行下面语句,就可以看到返回现在的服务器名字了。
Select @@servername
Go
Select @@servername
Go
9.7.12.
做完这些操作后,原先SQL
Server的所有配置都能够恢复到新的服务器上。只是用户数据库都是质疑状态,因为新服务器上没有它们的文件。接下来就可以使用前文介绍的恢复方法,将用户数据库依次恢复。
9.8. 小结 408