page contents

About the Post

Author Information

HOW TO: Use ROBOCOPY to Move SQL Files

As you know I recently blogged an article about attending SQL Saturday #493 in Mountain View, CA (Silicon Valley) and this is where I met Tim Ford (Blog | Twitter) for the first time. During his presentation, he asked for fellow bloggers to write about entry level topics. He stated that from time to time he gets feedback that people reading how blogs do not always understand some of the assumptions that we presume our readers to know. In some cases, we tend to try and outshine one another to demonstrate our knowledge, experience or passion in our topics. While that can be a good growth between people with a firm grasp on those topics, it can also lead to leaving others out of the discussion.

I plan to write this article with that goal in mind (to write a topic that can be appreciated by all types of readers) and explain why I used Robocopy to solve a simple problem. I will tweet out to #iwanttoshare to get added to Tim’s list. Before I begin, let me explain that this tool is a great and simple tool that is built into recent releases of Windows, so that means it is free and designed for copying files. The file(s) are copied, with progress shown.

A few things I love about Robocopy is the following capabilities:

  • It is really easy to call out either a network location or local directory for either the source or the destination
  • I able to copy just one single file, say a particular transaction log file or I can easily get every transaction log in that directory
  • The /z option can be used to tell Robocopy that if the transfer gets interrupted (for any reason) to keep on trying every 30 seconds until successful
  • The most important feature is that I can see a status or progress, in terms of actual bytes, not just the generic Windows’s “xx %” message that runs for three hours (but says 40 minutes)
    Please note: You may recall from this article that PowerShell was used to copy the files, but there was no status on where the progress is currently

My hope is that you will stop dragging and dropping (also known as copying and pasting) files between servers and/or workstations. Please note that method will consume a lot of memory on the server and also it is slower. (You can test this theory by moving large files in the neighborhood of 1 GB or more)

robocopy <source directory> <destination directory> <optional: file name> /<options>

This is the basic syntax of this tool (above) and you will see with the switch/?” (below) that there are a lot of options (that even the screenshot could not capture):

So, with the basics out of the way, let’s talk about a couple of use case scenarios:

  • Say you want to copy the SQL data file (in my case the AdentureWorks sample databases) from a shared location where I stage files to the Microsoft SQL Server data file location (in my case the H drive)
    robocopy E:\bin\SQL\Databases\2012 "H:\Program Files\MSSQL11.MSSQLSERVER\MSSQL\Data" /Z

    In this example, you can see the progress of a file being copied:

    Now, here is how it looks when it has completed:

  • In an earlier article, I talked about moving the system databases from the default location of your C drive to a separate volume for your data and log files.
    robocopy "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA" "H:\Program Files\MSSQL11.MSSQLSERVER\MSSQL\Data" *.mdf /move

    As you can see I called out the source, then the destination followed by the file extension of the data file (.mdf) with the /move switch

  • This is how you can move the logs to their own volume
    robocopy "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA" "O:\Program Files\MSSQL11.MSSQLSERVER\MSSQL\TLogs" *.ldf /move

    Again, I called out the source, then the destination followed by the file extension of the data file (.ldf) with the /move switch

    Please note:
    The reason that we recommend moving these files types in this manner is how they will be written. Data files are random writes, whereas the log file gets written sequentially. (one after the other, so no jumping around disk sectors)

  • This next feature is really helpful, but you must be careful you got the syntax correct. The switch /mir will allow you to sync files or folders between directories or servers. In my case this is really helpful as I build SQL Servers for an AlwaysOn configuration where I want to copy certain scripts and other files
    robocopy \\<server share name>\bin "E:\bin" /z /mir

    This is the view when it is completed and you can run this option multiple times to ensure you have a sync. It will not copy the same files, but only the files with changes. (Also known as the Delta)

    Warning: This option will only copy the new versions from the source, but it will also delete files from the destination if they do not exist on source folder. It will make sure that both the source and the destination have the exact same files.

As usual, I hope this helps you and more importantly inspires you to use Robocopy over copying from Windows Explorer.

Tags: , , , , , , , , , ,

Comments are closed.

Copy Protected by Chetan's WP-Copyprotect.