简体   繁体   English

将SQL Server Express数据库复制到另一台计算机

[英]Copy SQL Server Express Database to Another Computer

I am upsizing the back end of a client's Access app tomorrow and need to be ready with a plan for a particular problem with this client. 我明天正在升级客户端Access应用程序的后端,需要准备好针对此客户端的特定问题的计划。 The boss needs to be able to take the data file away from the office where the SQL Server is on a regular basis (usually monthly) and run statistical analyses on the data. 老板需要能够定期(通常是每月)将数据文件从SQL Server的办公室带走,并对数据进行统计分析。

I've looked at Easiest way to copy an entire SQL server Database from a server to local SQL Express , and the solutions there don't work in this scenario because: 我已经研究了将整个SQL Server数据库从服务器复制到本地SQL Express的最简单方法 ,并且那里的解决方案在这种情况下不起作用,因为:

  1. can't use a one-time solution (Database Publishing Wizard), as this needs to be scriptable. 不能使用一次性解决方案(数据库发布向导),因为这需要是可编写脚本的。

  2. can't use any method that depends on the two computers being networked, as that is not an option -- data has to be transferred via USB thumb drive (so, no replication). 不能使用依赖于联网的两台计算机的任何方法,因为这不是一个选项 - 数据必须通过USB拇指驱动器传输(因此,没有复制)。

  3. can't use any method that depends on running SQL Server management tools from the server console or from the workstation for the backup. 不能使用任何依赖于从服务器控制台或工作站运行SQL Server管理工具进行备份的方法。

  4. can't connect to the SQL Server database directly for the analysis because the data has to be portable to other locations. 无法直接连接到SQL Server数据库进行分析,因为数据必须可移植到其他位置。

What I think I need is some way to call a script that creates a backup file, then copy the result to the USB drive. 我认为我需要的是调用创建备份文件的脚本,然后将结果复制到USB驱动器的某种方法。 I then need a second script to copy the backup file from the USB drive and restore it onto the other SQL Server. 然后我需要第二个脚本从USB驱动器复制备份文件并将其还原到另一个SQL Server。

