使用 Robocopy 部署站点
我希望能够快速将更新部署到相当繁忙的站点。对于较小的站点,我只会通过 FTP 将新文件覆盖旧文件。然而,这个有一些定期更新的大型 dll,当它们复制时,站点实际上已关闭(此外,在出现问题时备份它们也很麻烦。
我的计划是使用 TortoiseHg 与通过 FTP(使用网络驱动器或类似的东西)在服务器上创建一个暂存副本,然后我可以检查所有操作是否顺利运行,一旦完成,我想运行一个 .bat 文件(或其他文件)来创建该文件的备份。实时站点(最好只有即将更改的文件,但这并不重要),然后将新更改的文件复制到实时站点
(如果可能),我还希望副本忽略某些目录(例如用户上传)。它不会覆盖实时站点上的这些文件?
我听说 RoboCopy 是可行的方法,但我不确定从哪里开始,我需要调用 2 个命令(1 个用于初始备份,一个用于初始备份)。如果出现问题,有什么方法可以将实时站点恢复到之前的状态吗?
该站点位于 ASP.NET 中,并将被复制到 Windows 2003 服务器。
编辑:当 web.config 项目发生更改并且需要合并时,这会变得有点棘手,以便临时服务器设置(应用程序设置、连接字符串等)不会部署到实时站点。这是如何处理的?
I want to be able to quickly deploy updates to a site that is fairly busy. For smaller sites I would just FTP the new files over the old ones. This one, however, has a few large dll's that regularly get updated and while they are copying the site is effectively down (plus there is the hassle of making backups of them in case something goes wrong.
My plan is to use TortoiseHg to synchronise with a staging copy on the server over FTP (using netdrive or something similar). I can then check all is running smoothly and once that is complete I would like to run a .bat file (or something else) that will create a backup of the live site (preferably only the files that are about to change, but that is not critical) and then copy the newly changed files over to the live site.
If possible I also want to have the copy ignore certain directories (like user uploads) so that it won't overwrite those files on the live site?
I've heard RoboCopy is the way to go but I'm not sure of where to start. Would I need to call 2 commands (1 for the initial backup and one for the copy)? Is there any way to restore the live site to it's previous state should something go wrong?
The site is in ASP.NET and would be copied to Windows 2003 server.
EDIT: It gets a little tricky when web.config items have changed and need to be merged so that the staging servers settings (appsettings, connection strings, etc) don't get deployed to the live site. How does that get handled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我们使用的是以下方法,
将当前部署的文件存档在带时间戳的文件夹下,以避免出现问题时丢失数据
C:\DevTools\Robocopy\robocopy.exe /R:1 /W:10 /mir "D:\WebSite\Files" "D:\Webarchive\ArchivedFiles\Documents.%date:~0, -8%.%日期:~3,-5%.%日期:~6%.%时间:~0,-9%.%时间:~3,-6%.%时间:~6,-3% " /XF *.scc
停止网站
通过复制除我们存档的文件之外的所有内容(/XD 是排除目录)来部署网站
C:\DevTools\Robocopy\robocopy.exe /R:1 /W:10 /mir "c:\dev\site" "D:\WebSite" /XF *.scc /XD "D: \WebSite\Files"
复制并重命名(这次使用 xcopy)一个包含正确信息的release.config到d:\Website\web.config(事实上,这就是我们过去所做的,现在我们有一个自制转换引擎来动态更改开发web.config的部分)。
在您的情况下,您必须为要忽略的任何目录(例如用户的上传目录)添加 /XD 标志。除非生产 web.config 文件很复杂,否则我真的建议简单地复制您作为项目一部分维护的release.config,与 web.config 并排
What we use is the following
archive the currently deployed files under a timestamped folder to avoid losing data in case of a problem
C:\DevTools\Robocopy\robocopy.exe /R:1 /W:10 /mir "D:\WebSite\Files" "D:\Webarchive\ArchivedFiles\Documents.%date:~0,-8%.%date:~3,-5%.%date:~6%.%time:~0,-9%.%time:~3,-6%.%time:~6,-3%" /XF *.scc
stop the website
deploy the website by copying everything except the files we archived (/XD is eXclude Directory)
C:\DevTools\Robocopy\robocopy.exe /R:1 /W:10 /mir "c:\dev\site" "D:\WebSite" /XF *.scc /XD "D:\WebSite\Files"
copy and rename (with xcopy, this time) a release.config with correct information to d:\Website\web.config (in fact, that's what we used to do, now we have a homebrew transformation engine to change parts of the dev web.config on the fly).
In your case, you'll have to add the /XD flags for any directory you want to ignore, such as the users' upload. And unless the production web.config file is complicated, i'd really recommend simply copying a release.config that you maintain as a part of the project, side by side with the web.config
Robocopy 是硬性要求吗?为什么不使用 MSBuild?您列出的所有内容都可以在 MSBuild 中轻松完成。
...
Is Robocopy a hard requirement? Why not use MSBuild? Everything you have listed can painlessly be done in MSBuild.
...
在基于 Nix 的服务器上,我会使用 RSYNC,并且我知道在 Windows 上您可以使用 DeltaCopy它是 RSYNC 的一个端口,并且是开源的(从未使用过 DeltaCopy,所以请仔细检查)无论如何,假设它像 RSYNC 一样工作,那么它速度很快,并且只更新已更改的文件。
您可以使用各种配置选项来删除目标上已在源上删除的文件,还可以在文件中使用添加来排除您不想复制的文件或目录,即本地配置。 。
您应该能够将其全部折叠到一个脚本中,以便在需要时运行,这意味着您可以对其进行测试和计时,以便了解发生了什么
On Nix based servers i would use RSYNC and i understand that on Windows you can use DeltaCopy which a port of RSYNC and is open sources (never used DeltaCopy so please check it carefully) Anyway assuming it works like RSYNC then it is fast and only updates files that have been changed.
You can use various configuration options to delete files on the target that have been deleted on the source and you can also use an add in a file that will exclude files or directories, i.e. the local config, you do not want copying. etc.
You should be able to fold it all into one script to run when required which means you can test and time it so you know what is happening.
查看这些链接,看看它们是否有帮助:
你会发现
robocopy.exe /?
非常有帮助。特别是,您需要使用/XF
开关来排除文件,使用/XD
来排除文件夹。不过,您需要编写一个脚本(例如bat、powershell、cscript)来处理web.config 问题。
Check out these links to see if they help:
You'll find that
robocopy.exe /?
is extremely helpful. In particular you'll want the/XF
switch for excluding files, and/XD
for excluding folders.You will need to write a script (e.g. bat, powershell, cscript) to take care of the web.config issues though.
Microsoft 自己使用 robocopy 将更新部署到某些站点。
我不知道您是否有多个服务器,但我们的部署脚本类似于:1) 停止 IIS(这将使服务器脱离负载均衡器轮换,2)RoboCopy /MIR from \STAGING\path\to\webroot to \WEB##\path\to\webroot 其中 ## 是服务器的编号, 3) 启动 IIS。这是在临时服务器上对站点进行冒烟测试后完成的。
这对您的配置问题没有太大帮助,但我们的暂存和生产配置文件是相同的。
Microsoft themselves use robocopy to deploy updates to some sites.
I don't know if you have multiple servers, but our deployment script went something like: 1) Stop IIS (which would take the server out of load-balancer rotation, 2) RoboCopy /MIR from \STAGING\path\to\webroot to \WEB##\path\to\webroot where ## is the number of the server, 3) Start IIS. This was done after the site was smoke-tested on the staging server.
That doesn't much help with your config problem, but our staging and production config files were the same.
您需要(也是我需要)的是一个同步程序,能够在服务器上创建文件备份,并通过 ftp 快速复制通过首先将文件复制到临时目录或部分更新,可以对文件进行处理。
这是我发现的一个程序:http://www.superflexible.com/ftp.htm
What you need (and I need) is a synchronize program with the ability to create backup of the files on the server, and make quick copy over ftp of the files at ones by probably copying them first on a temporary directory, or by partial updating.
This is one program that I found : http://www.superflexible.com/ftp.htm
WebDeploy 是处理部署的更好方法(请参阅 Scott H http://www.hanselman.com /blog/WebDeploymentMadeAwesomeIfYoureUsingXCopyYoureDoingItWrong.aspx)
但是,Robocopy 是一个很棒的低成本部署工具,我仍在某些网站上使用它(还没有时间将它们更改为 webdeploy)。 Robocopy 与 xcopy 类似,但具有更丰富的选项集。因此,您需要 2 个 Robocopy 命令(1 个用于备份,1 个用于部署)。我通常在文件暂存时执行备份命令。
管理配置文件总是很棘手(也是使用 webdeploy 的一个重要原因)。一种方法是为签入源代码管理的每个环境保留一份配置文件的副本(例如,web.dev.config、web.uat.config、web.prod.config 等)。暂存(或部署脚本)将获取并重命名必要的配置文件。
WebDeploy is a much better way to handle deploys (see Scott H http://www.hanselman.com/blog/WebDeploymentMadeAwesomeIfYoureUsingXCopyYoureDoingItWrong.aspx)
But, Robocopy is a great low-cost deploy tool that I still use on some sites (haven't find the time to change them to webdeploy). Robocopy is like xcopy but with a much richer set of options. So you would need 2 Robocopy commands (1 for backup and 1 for deploy). I normally do the backup command when the files are staged.
Managing config files is always tricky (and a big reason to use webdeploy). One approach, is keep a copy of the config files for each environment checked into your source control (eg, web.dev.config, web.uat.config, web.prod.config, etc). The staging (or deploy script) would grab and rename the necessary config file.
您可能需要使用多种工具的组合。
我会看看 DFSR(文件服务器角色),在您的实时站点上有一个只读文件夹(因此它是单向复制)。
它非常容易配置,具有漂亮的 GUI,能够根据位置和/或掩码排除文件,并且启用卷影复制后,您可以让它按您设置的计划运行并更新那些仅更改的文件(或拥有它)按计划运行,甚至手动运行)。这样做的好处是一旦配置完毕,您就不必再次接触它。
一旦您复制了大部分文件,您就可以在 web.config 上自动执行可能的合并(假设您希望自动执行)方面获得帮助。
You would probably need to use a combination of tools.
I would have a look at DFSR (File Server role) with a read-only folder on your live site (so it's one-way replication).
It is very easy to configure, has a nice GUI, ability to exclude files based on location and/or masks, and with Volume Shadow Copy enabled you can have it running on schedules you set and updating those files that change only (or have it run on a schedule, or even run it manually). The beauty of this is once it is configured, you don't have to touch it again.
Once you have the bulk of your files replicating you could then get assistance in automating the possible merge on web.config, assuming you want that automated.
MSBuild 很棒,除了一个小缺陷(或主要缺陷,取决于你的观点)。每次运行构建时它都会重建二进制文件。这意味着,对于从测试部署到生产,或从阶段部署到生产(或无论您的预生产环境被称为什么),如果您使用 MSBuild,则不会将现有二进制文件从一个环境升级到下一个环境,而是重新构建它们。这也意味着您确信,自从您对预生产环境进行 MSBuild 以来,源代码存储库中没有任何变化。允许对任何事情(无论是主要还是次要)进行哪怕最轻微的更改,都意味着您不会将经过全面测试的产品推广到您的生产环境中。在我工作的地方,这是不可接受的风险。
输入 Robocopy。通过 Robocopy,您可以将(希望)经过全面测试的产品复制到您的生产环境中。然后,您需要手动修改 web.config/app.config 以反映生产环境,或者使用转换工具来执行此操作。为此,我一直在使用 SourceForge 上提供的“配置转换工具”——它的工作原理就像 MSBuild web/app.config 转换一样。
MSBuild is great, except for one minor (or major depending on your point of view) flaw. It rebuilds the binaries every time you run a build. This means, for deploying from TEST to PRODUCTION, or STAGE to PRODUCTION (or whatever your pre-production environment is called), if you use MSBuild, you are not promoting existing binaries from one environment to the next, you are re-building them. This also means that you are relying, with certainty, that NOTHING has changed in the source code repository since you did an MSBuild to your pre-production environment. Allowing even the slightest chance of a change to anything, major or minor, means you will not be promoting a fully tested product into your production environment. In the places I work, that is not an acceptable risk.
Enter Robocopy. With Robocopy, you are copying a (hopefully) fully tested product to your production environment. You would then either need to manually modify your web.config/app.config to reflect the production environment, OR use a transformation tool to do that. I have been using the "Configuration Transformation Tool" available on SourceForge for that purpose - it works just like the MSBuild web/app.config transformations.