3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Robocopyについて勉強しておきたいので、実環境を使って色々勉強しておこうと思います。案件で使うんです。その前にプライベートでいじってみて、操作感を掴んでおこうと思います。

Robocopyとは

Windowsに標準搭載されているCLIベースの高機能ファイルコピーツール。
バックアップやサーバ間のファイル同期に便利なツールになるらしい。
確かに今回勉強しないといけなくなった背景としては、サーバ間でファイル同期するためなんですよね~

コマンドのオプションやリターンコードは公式サイトを参考にすると良さそうですね。

基本文法は以下とのこと。
送信元を指定してその後に送信先を指定。

robocopy <source> <destination> [<file>[ ...]] [<options>]

普通のコピーと何が違うのか

Windowsのコピーコマンド比較

どれでもUNC(Universal Naming Convention)パスを使えばサーバ間コピーなどに使えるようです。
難しい表現っぽく聞こえますが"¥¥サーバー名¥共有名¥ディレクトリ¥ファイル名"みたいなやつです

コマンド 特徴 用途
copy 最も基本的なコピーコマンド ファイル単位のコピー
xcopy copyの拡張版 フォルダ構造ごとコピー可能
robocopy 最も高機能 業務用バックアップ・同期

詳細比較

項目 copy xcopy robocopy
フォルダコピー 不可(ファイルのみ) 可能 可能
サブフォルダ 非対応 /S オプションで対応 デフォルトで対応
差分コピー 非対応 限定的(/D オプション) 完全対応
リトライ なし なし あり
ミラーリング 非対応 非対応 対応(/MIR)
属性保持 基本のみ 一部対応 完全対応
ログ出力 なし なし 詳細ログ可能
マルチスレッド 非対応 非対応 対応

実際に触ってみる

実際に触ってみて感覚を得ておきたいと思います。
環境としてはAWSでWindows Serverをt3.mediumで2台同じVPC・Subnetで立ち上げてそこで色々試してみるって感じで。

転送先に共有フォルダを作成する

検証になるので、everyoneにフルアクセスを付けました。

image.png

image.png

送信元から閲覧など出来るかを確認します。
"¥¥送信先サーバのIP¥dst"と入力して、送信先サーバのユーザとパスワードを入力。
image.png

閲覧・ファイルが作成出来ることを確認しました。
image.png

送信先でも作成したファイルが確認できます。
image.png

テストファイルを作成する

送信元のCドライブ直下にsrcフォルダを作成して、ここにファイル等を作成していきます。

image.png

powershellを使って以下のコマンドを実行します。

$root="C:\src"
New-Item -ItemType Directory -Force $root | Out-Null
1..2000 | % { Set-Content -Path "$root\small_$_.txt" -Value ("x"*200) }
fsutil file createnew "$root\big_1GB.bin" 1073741824
New-Item -ItemType Directory -Force "$root\sub\empty" | Out-Null
Set-ItemProperty "$root\small_1.txt" -Name Attributes -Value "Hidden"
attrib +R "$root\small_2.txt"

実行結果は以下となります。
大きいサイズのファイル・小さいサイズのファイル・入れ子になっているフォルダ等が作成されます。
image.png

基本的なコマンド

以下のコマンドを実行してRobocopyを試してみます。
自身のCドライブ配下のsrcフォルダ配下を相手のdstフォルダ配下にRobocopyします。

robocopy C:\src \\192.168.1.48\dst /E /R:1 /W:1 /LOG:C:\log1.txt

image.png

出力されたlogの中身を確認してみます。


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

  Started : Saturday, December 20, 2025 12:18:40 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

    Files : *.*
	    
  Options : *.* /S /E /DCOPY:DA /COPY:DAT /R:1 /W:1 

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

	                2001	C:\src\
	    New File  		   1.0 g	big_1GB.bin
  0.0%
  0.1%
  0.2%
  0.3%


~~~~~~~~~~~~~~~
中略
~~~~~~~~~~~~~~~

100%  
	    New File  		     202	small_999.txt
100%  
	  New Dir          0	C:\src\sub\
	  New Dir          0	C:\src\sub\empty\

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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         3         2         1         0         0         0
   Files :      2001      2001         0         0         0         0
   Bytes :   1.000 g   1.000 g         0         0         0         0
   Times :   0:00:14   0:00:14                       0:00:00   0:00:00


   Speed :           76,142,753 Bytes/sec.
   Speed :            4,356.923 MegaBytes/min.
   Ended : Saturday, December 20, 2025 12:18:54 AM

