将开发 SQL Server 复制到拇指驱动器的最简单方法

发布于 2024-12-14 12:19:55 字数 96 浏览 1 评论 0原文

我有一个小型 SQL 数据库,仅位于我的开发 PC 上。我想经常将其备份到拇指驱动器上。如果我能让它在检测到拇指驱动器时自动按计划执行,那就更好了。有什么好的方法可以做到这一点?

I have a small SQL database that is on my development PC only. I'd like to make frequent backup copies of it to a thumb drive. If I could have it do it automatically on a schedule when the thumb drive is detected, that'd be even better. What's a good way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝情姑娘 2024-12-21 12:19:56

我这样做是为了让数据库和其他文件夹在每次插入 USB 驱动器时自动同步到 USB 驱动器:

  1. 安装 SyncToy 由 Microsoft 制作。
    • 添加文件夹对以从 DB 文件夹复制到 USB 驱动器。
    • 过滤文件,仅显示您要复制的文件。
  2. 使用以下内容创建一个名为 SyncMe.bat 的批处理程序,并将其保存到 USB 驱动器上
  3. 从控制面板打开事件查看器。
  4. 导航至应用程序和服务日志 >微软> Windows DriverFrameworks-用户模式>操作
  5. 清除日志以便更轻松地找到正确的事件。
  6. 插入 USB 驱动器。
  7. 刷新日志,找到特定于该 USB 驱动器的最新事件,右键单击并将任务附加到此事件。
  8. 添加一个操作来运行您创建的 SyncMe.bat 程序

SyncMe.bat 内容(更新以反映您的路径和文件名):

@echo off

if exist "G:\SyncMe.bat" goto fileexists

goto nofile

:fileexists
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -S computername\instancename -E -n -Q "master..sp_detach_db 'DatabaseName'"
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -S computername\instancename -E -n -Q "master..sp_attach_db 'DatabaseName', 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.InstanceName\MSSQL\DATA\DatabaseName.mdf','C:\Program Files\Microsoft SQL Server\MSSQL10_50.InstanceName\MSSQL\DATA\LogName_log.ldf'"
goto end

:nofile
echo SyncMe.bat not found on G:\
goto end

:end

This is what I did to get the database and other folders to automatically sync to my USB drive every time I plug it in:

  1. Install SyncToy made by Microsoft.
    • Add a folder pair to copy from the DB folder to the USB drive.
    • Filter files to only the files you want to copy.
  2. Create a batch program with the content below called SyncMe.bat and save it on your USB drive
  3. Open Event Viewer from the Control Panel.
  4. Navigate to Applications and Services Logs > Microsoft > Windows DriverFrameworks-UserMode > Operational
  5. Clear the log to make it easier to find the right events.
  6. Plug in the USB drive.
  7. Refresh the log, find the latest event that is specific to that USB drive, right-click, and Attach Task To This Event.
  8. Add an action that runs the SyncMe.bat program you created

SyncMe.bat contents (update to reflect your paths and file names):

@echo off

if exist "G:\SyncMe.bat" goto fileexists

goto nofile

:fileexists
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -S computername\instancename -E -n -Q "master..sp_detach_db 'DatabaseName'"
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -S computername\instancename -E -n -Q "master..sp_attach_db 'DatabaseName', 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.InstanceName\MSSQL\DATA\DatabaseName.mdf','C:\Program Files\Microsoft SQL Server\MSSQL10_50.InstanceName\MSSQL\DATA\LogName_log.ldf'"
goto end

:nofile
echo SyncMe.bat not found on G:\
goto end

:end
眼眸里的快感 2024-12-21 12:19:56

我认为您无法让 SQL Server 为您完成这一切。我最终会编写一个使用 SMO 来执行操作的小控制台应用程序对我来说的工作:

Server sqlServer = new Server(@"MYMACHINE\INSTANCENAME");
sqlServer.DetachDatabase("MyDatabaseName", false);

// copy the mdf and ldf to your thumb drive

StringCollection databaseFiles = new StringCollection();
databaseFiles.Add(@"C:\MyDBLoc\MyDatabase.mdf");
databaseFiles.Add(@"C:\MyDBLoc\MyDatabase.ldf");
sqlServer.AttachDatabase("MyDatabaseName", databaseFiles);

I don't think you can get SQL Server to do this all for you. I'd end up writing a little console app that used SMO to do the work for me:

Server sqlServer = new Server(@"MYMACHINE\INSTANCENAME");
sqlServer.DetachDatabase("MyDatabaseName", false);

// copy the mdf and ldf to your thumb drive

StringCollection databaseFiles = new StringCollection();
databaseFiles.Add(@"C:\MyDBLoc\MyDatabase.mdf");
databaseFiles.Add(@"C:\MyDBLoc\MyDatabase.ldf");
sqlServer.AttachDatabase("MyDatabaseName", databaseFiles);
孤寂小茶 2024-12-21 12:19:56

您可以做的是复制 .mdf 和 .ldf 文件,如果您没有更改任何设置,您应该能够在此处找到它们:

C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA

I不认为您可以在服务器运行时执行此操作,但您可以轻松地从 Management Studio 分离数据库(右键单击数据库..任务..分离)。然后您可以肯定地复制文件,首先压缩它们应该会在大小上产生很大的差异并且易于对它们进行版本控制。

相反,在 Management Studio 中,您可以选择数据库节点,然后单击 .. 附加数据库以再次附加它们。

看起来像关于这个问题的一篇好文章。

问候格特-扬

What you can do is copy the .mdf and .ldf files, if you didn't change any settings you should be able to find them around here:

C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA

I don't think you can do that while the server is running, but you can detach a DB easiliy from Management Studio (right-click database .. tasks .. detach). Then you can copy the files for sure, zipping them first should make a lot of difference in size and easy to version them.

The other way around, in management studio you can select the databases node, and richtclick .. attach database to attach them again.

This seems like a good article on the matter.

Regards Gert-Jan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文