简体   繁体   English

使用.bak复制SQL Server数据库

[英]Copy SQL server database with .bak

I'm trying to copy a SQL Server database in VBA (MS-ACCESS). 我正在尝试在VBA(MS-ACCESS)中复制SQL Server数据库。

So I got this query to create the .bak 所以我得到了这个查询来创建.bak

DECLARE @name VARCHAR(50) -- database name  
DECLARE @path VARCHAR(256) -- path for backup files  
DECLARE @fileName VARCHAR(256) -- filename for backup  
DECLARE @fileDate VARCHAR(20) -- used for file name


-- specify database backup directory
SET @path = 'D:\Backup\'  


-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 


DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM master.dbo.sysdatabases 
WHERE name NOT IN ('master','model','msdb','tempdb')  -- exclude these databases


OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   


WHILE @@FETCH_STATUS = 0   
BEGIN   
       SET @fileName = @path + @name + '.BAK'  
       BACKUP DATABASE @name TO DISK = @fileName  


       FETCH NEXT FROM db_cursor INTO @name   
END   


CLOSE db_cursor   
DEALLOCATE db_cursor  

then I got this one to restore the database 然后我得到了这个来恢复数据库

RESTORE DATABASE SMD
FROM DISK='\\SRV_FILESMTL\SMD\SMD.bak'
WITH 
MOVE 'smd' TO 'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\SMD.mdf',
MOVE 'smd_log' TO 'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\SMD_log.ldf',
REPLACE

They are both called in the main form on a button click like this 它们都是按照这样的按钮单击在主窗体中调用的

On Error GoTo Error
    Dim os As String

    lblWait.Visible = True
    Me.Repaint

    CurrentDb.Execute ("qryToBackupServer")
    CurrentDb.Execute ("qryToRestoreDataW7")


    MsgBox ("Data imported successfully")
    lblWait.Visible = False
    Exit Sub
Error:
        MsgBox Err.Description
        lblWait.Visible = False
        Exit Sub

I thought it worked but I realized that the new data is not imported. 我认为它有效,但我意识到新数据没有导入。 So I checked if the .bak is created correctly and yes because if I copy the .bak on my computer and restore it using SQL Management studio, it works. 所以我检查了.bak是否正确创建是的,因为如果我在我的计算机上复制.bak并使用SQL Management studio恢复它,它就可以了。

So is there a problem with my query to restore? 我的查询还原有问题吗? There's no error, but is it supposed to take 30 secondes because when I copied the .bak, it was 5go and it took 5 minutes? 没有错误,但它应该需要30个secondes,因为当我复制.bak时,它是5go并且需要5分钟?

I was thinking maybe the database is locked because I call those functions in Access with tables that are linked to this database, but I got no error. 我想也许数据库是锁定的,因为我在Access中使用链接到此数据库的表调用这些函数,但我没有错误。

is there a problem because I get the .bak from another server? 是否有问题,因为我从另一台服务器获得.bak?

Thank you 谢谢

I found out that size of the .bak file was increasing every time I do a backup. 我发现每次备份时.bak文件的大小都在增加。 So I deleted it and I made another one. 所以我删除了它,我又做了一个。 It was 33 000 ko instead of 4go... 它是33 000 ko而不是4go ...

Now the backup works. 现在备份工作。

So in VBA, I delete the file before to do the backup. 所以在VBA中,我先删除文件进行备份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM