Using Robocopy to deploy sites

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?


What we use is the following

  • first build the website with msbuild in cruisecontrol.net to build the binaries
  • archive the currently deployed files under a timestamped folder to avoid losing data in case of a problem

    C:DevToolsRobocopyrobocopy.exe /R:1 /W:10 /mir "D:WebSiteFiles" "D:WebarchiveArchivedFilesDocuments.%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:DevToolsRobocopyrobocopy.exe /R:1 /W:10 /mir "c:devsite" "D:WebSite" /XF *.scc /XD "D:WebSiteFiles"

  • copy and rename (with xcopy, this time) a release.config with correct information to d:Websiteweb.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).

  • restart the website
  • (optional) delete the archive you made at step two
  • 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


    Is Robocopy a hard requirement? Why not use MSBuild? Everything you have listed can painlessly be done in MSBuild.

    <!-- Attempt to build new code -->
    <MSBuild Projects="$(BuildRootPath)ThePhotoProject.sln" Properties="Configuration=$(Environment);WebProjectOutputDir=$(OutputFolder);OutDir=$(WebProjectOutputDir)" />
    
    <!-- Get temp file references -->
    <PropertyGroup>
      <TempConfigFile>$([System.IO.Path]::GetTempFileName())</TempConfigFile>
      <TempEnvironmentFile>$([System.IO.Path]::GetTempFileName())</TempEnvironmentFile>
    </PropertyGroup>
    
    <!-- Copy current web configs to temp files -->
    <Copy SourceFiles="$(OutputFolder)web.config" DestinationFiles="$(TempConfigFile)"></Copy>
    <Copy SourceFiles="$(OutputFolder)web.$(Environment).config" DestinationFiles="$(TempEnvironmentFile)"></Copy>
    <ItemGroup>
      <DeleteConfigs Include="$(OutputFolder)*.config" />
    </ItemGroup>
    
    <Delete Files="@(DeleteConfigs)" />
    

    ...

    <!-- Copy app_offline file -->
    <Copy SourceFiles="$(CCNetWorkingDirectory)Builderapp_offline.htm"  DestinationFiles="$(DeployPath)app_offline.htm"  Condition="Exists('$(CCNetWorkingDirectory)Builderapp_offline.htm')"  />
    
    <ItemGroup>
      <DeleteExisting Include="$(DeployPath)***.*" Exclude="$(DeployPath)app_offline.htm" />      
    </ItemGroup>
    
    <!-- Delete Existing files from site -->
    <Delete Files="@(DeleteExisting)"  />
    <ItemGroup>
      <DeployFiles Include="$(OutputFolder)***.*" />
    </ItemGroup>
    
    <!-- Deploy new files to deployment folder. -->
    <Copy SourceFiles="@(DeployFiles)"  DestinationFiles="@(DeployFiles->'$(DeployPath)%(RecursiveDir)%(Filename)%(Extension)')"  />
    
    <!-- Delete app_offline file -->
    <Delete Files="$(DeployPath)app_offline.htm" Condition="Exists('$(DeployPath)app_offline.htm')"  />
    


    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, ie 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.

    链接地址: http://www.djcxy.com/p/81654.html

    上一篇: Web部署项目失败构建(Visual Studio 2008)

    下一篇: 使用Robocopy部署网站