Optionsの出力の意味

表示されている項目 意味(要点) 影響/メモ
*.* 対象ファイル指定(ここでは全ファイル) フォルダ配下のファイルを広く対象にする指定
/S サブディレクトリをコピー(空フォルダは除外) /E があると実質そちらが優先なので、併記は冗長になりがち
/E サブディレクトリをコピー(空フォルダも含む) 空フォルダも作成される
/DCOPY:DA ディレクトリのコピー属性:D=日時、A=属性 フォルダのタイムスタンプ/属性をできるだけ維持
/COPY:DAT ファイルのコピー属性:D=データ、A=属性、T=日時 ACL(権限)・所有者・監査はコピーしない(必要なら S/O/U を追加)
/R:1 失敗時の再試行回数 1回だけリトライして諦める(長時間ハングしにくい)
/W:1 再試行までの待ち時間(秒) リトライ間隔が1秒

結果の意味

区分 Total Copied Skipped Mismatch FAILED Extras どう読むか(今回の例)
Dirs 3 2 1 0 0 0 関与したフォルダは計3。2個は作成/コピーされ、1個は既存等で処理不要としてスキップ。
Files 2001 2001 0 0 0 0 ファイルは計2001。全てコピーされ、スキップ/不一致/失敗なし。
Bytes 1.000 g 1.000 g 0 0 0 0 総転送量は約1.0GBで、全量がコピーされた。
Times 0:00:14 0:00:14 0:00:00 0:00:00 全体所要14秒、コピー処理も14秒。不一致対応や失敗対応にかかった時間は0。
意味 どう読むか(今回の値)
Speed : 76,142,753 Bytes/sec. 実効転送速度(バイト/秒) 約 76,142,753 B/s(≒ 72.6 MiB/s、≒ 76.1 MB/s)程度で転送できている
Speed : 4,356.923 MegaBytes/min. 実効転送速度(MB/分)を別単位で表示 約 4,356.923 MB/分(= 約 72.6 MB/s 相当 ※robocopy表示のMB定義により誤差あり)
Ended : Saturday, December 20, 2025 12:18:54 AM 処理完了日時 2025/12/20 00:18:54 にコピー処理が終了した

ミラーリングを試してみる

ミラーリングをしてみます。
今回はsrcからsmall_10というファイルを消して、dstでsmall_20を消してmid_20を追加してみようと思います。
ミラーリングで同期が走るので、small_10がdstでも削除されて、dst側でもmid_20が削除され一方でsmall_20はsrcからコピーされるはずです。

ミラーリングをするときはまずdry runをすると良いようです。
今回は以下のコマンドを使ってまずどういった結果になるかを確認してみます。
/MIRを指定するとミラーリングを実施します。/Lとしていすることでdry runとして実行されます。

robocopy C:\src \\192.168.1.48\dst /MIR /L /v /LOG:C:\mir_dryrun.txt

実行した際に出力されたログ(mir_dryrun.txt)は以下となります。

EXTRA Filesがdstにしかなく、ミラーリングされると削除されるファイル、sameは変更点無しとしてスキップされるファイル、New Fileがミラーリングで同期されるファイルを示しています。想定通りであると言えるでしょう。

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

  Started : Saturday, December 20, 2025 6:07:32 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

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

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

	                2000	C:\src\
	  *EXTRA File 		       0	mid_20.txt
	  *EXTRA File 		     202	small_10.txt
	          same		   1.0 g	big_1GB.bin
	          same		     202	small_1.txt
	          same		     202	small_100.txt
	          same		     202	small_1000.txt
	          same		     202	small_1001.txt
	          same		     202	small_1002.txt
	          same		     202	small_1003.txt
	          same		     202	small_1004.txt

~~~~~~~~~~~~~~~
中略
~~~~~~~~~~~~~~~

	          same		     202	small_994.txt
	          same		     202	small_995.txt
	          same		     202	small_996.txt
	          same		     202	small_997.txt
	          same		     202	small_998.txt
	          same		     202	small_999.txt
	    New File  		     202	small_20.txt
	                   0	C:\src\sub\
	                   0	C:\src\sub\empty\

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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         3         0         3         0         0         0
   Files :      2000         1      1999         0         0         2
   Bytes :   1.000 g       202   1.000 g         0         0       202
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Saturday, December 20, 2025 6:07:32 AM              

今回出力された表示以外の内容をメモっておきます。

