三步完成,sql server 2014 实例迁移

要点:

SQL Server 2012 无人值守安装(添加新实例)

http://blog.csdn.net/wangzhpwang/article/details/40739533

  1. sql server  版本要一致
  2. 实例安装目录最好要一致
  3. 检查原sql server安装目录下log目录下的ConfigurationFile.ini获取必要参数
  4. 尝试1,尝试通过修改ConfigurationFile.ini进行静默安装
  5. 新旧服务器通过共享目录传输文件
  6. powershell 运行策略正确设置
  7. 设置共享目录everyone完全控制权限,以便新server能访问到bak文件
在原实例master上创建备份SP,@type=1我们只演示迁移系统数据库,若要备份全部库,请在sqlcmd 调用sp_backupdatabase时改为type=0
create proc sp_backupdatabase
 @bak_path nvarchar(4000)=''       --备份路径;
,@baktype int = null               --备份类型为全备,1为差异备,2为日志备份
,@type int = null                  --设置需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库;
,@dbnames nvarchar(4000)=''        --需要备份或排除的数据库,用,隔开,当@type=3或4时生效
,@overdueDay int = null            --设置过期天数,默认天;
,@compression int =0               --是否采用sql2008的压缩备份,0为否,1为采用压缩
as
--sql server 2005/2008备份/删除过期备份T-sql 版本v1.0
/*
author:perfectaction
date  :2009.04
desc  :适用于sql2005/2008备份,自动生成库文件夹,可以自定义备份类型和备份库名等
      可以自定义备份过期的天数
              删除过期备份功能不会删除最后一次备份,哪怕已经过期
              如果某库不再备份,那么也不会再删除之前过期的备份 
       如有错误请指正,谢谢.
*/

set nocount on
--开启xp_cmdshell支持
exec sp_configure 'show advanced options', 1
reconfigure with override
exec sp_configure 'xp_cmdshell', 1 
reconfigure with override
exec sp_configure 'show advanced options', 0
reconfigure with override
print char(13)+'------------------------'

--判断是否填写路径
if isnull(@bak_path,'')=''
    begin
        print('error:请指定备份路径')
        return 
    end

--判断是否指定需要备份的库
if isnull(ltrim(@baktype),'')=''
    begin
        print('error:请指定备份类型aa:0为全备,1为差异备,2为日志备份')
        return 
    end
else
    begin
        if @baktype not between 0 and 2
        begin
            print('error:指定备份类型只能为,1,2:  0为全备,1为差异备,2为日志备份')
            return 
        end
    end
--判断是否指定需要备份的库
if isnull(ltrim(@type),'')=''
    begin
        print('error:请指定需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库')
        return 
    end
else
    begin
        if @type not between 0 and 4
        begin
            print('error:请指定需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库')
            return 
        end
    end

--判断指定库或排除库时,是否填写库名
if @type>2
    if @dbnames=''
    begin
        print('error:备份类型为'+ltrim(@type)+'时,需要指定@dbnames参数')
        return 
    end

--判断指定指定过期时间
if isnull(ltrim(@overdueDay),'')=''
begin
    print('error:必须指定备份过期时间,单位为天,0为永不过期')
    return 
end

--判断是否支持压缩
if @compression=1 
    if charindex('2008',@@version)=0 or charindex('Enterprise',@@version)=0
    begin
        print('error:压缩备份只支持sql2008企业版')
        return 
    end

--判断是否存在该磁盘
declare @drives table(drive varchar(1),[size] varchar(20))
insert into @drives exec('master.dbo.xp_fixeddrives')
if not exists(select 1 from @drives where drive=left(@bak_path,1))
    begin
        print('error:不存在该磁盘:'+left(@bak_path,1))
        return 
    end

--格式化参数
select @bak_path=rtrim(ltrim(@bak_path)),@dbnames=rtrim(ltrim(@dbnames))
if right(isnull(@bak_path,''),1)!='\' set @bak_path=@bak_path+'\'
if isnull(@dbnames,'')!='' set @dbnames = ','+@dbnames+','
set @dbnames=replace(@dbnames,' ','')

--定义变量
declare @bak_sql nvarchar(max),@del_sql nvarchar(max),@i int,@maxid int
declare @dirtree_1 table (id int identity(1,1) primary key,subdirectory nvarchar(600),depth int,files int)
declare @dirtree_2 table (id int identity(1,1) primary key,subdirectory nvarchar(600),depth int,files int,
dbname varchar(300),baktime datetime,isLastbak int)
declare @createfolder nvarchar(max),@delbackupfile nvarchar(max),@delbak nvarchar(max)

