首页 » SQL Server » mssql 根据多个备份文件批量还原多个数据库

mssql 根据多个备份文件批量还原多个数据库

如果每周要你从sql server生产库的备份还原到本地备份库,你会怎么做?我有200多个sql server 数据库,那不能一个个恢复,太让费时间,于是我想偷个懒,把所有要恢复的所有项目数据库备份集文件全放到一个目录里,用一个批处理来完成,道理很简单,关键是思路,和我一样有这个麻烦事的可以再改进一下。

开始,要做点前期准备,

1,准备备份文件到一个目录(可以参考上一个文章提取.bak文件)

2,把备份全放到本地备份服务器的硬盘中

3,建一个sql脚本和一个bat批处理,配置一下服务用户信息与路径

先看第一个bat,  连数据库可以用osql 或isql ,有点像oracle 的sqlplus,

–begin————

@echo off
echo   sqlserver自动还原0.1.1
echo
echo 作者:路中一只蜗 qq:397464815
echo                        把一个目录中的多个备份文件还原到指定sql server中,
echo               并指定数据文件还原后的存放路径,库名来源文件名
echo            需要配置sql.bat中的 sqlserver ip   用户名  密码
echo            需要配置restore.sql 中的@backpath、@datapath
echo
echo ‘连接sqlserver 中…’
isql -S 192.168.168.20 -U sa -P 1qaz2wsx -n -d master -i ./restoredb.sql

————-end————————

配置ip,sa 密码,和sql 文件的路径

下面看restoredb.sql 的内容

————begin——————-
declare
@backpath varchar(2000),
@execsql varchar(2000),
@datapath varchar(2000);

begin
–备份文件存放的路径 数据库服务器上的路径
set @backpath=’d:\data\bakfiles’;
–组串找到上面目录中指定的备份文件    以BAK结尾
set @execsql=’dir /w ‘+ @backpath +’\*.BAK’;
–还原后文件存放的路径

set @datapath=’D:\sqlserver\MSSQL\Data’;

print ”
print ‘恢复开始,请耐心等待…’
print ”
print ‘开始把’+@backpath+’里的所有以bak结尾的备份文件还原sqlserver到’+@datapath+’目录下’

–检查临时表是否存在
begin
if not exists (select * from sysobjects where name=’backfiles’)
create table backfiles(name varchar(2000));
end
–清空原数据
truncate table backfiles
–插入临时表
insert into backfiles exec xp_cmdshell @execsql

print ‘插入临时表成功总共插入’+ cast(@@rowcount as varchar)+’条数据’

DELETE backfiles WHERE upper(right(name,3))<>’BAK’ OR NAME IS NULL

print ‘清除成功’+ cast(@@rowcount as varchar)+’条不合法数据’

declare @fileName varchar(100),
@dbName varchar(100)

declare file_cur cursor for select name from backfiles;

open file_cur

fetch next from file_cur
into @fileName

while @@fetch_status=0
begin

select  @dbName=substring(@fileName,1,charindex(‘_db_’,@filename)-1)

print ‘开始恢复’+@dbname+’……’

–restore database begin

set @execsql =’restore database ‘+@dbName+
‘ from disk=”’+@backpath+’\’+@fileName+”’
with move ”’+@dbname+’_data” to ”’+@datapath+’\’+@dbname+’_data.mdf”,move ”’+@dbname+’_log” to ”’+@datapath+’\’+@dbname+’_log.ldf”’

print @execsql

exec (@execsql)

–restore database  end
print ‘已把’+@fileNAME+’恢复到—-‘+@dbName

print ”

fetch next from file_cur
into @fileName

end
close file_cur

deallocate file_cur

end

————end ———————

配置备份集的目录,还原后的目录,库名是从数据库文件名中得到,逻辑文件名需要和数据库名一致(可以查看我原来的一篇修改逻辑文件名的帖子),运行完后查看一下结果。

打赏

目前这篇文章有7条评论(Rss)评论关闭。

  1. cybermagnetik | #1
    2011-05-23 at 03:13

    What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

    • Lena | #2
      2011-06-16 at 15:36

      Now that’s sutlbe! Great to hear from you.

      • Cherlin | #3
        2011-11-11 at 23:53

        So that’s the case? Quite a revaeltion that is.

  2. pharmacist | #4
    2011-01-25 at 01:39

    What a great resource!

    • admin | #5
      2011-01-25 at 13:31

      thanks

      • Barbi | #6
        2011-06-17 at 20:05

        You’re the greaestt! JMHO

        • Gertie | #7
          2011-11-11 at 19:41

          Wait, I cannot fatohm it being so straightforward.