0

I have written a batch file, the aim of the file is to stop vm, take a back up (Copy the entire Virtualbox VMs) folder to a different drive, start VMs.

Code is as below:

@echo on
cls
echo "Change directory to Virtualbox root directory"

cd /d "c:\Program Files\Oracle\VirtualBox"

echo "Powering off Virtual machines"

VBoxManage controlvm "centos74.master" poweroff
PING localhost -n 30 >NUL

VBoxManage controlvm "centos74.agent" poweroff
PING localhost -n 30 >NUL

echo "Commence backup work"

cd /d E:
if not exist Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2% mkdir Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%
REM cd Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%
robocopy C:\Users\user\VirtualBox VMs\ E:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%

echo "Change directory to Virtualbox root directory"

cd /d "c:\Program Files\Oracle\VirtualBox"

echo "Starting Virtual machines"

VBoxManage startvm "centos74.master"

PING localhost -n 30 >NUL

VBoxManage startvm "centos74.agent"

PING localhost -n 30 >NUL

@echo off

I however have two issues. 1. robocopy in itself doesn't work, please find below execution error message

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

  Started : Tuesday, 20 November 2018 9:19:03 AM
   Source - C:\Users\user\VirtualBox\
     Dest - E:\VMs\

    Files :
  Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30

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

ERROR : Invalid Parameter #3 : "E:\Backup-2018-11-20"

       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 !
  1. Not so important but still the below code is ineffective, meaning it should only create one and only folder even if you execute multiple times. When I execute multiple times it creates nested folder like

    E:\Backup-20-Nov-2018\Backup-20-Nov-2018\Backup-20-Nov-2018\

Buggy code:

if not exist Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2% mkdir Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%

Any assistance will be greatly appreciated.

learner
  • 2,480
  • 10
  • 50
  • 94
  • There is a space in the path `C:\Users\user\VirtualBox VMs\` so you have to double quote it (you should do that for all path variables possibly containing spaces) –  Nov 20 '18 at 00:14
  • if not exist "Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" mkdir "Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" robocopy "C:\Users\user\VirtualBox VMs\" "E:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" – learner Nov 20 '18 at 00:42
  • Still not working – learner Nov 20 '18 at 00:42
  • Got it working by xcopy, robocopy is simply rubbish! – learner Nov 20 '18 at 01:44
  • 1
    Robocopy is by far superior. Unfortunate you do not understand the syntax and are unable to communicate how you are using it. Just telling us it is not working does not help anyone understand how to fix the problem. – Squashman Nov 20 '18 at 02:23
  • @Squashman I would love to have it working but it keeps on complaining.. – learner Nov 20 '18 at 04:00
  • `------------------------------------------------------------------------------- ROBOCOPY :: Robust File Copy for Windows ------------------------------------------------------------------------------- Started : Tuesday, 20 November 2018 2:58:33 PM Source - Dest - Files : Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30 ------------------------------------------------------------------------------` – learner Nov 20 '18 at 04:00
  • `robocopy "C:\Users\user\VirtualBox VMs\*" "E:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%\" /E /TEE /LOG+ "CompleBackupLog.txt"` – learner Nov 20 '18 at 04:03
  • @Squashman Kindly let me know what I am doing wrong here? – learner Nov 20 '18 at 04:03
  • Why did you add an asterisk to the source? – Squashman Nov 20 '18 at 04:10
  • To tell robocopy, copy everything under that directory, that includes all files and folders. – learner Nov 20 '18 at 04:23
  • `robocopy "C:\Users\user\VirtualBox VMs\" "E:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%\" /E /TEE /LOG+:"CompleBackupLog.txt"` – learner Nov 20 '18 at 04:23
  • `Log File : E:\CompleBackupLog.txt Started : Tuesday, 20 November 2018 3:21:27 PM Source : C:\Users\user\VirtualBox VMs" E:\Backup-2018-11-20"\ Dest - Files : *.* Options : *.* /TEE /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 ------------------------------------------------------------------------------ ERROR : No Destination Directory Specified. **** /MIR can DELETE files as well as copy them !` – learner Nov 20 '18 at 04:26
  • I now fail to understand why robocopy takes destination into source? I have left a space between source and destination – learner Nov 20 '18 at 04:27

2 Answers2

3

Your problem is that when including quotes to protect spaces, you need to avoid ending backslashes in the folder paths because they will escape the double quotes so the arguments will not end where you think (see this question for more information).

robocopy "C:\Users\user\VirtualBox VMs" "e:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" /mir
MC ND
  • 69,615
  • 8
  • 84
  • 126
1

Fixing some double quoting, other things and using timeout 30 instead of ping command:

@echo on
cls
echo "Change directory to Virtualbox root directory"
cd /d "c:\Program Files\Oracle\VirtualBox"
echo "Powering off Virtual machines"
VBoxManage controlvm "centos74.master" poweroff
timeout 30
VBoxManage controlvm "centos74.agent" poweroff
timeout 30
echo "Commence backup work"
cd /d E:
if not exist "Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" mkdir "Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%"
REM cd "Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%"
robocopy "C:\Users\user\VirtualBox VMs" "E:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" /r:0
echo "Change directory to Virtualbox root directory"
cd /d "c:\Program Files\Oracle\VirtualBox"
echo "Starting Virtual machines"
VBoxManage startvm "centos74.master"
timeout 30
VBoxManage startvm "centos74.agent"
timeout 30
@echo off
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Many thanks to both of you. Since Gerhard went an extra mile, I have favored his as solution but technically both are right. – learner Nov 20 '18 at 14:13