用 C# 编写文件备份实用程序

发布于 2024-08-04 21:48:19 字数 990 浏览 7 评论 0原文

--开始咆哮--

我想做的就是复制“Documents and Settings”文件夹以将其备份到 Windows Server 2003 中,这样我们就可以根据需要轻松地从中获取旧文件,在我擦除服务器以将操作系统升级到 Windows Server 2008 后。 但是,当我尝试复制它时,我收到有关 NTUSER 正在使用等错误。 管理员不能只使用 Windows 资源管理器来复制文件夹,但无论怎样,这非常令人恼火。

我没有时间跳过安全圈、删除权限。 我没有时间启动一个巨大的文件夹副本,只是为了让它在遇到打开的文件句柄时在操作中随机失败。 我没有时间一遍又一遍地重复这个过程,每次都寻找打开的文件句柄并用进程资源管理器关闭它,从而产生系统不稳定的副作用。

--END RANT--

我知道用户模式进程可以具有某种允许其绕过 NTFS 安全性的标志。

  • “备份”角色 (SE_BACKUP_NAME/SE_RESTORE_NAME) 赋予进程(例如 C# 程序)?

  • 它可以在托管代码中完成吗(或者我必须挖掘 Win32 API 文档,或者手写清单并将它们嵌入谁知道在哪里,或者追踪一些晦涩的备份程序注册命令行-utility)?

  • 如何在 C# 中使用备份语义打开文件,以便复制该文件?

  • 打开文件句柄(带有独占读锁)会干扰出于备份目的读取文件,还是使用备份语义打开文件会解决这个问题,并在打开文件进行备份时暂时暂停写入文件?

我在这里问,因为在 stackoverflow 上保留记录是有用的信息,有人可能会胡言乱语,我没有时间自己查找。谢谢。

很快,我将把一些服务器升级到噩梦般的 Windows Server 2008,并学习 icacls 命令实用程序,这要归功于 Microsoft 删除了对多个文件夹/文件进行安全性编辑的 GUI 支持(聪明的举动。不是。)

--BEGIN RANT--

ALL I want to do is copy the "Documents and Settings" folder to back it up in Windows Server 2003, so we can grab old files from it, as needed, easily, after I wipe the server to upgrade the OS to Windows Server 2008.
BUT, I get errors about NTUSER being in use, etc., when I try to copy it.
It's VERY irritating that an administrator can't just use Windows explorer to copy folders, but what-ever.

I don't have time to jump through security hoops, wiping out permissions.
I don't have time to initiate a HUGE folder copy only to have it fail some random % through the operation when it runs into an open file handle.
I don't have time to repeat this process over and over, hunting down the open file handle and closing it with process explorer each time, destabilizing the system as a side effect.

--END RANT--

I understand that a user-mode process can have some kind of flag that allows it to bypass NTFS security.

  • How do I give this "backup" role (SE_BACKUP_NAME/SE_RESTORE_NAME) to a process, such as a C# program?

  • Can it be done in managed code (or am I gonna have to dig up Win32 API docs, or hand-write manifests and embed them who-knows-where, or track down some obscure backup-program-registration command-line-utility)?

  • How do I open a file in C# with backup semantics, so I can copy the file?

  • Will open file handles (with exclusive read-locks) interfere with reading the file for backup purposes, or will opening it with backup semantics take care of that, and temporarily suspend writing to the file while it's opened for backup?

I'm asking here, because it's useful information to remain on-record at stackoverflow, that someone probably can spout off the top of their head, that I don't have time to look up myself. Thank you.

Soon enough, I'll be upgrading some servers to the nightmare that is Windows Server 2008 and learning the icacls command utility, thanks to Microsoft removing GUI support for editing security on multiple folders/files (smart move. not.)

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

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

发布评论

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

评论(1

夏见 2024-08-11 21:48:19

我使用 Microsoft 同步框架 来创建我自己的备份和同步框架。恢复应用程序以迁移用户配置文件和其他相关文件。同步框架至少可以选择在使用文件时静默失败并继续而不提示,这是我想要的行为。

由于它是一个同步框架,因此具有一些与冲突管理相关的功能。我不需要它们,但除了文件同步之外,它还有其他用于数据库和 Web 同步的“提供者”。

也许使用 psexec 远程运行程序可以减少使用的文件数量。就我而言,我只关心“当前”用户,因此该程序由本地用户运行并将文件备份到网络共享。

同步框架确实需要 MSI 安装,但我也使用 PSExec 进行了安装。

I've used the Microsoft Synchronization Framework to create my own Backup & Restore application to migrate user profiles and other related files. The Sync Framework at least has the option to fail silently and continue without prompting when a file is in use, which is the behavior I wanted.

Since it is a synchronization framework it has some ability related to Conflict management. I didn't need them, but it also has other 'providers' for Database and Web synchronization, in addition to the File Sync.

Maybe running the program remotely using psexec can reduce the number of files in use. In my case I only cared about the "current" user so the program was run by the local user and backed up files to a Network Share.

The Sync Framework did require an MSI installation, but I did that with PSExec too.

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