Copy files from one Windows server to another and retain permissions

Just want the script?

If you don’t want to read the blah blah blah you can access robocopy-script on Github

I want to know the details…

At my day job I was tasked with migrating several websites and folders from one Windows server to another. The sites have a ton of quirky permissions settings because they are mostly intranet sites. Several have anonymous browsing turned off. As you may or may not know if you just copy them via drag and drop in File Explorer you will lose all of those permissions that took years to refine. Additionally you will lose creation dates, etc.

After some research I determined that Robocopy was what I needed to retain all that valuable cruft information. Unfortunately (or fortunately if you dig command line stuff) Robocopy is a command line tool with quite a few flags. A little more digging revealed there is a GUI for Robocopy which allows you to save scripts, but I decided to roll my own script.

Features:

  • A simple “config” section in the script where you define the to/from locations
  • A config option to run attended or unattended (set this in the script)
  • Logging
  • Utilization of the following flags (descriptions are verbatim from the Robocopy documentation):
    • /E – Copies all subdirectories (including empty ones).
    • /SEC – Copies NTFS security information. (Source and destination volumes must both be NTFS). Equivalent to /COPY:DATS.
    • /COPY:DATSO – Copies the file information specified by copyflags, which can be any combination of the following : D – file Data. | A – file Attributes | T – file Timestamps. | S – file Security (NTFS ACLs). | O – file Ownership information.
    • /V – Produces verbose output (including skipped files).
    • /TEE – Displays output in the console window, in addition to directing it to the log file specified by /LOG or /LOG+.
    • /NP – Turns off copy progress indicator (% copied). **
    • /LOG – Redirects output to the specified file, overwriting the file if it already exists.
    • /B – Copies files in Backup mode (Backup copies are not restartable, but can copy some files that restartable mode cannot). ***
    • /R:10 – Specifies the number of retries on failed copies. (The default is 1 million.)
    • /W:30 – Specifies the wait time between retries. (The default is 30 seconds.)

** I liked seeing the progress as it copied, but all those percentages get written to the log file same as the screen.
*** Consider /Z to use restartable mode.

Obviously you could tweak the flags in the script to get a combination more suitable for you.

If this sounds like something you might find useful you can access robocopy-script on Github. I should note that this is my first public repo on Github. Yay.

DOS .bat files (What I learned from writing a subversion backup script)

Comments
REM Comment Style 1
:: Comment Style 2

Set a variable
SET myCat=Dante
Output a variable
ECHO %myCat%
If then Else statement
if %yCat%== "Dante" (ECHO MY CAT) ELSE (ECHO NOT MY CAT)
Output a command’s result to a file
dir >> log.txt
Split a command across 2 lines (use ^ at the end of the line)
ECHO This is line one and ^
this is line 2.

Two commands on 1 line (separate with &)
echo command 1 & echo command2
List only directory names and no info
dir /b /ad
For-in-do (For every item do something)
FOR /F %%G IN ('dir /b /ad %repodir%') DO ECHO %%G

Subversion backup of multiple repositories via DOS .bat file

I recently had to implement Subversion at work to manage our source code. Part of that implementation was coming up with a way to automate the backup process. Since developers could be accessing code repositories at any time there is a special command (called hotcopy) in Subversion for copying a repository to another location, which you can then backup to disk, tape, etc.

I started out just doing a simple DOS .bat file to run the hotcopy. My plan was to schedule this to run in the Windows scheduler prior to the nightly backup. As I started working on the script though I found myself trying to improve it to be as generic and hands off as possible. I also wanted to backup multiple repositories.

Here are some “features” of the script:

  • It will hotcopy multiple repositories in a specified directory.
  • It can be run attended or unattended
  • It has some basic log functionality

Here is a look at the actual hotcopy command in the .bat file:
ECHO Starting SVN backup for %%G... >> %repolog% & ^
svnadmin hotcopy %repodir%%%G %repodirhot%%%G --clean-logs >> %repolog% & ^

Download it here and rename it from svn_backup.txt to svn_backup.bat.