如何从类似于mysql的命令行备份/还原SQL Server数据库


8

我需要自动从旧服务器迁移某些数据库。我知道可以使用C#作为工具编写脚本,但是我需要一个简单,快速且有效的解决方案来构建批处理文件来完成这项工作。

Answers:


9

使用备份/还原语句创建一个.sql文件并使用 SQLCMD

在该文章的大约一半处,有一个进行备份的示例。

您可以参考以下页面,其中包含有关T-SQL的信息以进行备份和还原:


您可以在批处理文件中调用上面的Sqlcmd。这将使使用更具说服力,同时可以根据需要分配不同的服务器名和用户名。
2012年

2

由于您必须将数据库从旧服务器迁移到新服务器,因此以下是将其自动化的脚本..

重要提示:在TEST服务器上进行测试之前,请务必先进行测试,以使您了解脚本的功能……对于任何数量的数据丢失,我不承担任何责任!

set nocount on
/****************************************************************************** 
    Author  :: Kin
    Desc    :: Transfer Logins, Databases from one instance/Server to another
*******************************************************************************/
  declare @datafile varchar(255),
            @logfile varchar(255),
            @dbid tinyint,
            @SQLText varchar(8000),
            @dbname varchar(255),
            @destserver varchar(255),
            @SQLText2 varchar(8000)
set @destserver ='' --Destination Server Name goes here.

--1.Transfer Logins
select @SQLText='exec master..xp_cmdshell ''sqlcmd -S'+@@servername+' -E -Q"execute master.dbo.sp_help_revlogin" -oD:\logs\revloginout.sql'''
--print @sqltext
exec (@sqltext)
-- Create on Destination Server.
select @SQLText='exec master..xp_cmdshell ''sqlcmd -S'+@destserver+' -E -iD:\logs\revloginout.sql'''
--print @sqltext
exec (@sqltext)

--2. Detach All Local Databases and prepare for Attach on dest.
 --- if you want to filer only some database, then you can do it here !!
if exists(select 1 from tempdb..sysobjects where name like '%#filetable%')
      begin
      drop table #filetable
      end
      create table #filetable (mdf varchar(255),ldf varchar(255),dbid tinyint,dbname varchar(100),fileid tinyint)
      --

      insert #filetable (mdf,dbid,fileid) 
      select physical_name,database_id,data_space_id from sys.master_files where data_space_id=1

      insert #filetable (ldf,dbid,fileid) 
      select physical_name,database_id,data_space_id from sys.master_files where data_space_id=0

      update u 
      set u.dbname = s.name
      from #filetable u
      inner join master..sysdatabases s 
      on u.dbid = s.dbid


select @dbid = min(dbid) from #filetable where dbid > 4
while @dbid is not null
begin

      select @SQLText = 'alter database '+ dbname from #filetable where dbid = convert(varchar,@dbid) 
      select @SQLText = @SQLText+' set single_user with rollback immediate'
      select @SQLText = @SQLText+' exec master..sp_detach_db ' + dbname from #filetable where dbid = convert(varchar,@dbid)
      print @SQLText
      Exec(@SQLText)

      select @SQLText2 = 'exec opendatasource(''SQLNCLI'',''Datasource='+@destserver+';Persist Security Info=False;Integrated Security=SSPI'').master.dbo.sp_attach_db '''+dbname+'''' from #filetable where dbid = @dbid
      select @SQLText2= @SQLText2+','''+ mdf+'''' from #filetable where dbid = @dbid and mdf is not null
      select @SQLText2=@SQLText2+','''+ ldf+''''  from #filetable where dbid = @dbid and ldf is not null
      print @SQLText2
      Exec(@SQLText)
      select @dbid = min(dbid) from #filetable where dbid > 4 and dbid > @dbid
end

select * from #filetable
drop table #filetable
--Finally Shutdown SQL Server
shutdown with nowait
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.