0

I'm trying to use robocopy to move files to a remote machine. This is being done on a Windows machine through a file script.sh that is executed inside Git Bash.

Background

First I use net use to add the shared directory:

NET USE \\\\$DEPLOY_SERVER\\$DEPLOY_SHARE /user:$DEPLOY_SERVER\\$DEPLOY_USER $DEPLOY_PASS

This works fine.

Issue

Then I go to copy the files with robocopy:

robocopy . \\\\$DEPLOY_SERVER\\$DEPLOY_SHARE\\$DEPLOY_DIR /MIR

which fails with error code 16.

From the robocopy docs, 16 is Serious error. Robocopy did not copy any files.Either a usage error or an error due to insufficient access privileges on the source or destination directories.

Running it without /MIR works fine with no errors.

Error log

When run with /MIR I get the error:

-------------------------------------------------------------------------------
ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------    
Started : Mon Dec 18 15:46:26 2017    
Source - C:\<path to current dir>\
 Dest - \\<deploy server>\<deploy share>\<deploy dir>\    
Files :
Options : /COPY:DAT /R:1000000 /W:30    
------------------------------------------------------------------------------    
ERROR : Invalid Parameter #3 : "C:/Users/<my user>/AppData/Local/Programs/Git/MIR"    
   Simple Usage :: ROBOCOPY source destination /MIR    
         source :: Source Directory (drive:\path or \\server\share\path).
    destination :: Destination Dir  (drive:\path or \\server\share\path).
           /MIR :: Mirror a complete directory tree.    
For more usage information run ROBOCOPY /?    
****  /MIR can DELETE files as well as copy them !

Further error description

This happens for any options, not just /MIR, this is just the simplest form of the error.

I've read many questions very similar to this, the common answer is to put your paths in double quotes in case there are spaces. My paths do not have spaces but I've tried that as well and got the same error. Additionally this would result in the "Source" and "Dest" paths in the robocopy output to be incorrect, but they are correct for me.

I have no idea why I would be going to that git directory and looking for MIR, or why it's not just parsing the options correctly. Additionally I checked and MIR does not exist inside C:/Users/<my user>/AppData/Local/Programs/Git/.

Research

Other questions that are similar but didn't help me:

Conclusion

Any help would be greatly appreciated! I'm also open to using something other than robocopy, but it would need to work with a remote machine, be runnable from a shell script, support mirroring, and not use ssh(net use works for robocopy so ideally something else like that).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Josh G
  • 644
  • 7
  • 21
  • The [tag:shell] tag (and "shell script" phrasing) is not typically used for questions about Windows -- as the description says, questions containing that tag are generally about POSIX-family shells on UNIXlike operating systems. Perhaps you should be tagging [tag:cmd] or [tag:powershell] (as appropriate) instead? (Or if this is being run in Cygwin or msys bash, please specify that explicitly). – Charles Duffy Dec 18 '17 at 21:08
  • @CharlesDuffy thanks! I added it to the top, this is on a Windows machine, but it's run inside Git Bash or using sh.exe. – Josh G Dec 18 '17 at 21:12
  • Which of those two? Even if `/bin/sh` in the same binary as Git Bash, it runs in POSIX mode when started with that name, so it's a different featureset exposed. – Charles Duffy Dec 18 '17 at 21:14
  • @CharlesDuffy the error occurs either way, but for the question I'm going with Git Bash, and the question has been updated accordingly – Josh G Dec 18 '17 at 21:16
  • If you run `bash -x yourscript`, does the output differ materially from what you'd expect? (If, for instance, the log says it's passing `$'/MIR\r'` instead of `/MIR`, that's an example of the kind of difference that would be expected to cause this problem -- and implies that your script was saved with DOS newlines instead of UNIX ones; incidentally, that's literally the *very first thing* that the [`bash` tag wiki](https://stackoverflow.com/tags/bash/info) says to check for in the section *"Before asking about problematic code"*). – Charles Duffy Dec 18 '17 at 21:18
  • @CharlesDuffy yep I checked that as well, and the line looked fine: `robocopy /MIR /ns /nc /ndl /np`, no funky characters or anything – Josh G Dec 18 '17 at 21:27
  • Just for a sanity check I did `tr -d '\r' < script.sh > script_new.sh` and then `cat -v script_new.sh` to confirm that there definitely isn't the `^M` character, then `bash -x script_new.sh` and saw the same error, with the line in question ending up the same way as well – Josh G Dec 18 '17 at 21:35

2 Answers2

3

I got it working by escaping the /MIR Parameter with an extra forward slash:

robocopy "C:\source" "C:\destination" //MIR

Hope this helps ;)

1

It happens when need admin privileges/ or pop to create file into that directory

So try to have a log/temp directory and try to add log file into that log directory

mkdir log\
e.g: robocopy . \\\\$DEPLOY_SERVER\\$DEPLOY_SHARE\\$DEPLOY_DIR 
/log:"\\$ServerShare\log\copy_log.log"
Nisarg
  • 1,631
  • 6
  • 19
  • 31