--获取需要备份的库名--------------------start
declare @t table(id int identity(1,1) primary key,name nvarchar(max))
declare @sql nvarchar(max)
set @sql = 'select name from sys.databases where state=0 and name!=''tempdb''  '
    + case when @baktype=2 then ' and recovery_model!=3 ' else '' end
    + case @type when 0 then 'and 1=1'
        when 1 then 'and database_id<=4'
        when 2 then 'and database_id>4'
        when 3 then 'and charindex('',''+Name+'','','''+@dbnames+''')>0'
        when 4 then 'and charindex('',''+Name+'','','''+@dbnames+''')=0 and database_id>4'
        else '1>2' end
insert into @t exec(@sql)
--获取需要备份的库名---------------------end

--获取需要创建的文件夹------------------start
insert into @dirtree_1 exec('master.dbo.xp_dirtree '''+@bak_path+''',0,1')
select @createfolder=isnull(@createfolder,'')+'exec master.dbo.xp_cmdshell ''md '+@bak_path+''+name+''',no_output '+char(13)
from @t as a left join @dirtree_1 as b on a.name=b.subdirectory and b.files=0 and depth=1 where  b.id is null
--获取需要创建的文件夹-------------------end


--生成处理过期备份的sql语句-------------start
if @overdueDay>0
begin
    insert into @dirtree_2(subdirectory,depth,files) exec('master.dbo.xp_dirtree '''+@bak_path+''',0,1')
    if @baktype =0 
    delete from @dirtree_2 where depth=1 or files=0 or charindex('_Full_bak_',subdirectory)=0 
    if @baktype =1 
    delete from @dirtree_2 where depth=1 or files=0 or charindex('_Diff_bak_',subdirectory)=0 
    if @baktype=2
    delete from @dirtree_2 where depth=1 or files=0 or charindex('_Log_bak_',subdirectory)=0 
    if exists(select 1 from @dirtree_2)
    delete from @dirtree_2 where isdate(
            left(right(subdirectory,19),8)+' '+ substring(right(subdirectory,20),11,2) + ':' +  
            substring(right(subdirectory,20),13,2) +':'+substring(right(subdirectory,20),15,2) 
            )=0
    if exists(select 1 from @dirtree_2)
    update @dirtree_2 set dbname = case when @baktype=0 then left(subdirectory,charindex('_Full_bak_',subdirectory)-1)
        when @baktype=1 then left(subdirectory,charindex('_Diff_bak_',subdirectory)-1) 
        when @baktype=2 then left(subdirectory,charindex('_Log_bak_',subdirectory)-1) 
        else '' end    
        ,baktime=left(right(subdirectory,19),8)+' '+ substring(right(subdirectory,20),11,2) + ':' +  
            substring(right(subdirectory,20),13,2) +':'+substring(right(subdirectory,20),15,2) 
    from @dirtree_2 as a
    delete @dirtree_2 from @dirtree_2 as a left join @t as b on b.name=a.dbname where b.id is null
    update @dirtree_2 set isLastbak= case when (select max(baktime) from @dirtree_2 where dbname=a.dbname)=baktime 
    then 1 else 0 end from @dirtree_2 as a
    select @delbak=isnull(@delbak,'')+'exec master.dbo.xp_cmdshell ''del '+@bak_path+''+dbname+'\'
    +subdirectory+''',no_output '+char(13) from @dirtree_2 where isLastbak=0 and datediff(day,baktime,getdate())>@overdueDay
end
--生成处理过期备份的sql语句--------------end




begin try
    print(@createfolder)  --创建备份所需文件夹
    exec(@createfolder)   --创建备份所需文件夹
end try
begin catch
    print 'err:'+ltrim(error_number())
    print 'err:'+error_message()
    return
end catch


select @i=1 ,@maxid=max(id) from @t
while @i<=@maxid
begin
    select @bak_sql=''+char(13)+'backup '+ case when @baktype=2 then 'log ' else 'database ' end
            +quotename(Name)+' to disk='''+@bak_path + Name+'\'+
            Name+ case when @baktype=0 then '_Full_bak_' when @baktype=1 then '_Diff_bak_' 
            when @baktype=2 then '_Log_bak_' else null end + case when @compression=1 then 'compression_' else '' end+
            replace(replace(replace(convert(varchar(20),getdate(),120),'-',''),' ','_'),':','')+
            case when @baktype=2 then '.trn' when @baktype=1 then '.dif' else '.bak' end +'''' 
            + case when @compression=1 or @baktype=1 then ' with ' else '' end
            + case when @compression=1 then 'compression,' else '' end
            + case when @baktype=1 then 'differential,' else '' end
            + case when @compression=1 or @baktype=1 then ' noformat' else '' end 
    from @t where id=@i
    set @i=@i+1
    begin try
        print(@bak_sql)--循环执行备份
        exec(@bak_sql) --循环执行备份
    end try
    begin catch
        print 'err:'+ltrim(error_number())
        print 'err:'+error_message()
    end catch
end

begin try
    print(@delbak)   --删除超期的备份
    exec(@delbak)    --删除超期的备份
end try
begin catch
    print 'err:'+ltrim(error_number())
    print 'err:'+error_message()
end catch


--关闭xp_cmdshell支持
--exec sp_configure 'show advanced options', 1
--reconfigure with override
--exec sp_configure 'xp_cmdshell', 1 
--reconfigure with override
--exec sp_configure 'show advanced options', 0
--reconfigure with override
--exec sp_backupdatabase 'D:\backup',0,1,'',0,0



D:\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA

原机器上运行powershell脚本,进行备份并把备份复制到新server共享目录上
cls
$sdt='2016-09-27 10:07'
while($(get-date -format 'yyyy-MM-dd HH:mm') -eq $sdt){
    cls;
    "Time is now $(get-date ).Process will start at $sdt"
    sleep 10
} 
"my job here" 

if (Test-Path 'D:\backup_migration'){
    cd 'D:\backup_migration'
    ls *.* -Recurse|%{Remove-Item $_.fullname  -Force -ErrorAction Stop}
}
cd c:/
cmd.exe /c 'sqlcmd.exe -S"192.168.10.101" -Usa -Pxxxxxxxxx -dmaster -Q"set nocount on ;exec sp_backupdatabase ''D:\backup_migration'',0,1,'''',0,0"'

"closing mssqlserver"
sleep 10
#if($(Get-Service "mssqlserver").Status -eq 'Running'){$(Get-Service "mssqlserver").Status;Get-Service "mssqlserver"|Stop-Service -Force}

$target='\\192.168.10.151\Backup\migrate'
if(Test-Path $target){
    cd $target
    ls *.* -Recurse|%{Remove-Item $_.fullname  -Force -ErrorAction Stop} 
}else{
    New-Item $target -ItemType directory
    cd $target
}

Copy-Item -Path 'D:\backup_migration' -Destination $target -Recurse  -Force

ls "D:\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn\mssqlsystemresource.*"|%{Copy-Item -Path $_.FullName -Destination $target -Recurse }
""|out-file "$target\mark.txt"
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
#$wmi.EnableStatic("192.168.1.152", "255.255.255.0")
#$wmi.SetGateways("192.168.1.1", 1)
#$wmi.SetDNSServerSearchOrder("8.8.8.8")
$wmi.IPAddress


新机器上运行powershell脚本,还原刚才的备份文件到新sql实例
cls

$folder='D:\Backup\migrate'

$f_path="$folder\mark.txt"
while(!$(test-path $f_path)){
    cls;
    "Time is now $(get-date ).Waiting for $f_path"
    sleep 10
} 
remove-item -path $f_path
"my job here" 

cd $folder 
Get-Service "mssqlserver"|Stop-Service -Force
1..2|%{ ""|out-file "$_.sql"}
ls *.bak -Recurse |%{    
    $dbname= $_.Directory.Name
    $fname=$_.FullName
    $sqlf="restore database $dbname from disk=""$fname"" with replace"
    $sqlf+= "
go"
 $sqlf+= "
waitfor delay '0:0:5'
go
"
    $sqlf
    if( $dbname -eq 'master'){$sqlf|Out-File 1.SQL -Append }ELSE{$sqlf|Out-FILE 2.SQL -Append}
}

cmd.exe /c 'net start mssqlserver /m'
cmd.exe /c "sqlcmd.exe -S""192.168.10.151"" -Usa -Pxxxxxxxxxxxx -i""$folder\1.sql"""
sleep 10
if($(Get-Service "mssqlserver").Status -eq 'Running'){$(Get-Service "mssqlserver").Status;Get-Service "mssqlserver"|Stop-Service -Force}

ls mssqlsystemresource.* |%{Copy-Item -Path $_.FullName -Destination "D:\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn\" -Force }

cmd.exe /c 'net start mssqlserver '
cmd.exe /c "sqlcmd.exe -S""192.168.10.151"" -Usa -Pxxxxxxxxxxxx -i""$folder\2.sql"""

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
#$wmi.EnableStatic("192.168.1.101", "255.255.255.0")
#$wmi.SetGateways("192.168.1.1", 1)
#$wmi.SetDNSServerSearchOrder("8.8.8.8")
$wmi.IPAddress

完。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于将 SQL Server 数据库备份迁移的问题,您可以按照以下步骤进行操作: 1. 首先,确保您已经有了数据库的备份文件(.bak 文件)。如果没有备份文件,您可以使用 SQL Server Management Studio (SSMS) 或者 Transact-SQL 命令来创建一个数据库备份。 2. 将备份文件从源服务器复制到目标服务器。您可以使用文件共享、FTP、云存储等方式进行复制。 3. 在目标服务器上,打开 SSMS 连接到 SQL Server 实例。 4. 在 SSMS 中,右键单击 "数据库" 节点,选择 "还原数据库"。 5. 在 "还原数据库" 窗口中,选择 "设备" 选项卡,并单击 "..." 按钮。 6. 在 "选择备份设备" 窗口中,单击 "添加",然后浏览到您复制到目标服务器的备份文件。 7. 选择要还原的备份文件,并在 "还原到" 部分指定新数据库的名称和位置。 8. 确保 "恢复" 选项卡中选择了正确的恢复操作。通常情况下,选择 "覆盖现有数据库(WITH REPLACE)"。 9. 单击 "确定" 开始还原操作。 10. 等待还原过程完成。一旦还原完成,您的数据库将在目标服务器上可用。 请注意,在执行数据库还原操作之前,请确保目标服务器上没有同名的数据库,或者您已经备份了目标服务器上的数据库。此外,还原操作可能需要一些时间,具体取决于备份文件的大小和网络速度。 希望这些步骤能够帮助您成功迁移 SQL Server 数据库备份!如果您有任何更多的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值