表示 意味 送信元 送信先 /MIR実行時の動作
*EXTRA File コピー先にしか存在しない ❌無い ✅ある 削除される
New File 送信元にしか存在しない ✅ある ❌無い コピーされる
same 両側に存在し、同一 ✅ある ✅ある スキップ(何もしない)
Newer 送信元の方が新しい ✅新 ✅古 上書きコピーされる
Older 送信元の方が古い ✅古 ✅新 デフォルトではスキップ
*MISMATCH 同名だがサイズ違い ✅ある ✅ある 上書きコピーされる

dry runのオプションを外して実行してみます。

robocopy C:\src \\192.168.1.48\dst /MIR /v /LOG:C:\mir.txt

実行結果をログから取り出してみます。

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

  Started : Saturday, December 20, 2025 6:20:24 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

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

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

	                2000	C:\src\
	  *EXTRA File 		       0	mid_20.txt
	  *EXTRA File 		     202	small_10.txt
	          same		   1.0 g	big_1GB.bin
	          same		     202	small_1.txt
	          same		     202	small_100.txt
	          same		     202	small_1000.txt
	          same		     202	small_1001.txt
	          same		     202	small_1002.txt

~~~~~~~~~~~~~~~
中略
~~~~~~~~~~~~~~~

	          same		     202	small_996.txt
	          same		     202	small_997.txt
	          same		     202	small_998.txt
	          same		     202	small_999.txt
	    New File  		     202	small_20.txt
100%  
	                   0	C:\src\sub\
	                   0	C:\src\sub\empty\

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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         3         0         3         0         0         0
   Files :      2000         1      1999         0         0         2
   Bytes :   1.000 g       202   1.000 g         0         0       202
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Saturday, December 20, 2025 6:20:24 AM

改めてdry runしてみます。
出力結果を確認すると全てSkippedになっておりますので、同期出来ていそうですね。

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

  Started : Saturday, December 20, 2025 6:28:06 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

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

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

	                2000	C:\src\
	          same		   1.0 g	big_1GB.bin
	          same		     202	small_1.txt
	          same		     202	small_100.txt
	          same		     202	small_1000.txt
	          same		     202	small_1001.txt
	          same		     202	small_1002.txt
	          same		     202	small_1003.txt

~~~~~~~~~~~~~~~
中略
~~~~~~~~~~~~~~~

	          same		     202	small_998.txt
	          same		     202	small_999.txt
	                   0	C:\src\sub\
	                   0	C:\src\sub\empty\

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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         3         0         3         0         0         0
   Files :      2000         0      2000         0         0         0
   Bytes :   1.000 g         0   1.000 g         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Saturday, December 20, 2025 6:28:06 AM

送信先のサーバの共有を同期中に止めてみる

srcディレクトリ内のファイルを削除。
その後以下のコマンドを使って大きめのファイルを作成します。

fsutil file createnew C:\src\big_1.bin 1073741824
fsutil file createnew C:\src\big_2.bin 1073741824
fsutil file createnew C:\src\big_3.bin 1073741824
fsutil file createnew C:\src\big_4.bin 2147483648
fsutil file createnew C:\src\big_5.bin 2147483648

これをRobocopyしていきます。使うコマンドは以下とします。
このコマンドを実行中にdst側の共有化を解除します。
/Zを指定することで再開可能モードでファイルをコピーします。 再起動可能モードでは、ファイルコピーが中断された場合、robocopy はファイル全体を再コピーするのではなく、中断した場所から再開できるようになるらしいです。

robocopy C:\src \\192.168.1.48\dst /MIR /Z /R:3 /W:5 /LOG:C:\test_resume.txt /TEE

実行した結果のログは以下になります。
各ファイルに対して初回試行後、エラーが発生すると5秒待機してリトライを3回実行(合計4回の試行)し、それでも通信が復旧しない場合エラーとして次のファイルに処理を進めることがわかります。

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

  Started : Saturday, December 20, 2025 6:53:41 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

    Files : *.*
	    
  Options : *.* /TEE /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:3 /W:5 

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

	                   5	C:\src\
	    New File  		   1.0 g	big_1.bin

	    New File  		   2.0 g	big_4.bin

~~~~~~~~~~~~~~~
中略 共有化停止
~~~~~~~~~~~~~~~


	    New File  		   2.0 g	big_4.bin
2025/12/20 06:54:33 ERROR 67 (0x00000043) Copying File C:\src\big_4.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_4.bin
2025/12/20 06:54:38 ERROR 67 (0x00000043) Copying File C:\src\big_4.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_4.bin
2025/12/20 06:54:43 ERROR 67 (0x00000043) Copying File C:\src\big_4.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_4.bin
2025/12/20 06:54:48 ERROR 67 (0x00000043) Copying File C:\src\big_4.bin
The network name cannot be found.



