一,读取备份文件
SQL:
Restore filelistonly from disk=’’ 读取差异和完整备份文件
Restore headeronly from disk=’’ 读取日志备份文件
SQL Litespeed:
Master.. xp_restore_headeronly @Filename=’’ 读取日志备份文件
如果结果没有返回错误,基本上备份文件就是正确的。
注释:我们DB现在差异和完整备份一般都是用SQL Litespeed,日志备份用SQL.
例如:
Master..xp_restore_filelistonly @Filename='D:/PALBackUp/PAL_COMPSN_P80_DIFF20071008.BAK'----读取差异和完整备份文件
Restore headeronly from disk='D:/PALBackUp/PAL_COMPSN_P80_LOG20071008.TRN' ---读取日志备份文件
二、指定访问路径权限
Exec master..xp_cmdshell 'net use path PWD /user:fp-qsmc"administrator'
三、断开当前对DB PAL的操作
Declare @SQLStr nchar(100)
Declare @myspid smallint
Declare @mycursor cursor
Declare whocursor cursor for
Select spid from master..sysprocesses where dbid=db_id('PAL')
Set @mycursor=whocursor
Open @mycursor
Fetch next from @mycursor into @myspid
While @@fetch_status=0
Begin
--select @myspid
Set @SQLStr='kill '+cast(@myspid as char(3))
Execute sp_executesql @SQLStr
Fetch next from @mycursor into @myspid
End
Close @mycursor
Deallocate whocursor
四、还原Full完整备份
Declare @FullFileName Varchar(200)
Set @FullFileName='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_FULL20080914.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL',@Filename=@FullFileName,
@with='Move "PAL_COMPSN_P80_Data" to "D:/PALFA/DataBase/PAL/PAL.MDF"',
@with='Move "PAL_COMPSN_P80_Log" to "D:/PALFA/DataBase/PAL/PAL.LDF"',
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
执行完后就会在DB中出现一个备份/只读的PAL数据库。如果之前DB中存在一个可读写的PAL数据库,只需要更改语句为:
Declare @FullFileName Varchar(200)
Set @FullFileName='""172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_FULL20080914.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL',@Filename=@FullFileName,
@with='Move "PAL_Data" to "D:/PALFA/DataBase/PAL/PAL.MDF"',
@with='Move "PAL_Log" to "D:/PALFA/DataBase/PAL/PAL.LDF"',
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"',
@with='replace'
只是在结尾添加 ,@with='replace'这一句。
五、还原Diff差异备份
Declare @FullFileName Varchar(200)
Set @FullFileName='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_DIFF20080919.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL',@Filename=@FullFileName,
@with='Move "PAL_COMPSN_P80_Data" to "D:/PALFA/DataBase/PAL/PAL.MDF"',
@with='Move "PAL_COMPSN_P80_Log" to "D:/PALFA/DataBase/PAL/PAL.LDF"',
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
---@with='recovery'
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
替换为@with='recovery'代表还原后数据库可读写
六、还原Log日志备份
1)Litespeed还原Log
Declare @FileNum int
SET @FileNum=5
Declare @FullFileName Varchar(200)
Set @FullFileName= '//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_LOG20080918.TRN'
While @FileNum<=17
BEGIN
EXEC master.dbo.xp_restore_log
@database='PAL',@Filename=@FullFileName,
@filenumber=@FileNum,
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
SET @FileNum=@FileNum+1
END
2)SQL还原Log
Declare @i int
Set @i=5
while @i<18
Begin
Restore log PAL from disk='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_LOG20080918.TRN'
with STANDBY='D:/PALFA/DataBase/PAL/UNDO_PAL.DAT',file=@i
Set @i=@i+1
End
最后一个LOG
Restore log PAL from disk='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_LOG20080918.TRN '
with recovery,file=18 ---最大的POSITION
这样还原后数据库可读写
七、Log时间点还原(SQL及Litespeed两种方式)
现在要求还原到2008-09-17 16:45:00:000 ,假设Log是每小时备份一次.那么首先应该还原日志到FileNum 为17(2008-09-17 16:00:00:000 ),然后用这个17号FileNum 做Point还原
1)Litespeed Point还原
Declare @FileNum int
SET @FileNum=18
Declare @FullFileName Varchar(200)
Set @FullFileName= '//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_LOG20080917.TRN'
EXEC master.dbo.xp_restore_log
@database='PAL',@Filename=@FullFileName,@WITH='RECOVERY',
@filenumber=@FileNum,
@with='STOPAT="2008-09-17 16:30:00.000"'
2)SQL Point还原
Restore log PAL from disk='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_LOG20080918.TRN'
with file=18,RECOVERY,STOPAT = '2008-09-17 16:30:00.000'
八,映射账户
在PAL db执行下面语句后就可以把之前UID为qmsuser和sdsuser赋之前相对应的权限。
sp_change_users_login 'Update_One','qmsuser','qmsuser'
sp_change_users_login 'Update_One','sdsuser','sdsuser'
五、还原Diff差异备份回出问题
Declare @FullFileName Varchar(200)
Set @FullFileName='//172.26.40.6/d$/PALBackUp/PAL_COMPSN_P80_DIFF20080919.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL',@Filename=@FullFileName,
@with='Move "PAL_COMPSN_P80_Data" to "D:/PALFA/DataBase/PAL/PAL.MDF"',
@with='Move "PAL_COMPSN_P80_Log" to "D:/PALFA/DataBase/PAL/PAL.LDF"',
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
---@with='recovery'
@with='standby="D:/PALFA/DataBase/PAL/UNDO_PAL.DAT"'
替换为@with='recovery'代表还原后数据库可读写
1. Full 还原
Declare @FullFileName Varchar(200)
Set @FullFileName='e:/BackUp/172.17.0.8/PAL/PAL_FULL20080914.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL_LORI',@Filename=@FullFileName,
@with='Move "PAL" to "e:/Restore/PAL_LORI/PAL_LORI_Data.MDF"',
@with='Move "PAL_log" to "e:/Restore/PAL_LORI/PAL_LORI_LOG.LDF"',
@with='standby="e:/Restore/PAL_LORI/UNDO_PAL_LORI.DAT"'
2. Diff 还原
Declare @FullFileName Varchar(200)
Set @FullFileName='e:/BackUp/172.17.0.8/PAL/PAL_DIFF20080917.BAK'
EXEC master.dbo.xp_restore_database
@database='PAL_LORI',@Filename=@FullFileName,
@with='Move "PAL" to "e:/Restore/PAL_LORI/PAL_LORI_Data.MDF"',
@with='Move "PAL_Log" to "e:/Restore/PAL_LORI/PAL_LORI_LOG.LDF"'
,@with='standby="e:/Restore/PAL_LORI/UNDO_PAL_LORI.DAT"'
3. Log 还原
declare @FileNum int
SET @FileNum=5
while @FileNum<=17
BEGIN
EXEC master.dbo.xp_restore_log
@database='PAL_LORI',@Filename=@FullFileName,
@filenumber=@FileNum,
@with='standby="e:/Restore/PAL_LORI/UNDO_PAL_LORI.DAT"'
SET @FileNum=@FileNum+1
END
4. Point 还原
现在要求还原到2008-09-17 16:45:00:000 ,那么首先应该还原日志到FileNum 为17,然后用这个18号FileNum 做Point还原
declare @FileNum int
SET @FileNum=18
Declare @FullFileName Varchar(200)
Set @FullFileName='e:/BackUp/172.17.0.8/PAL/PAL_LOG20080917.TRN'
EXEC master.dbo.xp_restore_log
@database='PAL_LORI',@Filename=@FullFileName,@WITH='RECOVERY',
@filenumber=@FileNum,
@with='STOPAT="2008-09-17 16:30:00.000"'
Restore: (If the files have the same name)
exec master.dbo.xp_restore_database
@database = 'dbname',
@filename = 'path to backup filename'
Restore with move: (If database does not exist and you are moving the datafiles to a new location)
exec master.dbo.xp_restore_database
@database = 'dbname',
@filename = 'path to backup filename',
@with = 'move "logical filename" to "physical file location.mdf"',
@with = 'move "logical filename" to "physical file location.ldf"'
Restore with replace: (If database does exist and you are restoring files with a different name)
exec master.dbo.xp_restore_database
@database = 'dbname',
@filename = 'path to backup filename',
@with = 'replace',
@with = 'move "logical filename" to "physical file location.mdf"',
@with = 'move "logical filename" to "physical file location.ldf"'
**Note in both the restore with move and restore with replace you move the logical name. To find this out run:
exec master.dbo.xp_restore_filelistonly
@filename ='path to backup file'
Restore single backup from a backup set (Multiple backups written to same file)
**NOTE: If you restore without specifying a file number, by default it will restore the FIRST backup in the set.**
To find the file number:
EXEC master.dbo.xp_restore_headeronly
@filename = 'path to backup filename'
Scroll over to right and look for column BackupStartDate to verify the backup you want.
To restore:
exec master.dbo.xp_restore_database
@database = 'dbname',
@filename = 'path to backup file' ,
@file number =2