The data being transported is read-only (or, any changes made don't need to get back to the main server), and data won't be updated in the second location. 传输的数据是只读的(或者,所做的任何更改都不需要返回主服务器),并且不会在第二个位置更新数据。 It's currently scripted with a plain old batch file to copy the back-end MDB file, and I need something that is as simple for the user. 它目前用一个普通的旧批处理文件编写脚本来复制后端MDB文件,我需要一些对用户来说简单的东西。

It can't have any dependencies on, say, Powershell (of SQL Server Management Studio), because I don't want it to have to be installed on the computer the user is running the script from (there are a half dozen workstations the script needs to be runnable from, and I don't want to have to install something on all of those). 它不能依赖于Powershell(SQL Server Management Studio),因为我不希望它必须安装在用户运行脚本的计算机上(有六个工作站,脚本需要可以运行,我不想在所有这些上安装一些东西)。

I'm going to be setting up the backup agent to create a backup every night, so I could perhaps copy that file without needing to initiate the backup before copying. 我将设置备份代理以每晚创建备份,因此我可以复制该文件,而无需在复制之前启动备份。 Thus I might only need to script the restore on the target computer. 因此,我可能只需要在目标计算机上编写恢复脚本。

Thoughts, suggestions, pointers? 思想,建议,指针?

You should definitely be able to create something like that. 你绝对应该能够创造类似的东西。

One part would be a T-SQL CREATE BACKUP script as a .sql script, and execute that from a standard Windows batch ( *.bat ) or command ( *.cmd ) file using the sqlcmd command line tool. 一部分是作为.sql脚本的T-SQL CREATE BACKUP脚本,并使用sqlcmd命令行工具从标准Windows批处理( *.bat )或命令( *.cmd )文件执行该脚本。

That would be something like this: 那将是这样的:

backup.sql backup.sql

BACKUP DATABASE YourDatabase
TO DISK = 'Z:\Backup\YourDatabase.bak'
WITH FORMAT;

The second part would be a .sql file with a T-SQL RESTORE script, basically reading the data from a given location on disk and restoring it to that SQL Server instance there. 第二部分是带有T-SQL RESTORE脚本的.sql文件,基本上从磁盘上的给定位置读取数据并将其恢复到那里的SQL Server实例。

restore.sql restore.sql

RESTORE DATABASE YourDatabase
   FROM AdventureWorks2008R2Backups 
   WITH 
     MOVE 'YourDatabase_Data' TO 'C:\MSSQL\Data\YourDatabase.mdf',
     MOVE 'YourDatabase_Log' TO 'C:\MSSQL\Data\YourDatabase_Log.ldf';
GO

Of course, you need to adapt those names and paths to your own actual requirements - but that should just give you a hint how to get started with this endeavor. 当然,您需要根据自己的实际要求调整这些名称和路径 - 但这应该只是为您提供如何开始这项工作的提示。

To execute one of those .sql script using sqlcmd , you need something like: 要使用sqlcmd执行其中一个.sql脚本,您需要以下内容:

sqlcmd -S (name of server) -U (login) -P (password) -I (name of script file)

eg 例如

sqlcmd -S (local) -U someuser -P top$secret -I backup.sql

Resources: 资源:

I had this same issue transporting db between production (server) and test-development (local in another location) and also transporting finished db to hosted server. 我有同样的问题,在生产(服务器)和测试开发(本地在另一个位置)之间传输数据库,并将完成的数据库传输到托管服务器。

As it turned out I can just transport the .MDF by itself. 事实证明,我可以自己运输.MDF。

  1. Make sure target db does not have that db attached, delete it first in SSMS. 确保目标db没有附加该数据库,首先在SSMS中将其删除。
  2. Move the .MDF by itself (without the log file). 单独移动.MDF(不带日志文件)。
  3. At target location default c:\\program files...sql..\\DATA make sure all prior instance of MDF and LDF are moved or deleted -- if it sees an .ldf there it gets confused. 在目标位置默认c:\\ program files ... sql .. \\ DATA确保移动或删除MDF和LDF的所有先前实例 - 如果它看到.ldf那里就会混淆。
  4. In SSMS, choose to attach. 在SSMS中,选择附加。 Press Add, select the .mdf. 按添加,选择.mdf。
  5. At this point, in the box right below, it will show that an MDF and LDF are attached and that the LDF is missing. 此时,在右下方的框中,将显示MDF和LDF已连接且缺少LDF。 Click the LDF and press the REMOVE button. 单击LDF并按REMOVE按钮。
  6. Now the MDF will be attached and a new/empty LDF will be added. 现在将附加MDF并添加新的/空的LDF。

I do this all the time; 我一直这样做; works perfectly -- saves the trouble of transporting a large .ldf. 完美运行 - 省去了运输大型.ldf的麻烦。 (I often use dropbox.com instead of a thumb drive and encrypt the file with PKZIP/SecureZip first.) (我经常使用dropbox.com而不是拇指驱动器,并首先用PKZIP / SecureZip加密文件。)

I'm not sure what happens if you copy the MDF with the server started, though I do not stop it before copying. 我不确定如果你在服务器启动的情况下复制MDF会发生什么,虽然我没有在复制之前停止它。 I'm not sure how to make this scriptable -- the attach can be scriptable but I'm not sure about deleting the .LDF before proceeding. 我不确定如何使这个脚本化 - 附加可以编写脚本,但我不确定在继续之前删除.LDF。

You could script the server to make a snapshot copy of the database, then transport that MDF knowing that it was not being updated. 您可以编写服务器脚本以生成数据库的快照副本,然后传输该MDF,因为它知道它没有被更新。

Another idea - write a program to insert all the records into a SQL-Server compact edition file and transport that? 另一个想法 - 写一个程序将所有记录插入到SQL-Server紧凑版文件中并传输它? I've not tried it, but read about it. 我没试过,但是读了一下。

Just a thought but if he currently goes away with a copy of a MDB file with all the data in just for himself then why not carry on like that. 只是一个想法,但如果他现在带着一个MDB文件的副本,其中包含所有数据,那么为什么不继续这样做。 You could just do it all in access with a query for each table. 您可以通过查询每个表来访问所有内容。

If you setup your “reporting” MDB with the tables linked and called “tblFoo_LINKED” and a local table called “tblFoo” you could then just run a bit of VBA code that would loop through all the tables and execute something like 如果您使用链接的表格“tblFoo_LINKED”和名为“tblFoo”的本地表设置“报告”MDB,那么您可以运行一些VBA代码,这些代码将循环遍历所有表并执行类似的操作

INSERT INTO tblFoo SELECT * FROM tblFoo_LINKED

The SQL server backup restore would still be the option I would go for but just offering a different spin on things which might fit your needs SQL服务器备份恢复仍然是我想要的选择,但只是提供可能适合您需求的不同内容

Why not continue copying the entire database file? 为什么不继续复制整个数据库文件? This reads like a small enough operation to get away with a momentary pause: You could shrink the database, detach, and copy the files directly. 这看起来像一个足够小的操作,可以暂时停止:您可以缩小数据库,分离并直接复制文件。 On the target system, you could attach the SQLExpress DB by file name in the connection string. 在目标系统上,您可以在连接字符串中按文件名附加SQLExpress DB

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

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