ERROR: RETRY LIMIT EXCEEDED.

	    New File  		   2.0 g	big_5.bin
2025/12/20 06:54:48 ERROR 67 (0x00000043) Copying File C:\src\big_5.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_5.bin
2025/12/20 06:54:53 ERROR 67 (0x00000043) Copying File C:\src\big_5.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_5.bin
2025/12/20 06:54:58 ERROR 67 (0x00000043) Copying File C:\src\big_5.bin
The network name cannot be found.

Waiting 5 seconds... Retrying...
	    New File  		   2.0 g	big_5.bin
2025/12/20 06:55:03 ERROR 67 (0x00000043) Copying File C:\src\big_5.bin
The network name cannot be found.


ERROR: RETRY LIMIT EXCEEDED.


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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         5         0         0         0         5         0
   Bytes :   7.000 g         0         0         0   7.000 g         0
   Times :   0:01:22   0:00:06                       0:01:15   0:00:00
   Ended : Saturday, December 20, 2025 6:55:03 AM

実行中に共有化を再開して、止めて、改めて再開してみます。
その時のログの中身になります。

big_1.bin は1回目で途中までコピーされており、2回目の実行時に /Z オプションにより
13.8% の地点から再開されていることがわかります。copyやxcopyだとこういう挙動にならずに最初からbig_1.binをコピーしなおすという事になるようです。大きなファイルでこれが発生したら悲惨ですね。
big_4.binのタイミングで共有化を止めて再開したのですが、再開された後で処理が途中から継続されて完了していることがわかります。

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

  Started : Saturday, December 20, 2025 7:00:12 AM
   Source : C:\src\
     Dest : \\192.168.1.48\dst\

    Files : *.*
	    
  Options : *.* /TEE /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:3 /W:5 

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

	                   5	C:\src\
2025/12/20 07:00:13 ERROR 5 (0x00000005) Accessing Destination Directory \\192.168.1.48\dst\
Access is denied.

Waiting 5 seconds... Retrying...
2025/12/20 07:00:18 ERROR 5 (0x00000005) Accessing Destination Directory \\192.168.1.48\dst\
Access is denied.

Waiting 5 seconds... Retrying...
	    Newer     		   1.0 g	big_1.bin
 13.8%
 13.9%
 13.9%
 13.9%

100%        Newer                  1.0 g        big_1.bin
100%        New File               1.0 g        big_2.bin
100%        New File               1.0 g        big_3.bin
 43.4%      New File               2.0 g        big_4.bin
2025/12/20 07:03:16 ERROR 59 (0x0000003B) Copying File C:\src\big_4.bin
An unexpected network error occurred.
Waiting 5 seconds... Retrying...
            New File               2.0 g        big_4.bin
2025/12/20 07:03:21 ERROR 67 (0x00000043) Copying File C:\src\big_4.bin
The network name cannot be found.
Waiting 5 seconds... Retrying...
            New File               2.0 g        big_4.bin
2025/12/20 07:03:26 ERROR 5 (0x00000005) Copying File C:\src\big_4.bin
Access is denied.
Waiting 5 seconds... Retrying...
100%        New File               2.0 g        big_4.bin
100%        New File               2.0 g        big_5.bin

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

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         5         5         0         0         0         0
   Bytes :   7.000 g   7.000 g         0         0         0         0
   Times :   0:05:41   0:05:16                       0:00:25   0:00:00


   Speed :           23,783,764 Bytes/sec.
   Speed :            1,360.918 MegaBytes/min.
   Ended : Saturday, December 20, 2025 7:05:54 AM

エラーのところで、
ERROR 59 (0x0000003B) やERROR 67 (0x00000043) 、ERROR 5 (0x00000005)等を確認してみたら、私が共有を再開する時にWindowsのフォルダ設定をプロパティでガチャガチャ編集していることがうかがえて面白いです(?)

ERROR 59(共有が突然停止)
  ↓ 5秒待機
ERROR 67(共有が見つからない)
  ↓ 5秒待機
ERROR 5(共有が再開されたが、まだアクセス権が不安定)
  ↓ 5秒待機
100% 完了!(共有が完全に復旧、43.4%から再開)

なんとなくRobocopyへ苦手意識が払しょくしました。
これで案件で対応できるかな・・・

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?