Real-World Scripting: Replicating Web Site Content


Every day, a Web team changes the content of your company's intranet Web site. The virtual directory structure remains consistent, but the team constantly updates many static pages with new images and information. The team makes its changes to the content on a staging server and then copies the new content to the production server. Recently, the team has encountered problems such as broken links when copying the new content. The Web team leader has asked you to script a solution that meets these requirements:

  1. In one operation, a regularly scheduled batch job must roll over the new Web site contents, which total about 1GB, from the staging server to the production server.
  2. The rollover must be transparent to users.
  3. Three copies (the current copy and two archives) of the Web site content must be present.
  4. If the new site fails, a fallback operation must exist.
  5. Controls must be in place to prevent team members from accidentally running the script.

Copying 1GB of files onto the production server takes several minutes. If you leave the Web service running while files are copying, users might experience broken links because of partially overwritten files. If you stop the Web service, users might not be able to reach the site. Thus, you create a Windows NT shell script, WebRoll.bat, to synchronize the copying operation, ensuring content integrity and high Web site availability.

WebRoll.bat uses three Microsoft Windows NT Server 4.0 Resource Kit utilities (Robocopy, NetSvc, and Sleep) to copy site content between the StageContent directory on the staging server and four folders (NewContent, CurrentContent, OldContent, and OldestContent) on the production server. First, the script uses Robocopy to copy the new site content from the StageContent directory to the NewContent folder. When this copy operation is complete, the script deletes the OldestContent folder and renames the OldContent folder to OldestContent. These copying, deleting, and renaming processes all take place while the Web service is still online, so no user interruptions occur.

The next few steps occur in about 10 seconds and finish the rollover operation. First, the script uses NetSvc to stop the Web service. Next, the script renames the CurrentContent folder to OldContent and renames the NewContent folder to CurrentContent. Finally, the script restarts the Web service.

WebRoll.bat incorporates two safety measures. First, to prevent the rename operation from failing, the script uses the Sleep utility to put a 5-second pause after stopping the service and a 5-second pause before restarting the service. These pauses prevent the possibility of contention for the files you're renaming. Second, to prevent a team member from accidentally running the script, the person must type

WebRoll.bat ROLL

when running or scheduling the script. WebRoll.bat tests for the ROLL security key; if it isn't present, the script informs the team member that the security key is incorrect and exits.

After the script runs, the new content is online for users and the required three versions of the site content exist in the CurrentContent, OldContent, and OldestContent folders. In the event of a problem, the team can easily fall back to a previous content version by stopping the Web service, renaming the CurrentContent folder to RolledBackContent, renaming the OldContent folder to CurrentContent, and restarting the service. (WebRoll.bat includes example code for this rollback operation.)

Listing 1 contains an excerpt from WebRoll.bat. You can find the entire script on the Win32 Scripting Journal Web site. The script includes comments to help you understand the code. Here are the steps to get the script working:

  1. Set up the directories or shares for the source and destination locations of the Web site content.
  2. Configure the script to point to the directories or shares.
  3. Configure the server names and paths to the resource kit utilities and Robocopy log file locations.
  4. Adapt /LOG switch if needed. Robocopy 1.95 supports the /LOG switch that this script uses. If you have an earlier version that doesn't have this option, you can redirect the output from Robocopy to a log file. Simply replace the code /LOG+:"C:\StageToProd.txt" with >>C:\StageToProd.txt in the line at Callout A.
  5. Set up additional permissions if needed. The files copied into the NewContent folder inherit permissions from the parent directory. If your NTFS file permissions structure requires finer granularity, you need to use Robocopy with the /SEC switch or the Xcacls command to set permissions.
  6. Test the script on a development server to be sure it's working properly.
  7. Schedule the script to run at the frequency you require. You can run this script on the production or staging server.

You can modify WebRoll.bat to simultaneously move content to Web servers that are members of an array or to a different site on the same server.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.