Windows command – robocopy

測試環境為 Windows2012 R2 x86_64 (虛擬機)

  • ServerA IP : 192.168.95.176
  • ServerB IP : 192.168.95.175

原本計劃使用 DFS-Replication (分散式檔案系統 Distributed File System) 複寫功能將 ServerA 的資料同步到 ServerB,但需要先架設好 AD (Active Directory),因為就只有兩台 server A , B 所以就先不考慮使用 AD+DFS 架構了.

找了另外一個比較簡單的方式,直接使用 Windows 內建的指令 robocopy (Robust File Copy).

robocopy "<source replicated folder path>" "<destination replicated folder path>" /Parameter

像我的情況需要將 ServerA 的資料同步到 ServerB 就可以使用參數 /MIR (Mirror 鏡像) ,但需要注意的是當執行 robocopy 後 serverB 的資料會隨著 ServerA 一樣新增,刪除,如果想要保留 serverA 的舊資料可以使用 磁碟區陰影複製服務(Volume Shadow Copy Service) 的功能.

下面範例將 遠端 \\192.168.95.175\share 資料夾鏡像 (Mirror) 到 本地端的 c:\Mirror\ 資料夾.

C:\Users\Ben>robocopy \\192.168.95.175\share c:\Mirror\ /MIR

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Wednesday, September 12, 2018 8:44:44 AM
   Source : \\192.168.95.175\share\
     Dest : c:\Mirror\

    Files : *.*

  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /R:1000000 /W:30

------------------------------------------------------------------------------

                           3    \\192.168.95.175\share\
100%        New File                   0        New Bitmap Image.bmp
            New File                  22        New Compressed (zipped) Folder.z
100%
100%        New File                 976        New Text Document.lnk
          New Dir          2    \\192.168.95.175\share\New folder\
100%        New File                   0        New Bitmap Image.bmp
100%        New File                  10        New Text Document.txt

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         1         0         0         0         0
   Files :         5         5         0         0         0         0
   Bytes :      1008      1008         0         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00


   Speed :               63000 Bytes/sec.
   Speed :               3.604 MegaBytes/min.
   Ended : Wednesday, September 12, 2018 8:44:44 AM

當遠端資料沒有更新時,本地端的資料就不會更新 (skipped).

C:\Users\Ben>robocopy \\192.168.95.175\share c:\Mirror\ /MIR

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Wednesday, September 12, 2018 8:45:07 AM
   Source : \\192.168.95.175\share\
     Dest : c:\Mirror\

    Files : *.*

  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /R:1000000 /W:30

------------------------------------------------------------------------------

                           3    \\192.168.95.175\share\
                           2    \\192.168.95.175\share\New folder\

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         0         0         0         0         0
   Files :         5         0         5         0         0         0
   Bytes :      1008         0      1008         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Wednesday, September 12, 2018 8:45:07 AM

不做鏡像時,微軟建議使用參數如下.

robocopy.exe "\\srv01\e$\rf01" "d:\rf01" /e /b /copyall /r:6 /w:5 /MT:64 /xd DfsrPrivate /tee /log:c:\temp\preseedsrv02.log

參數說明:

  • /e
    Copies subdirectories and their files, as well as empty subdirectories.
  • /b
    Copies files in Backup mode.
  • /copyal
    Copies all file information, including data, attributes, time stamps, the NTFS access control list (ACL), owner information, and auditing information.
  • /r:6
    Retries the operation six times when an error occurs.
  • /w:5
    Waits 5 seconds between retries.
  • MT:64
    Copies 64 files simultaneously.
  • /xd DfsrPrivate
    Excludes the DfsrPrivate folder.
  • /tee
    Writes status output to the console window, as well as to the log file.
  • /log <log file path>
    Specifies the log file to write. Overwrites the file’s existing contents. (To append the entries to the existing log file, use /log+ <log file path>.)
  • /v
    Produces verbose output that includes skipped files.

其他參數請參考 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

沒有解決問題,試試搜尋本站其他內容

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料