我已经编写了一个批处理文件,该文件的目的是停止vm,将备份(复制整个Virtualbox VM)文件夹备份到其他驱动器,然后启动VM。
代码如下:
@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
但是我有两个问题。 1. robocopy本身不起作用,请在下面找到执行错误消息
-------------------------------------------------------------------------------
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 !
不是很重要,但是下面的代码仍然无效,这意味着即使您执行多次,它也应该只创建一个文件夹。当我多次执行时,它会创建像
这样的嵌套文件夹E:\ Backup-20-Nov-2018 \ Backup-20-Nov-2018 \ Backup-20-Nov-2018 \
Buggy代码:
if not exist Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2% mkdir Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%
任何帮助将不胜感激。
答案 0 :(得分:3)
您的问题是,当包含引号来保护空格时,您需要避免在文件夹路径中结束反斜杠,因为它们将转义双引号,因此参数不会在您认为的地方结束(有关详细信息,请参见this question) )。
robocopy "C:\Users\user\VirtualBox VMs" "e:\Backup-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%" /mir
答案 1 :(得分:1)
修正一些双引号,其他方法,并使用timeout 30
代替ping
命令:
@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