xcopy 在 SQL Server 上引发错误(xp_cmdshell 脚本)

发布于 2025-01-16 02:48:27 字数 544 浏览 2 评论 0原文

在 SQL 上,我们通过 xp_cmdshell 脚本复制现有文件夹来创建一个新文件夹。

SQL脚本:

declare @cmd   sysname 
set @cmd = 'xcopy /t /e /i "C:\Rudresh\Process\001" "C:\Rudresh\Process\002" /O /X /H /K'                
exec master..xp_cmdshell @cmd

脚本在执行时会抛出拒绝访问错误,并且我已经提供了所有文件夹权限。 请告诉我是否需要为 SQL 用户授予任何其他权限。?或者我是否错过了某些配置(NTFS 或 ACL 权限)

注意:如果我从脚本中删除 /O /X 并创建目录,则脚本可以正常工作。

输入图片此处描述

on SQL we are creating a new folder by copying and existing folder by xp_cmdshell script.

SQL Script:

declare @cmd   sysname 
set @cmd = 'xcopy /t /e /i "C:\Rudresh\Process\001" "C:\Rudresh\Process\002" /O /X /H /K'                
exec master..xp_cmdshell @cmd

Script will throw Access Denied error upon execution and I have provided all the folder permission.
kindly let me know is there any other permission i need to grant for SQL User.? or do i missed something configure (NTFS or ACL permissions)

Note: Script works fine if i remove /O /X from the script and creates the directory.

enter image description here

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

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

发布评论

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

评论(1

雨的味道风的声音 2025-01-23 02:48:27

当 SQL Server 打开并使用 SQL Server 数据库文件时,您无法安全地创建 SQL Server 数据库文件的纯副本。因此,这些文件通常由 SQL Server 打开,不允许任何共享,甚至共享读取。有一种访问权限甚至可以绕过这一点,但我不推荐它,因为复制的文件可能会损坏且无法使用。

许多(大多数?)备份工具和产品可以安全地复制这些文件,但这是因为它们使用特殊的 SQL Server 接口和/或互锁来确保它们不会获得因 SQL Server 中途更改内容而损坏的文件。复制。

因此,此问题的解决方案是:

  1. 在运行 XCopy 之前关闭 SQL Server(或分离特定数据库)(完成后重新附加数据库),或
  2. 使用支持 SQL Server 的备份工具,或
  3. 使用 SQL Server BACKUP 命令来备份数据库和日志文件,然后让 XCopy 仅复制备份文件,而不是实时文件,或者
  4. 仅让 SQL Server BACKUP 完成所有工作,而不需要任何 XCopy。 SQL Server 有很多工具来支持这种方法,这是大多数 DBA 所做的事情(这些工具使用 SQL Agent 来运行调用 SQL 备份的 Powershell 作业)。

You cannot safely do a plain copy of SQL Server database files when that database is open and in use by SQL Server. For this reason, these files are typically opened by SQL Server disallowing any sharing, even shared reads. There is an access privilege that can bypass even this, but I do not recommend it as the copied files will likely be corrupt and unusable.

Many (most?) BACKUP tools and products can safely copy these files, but this is because they use a special SQL Server interface and/or interlock to insure that they do not get files that are corrupted by SQL Server changing the contents midway through a copy.

So the solutions to this problem are either:

  1. Shutdown SQL Server (or detach the specific database) before you run your XCopy (then re-Attach the database when done), or
  2. Use a Backup tool that supports SQL Server instead, or
  3. Use SQL Server BACKUP commands to make backups of the databases and log files and then have your XCopy copy only the backup files, not the live files, or
  4. Just have SQL Server BACKUP do all of the work without any XCopy. SQL Server has a lot of facilities to support this approach and it is was most DBAs do for this kind of thing (these facilities using SQL Agent to run a Powershell job calling SQL Backup).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文