Autom. Backup via bat file

Discussions related to using VirtualBox on Windows hosts.
Post Reply
SkyCaptain4052
Posts: 8
Joined: 12. Oct 2017, 10:50

Autom. Backup via bat file

Post by SkyCaptain4052 »

Hi everyone,

I have a little problem. after a good year of manually backing up my server, i was thinking about finally getting around to create a bat file to do it for me with a simple double click. Problem is, i cant seem to figure out how.

So first thing i did was to follow these instructions:

Code: Select all

"c:\Program Files\Oracle\VirtualBox\vboxmanage.exe" controlvm UbuntuServer savestate
xcopy "C:\Users\da\VirtualBox VMs\UbuntuServer\*" e:\backup_vm\%date%\* /Y /S
"c:\Program Files\Oracle\VirtualBox\vboxmanage.exe" startvm UbuntuServer
this wont work and im getting a bunch of errors.
"vboxmanage.exe error code e_fail (0x80004005) - uspecified error (extended info not available)"
"VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Write)" at line 493 of file VBoxManageModifyVM.cpp"

then further down i get
"xcopy "C:\Users\da\VirtualBox VMs\UbuntuServer\*" e:\backup_vm\%date%\* /Y /S"
invalid number of parameters

does anyone have a script i can copy? a simple "save state of machine" -> "copy disk file" -> "continue machine". and if we're really fancy, add a archiving functionality like in this example which again, i cant get to work:

Code: Select all

@ECHO OFF
CLS
 
SET "VM=Ubuntu Server 14.04"
SET "VM_DIR=C:\VirtualBox VMs\"
SET "BACKUP_DIR=C:\VirtualBox VMs\backup\"
SET VBOXMANAGE="C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
SET RAR="C:\Program Files\WinRAR\Rar.exe"
SET CICLOS=10
SET PAUSAS=5
SET SOLICITUD_APAGADO=0
SET ERROR=0
SET RUNNING_INICIAL=2
 
ECHO Starting backup VM "%VM%"...
 
:check_running
%VBOXMANAGE% list runningvms > %TEMP%\runningvms.txt
FIND "%VM%" %TEMP%\runningvms.txt > nul
 
IF %errorlevel% EQU 0 (
    SET RUNNING=1
) ELSE (
    SET RUNNING=0
)
 
IF %RUNNING_INICIAL% EQU 2 (
    SET RUNNING_INICIAL=%RUNNING%
)
 
IF %CICLOS% GTR 0 (
    IF %RUNNING% EQU 1 (
        IF %SOLICITUD_APAGADO% EQU 0 (
            SET SOLICITUD_APAGADO=1
            ECHO Power off VM "%VM%"...
            %VBOXMANAGE% controlvm "%VM%" acpipowerbutton
        )
 
        ECHO Waiting VM "%VM%" shut down...
        TIMEOUT /t %PAUSAS% /nobreak > nul
        SET /a CICLOS-=1
        GOTO check_running
    ) ELSE (
        ECHO VM "%VM%" is power off now...
    )
)
 
DEL %TEMP%\runningvms.txt
 
IF %RUNNING% EQU 1 (
    SET ERROR=1
    GOTO end
)
 
:copy_vm
ECHO Copying VM "%VM%"...
TIMEOUT /t 3 /nobreak > nul
XCOPY "%VM_DIR%%VM%" "%BACKUP_DIR%%VM%" /E /I /Y
 
IF %errorlevel% NEQ 0 (
    SET ERROR=2
    GOTO end
)
 
:start_vm
IF %RUNNING_INICIAL% EQU 1 (
    ECHO Starting VM "%VM%"...
    %VBOXMANAGE% startvm "%VM%"
     
    IF %errorlevel% NEQ 0 (
        SET ERROR=3
        GOTO end
    )
)
 
:compress_backup
ECHO Compressing backup VM "%VM%"...
SET FECHA=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
%RAR% a -r -ep1 -o+ "%BACKUP_DIR%%VM% %FECHA%.rar" "%BACKUP_DIR%%VM%"
 
IF %errorlevel% NEQ 0 (
    SET ERROR=4
    GOTO end
)
 
:delete_uncompressed_backup
ECHO Removing uncompressed backup VM "%VM%"...
RMDIR "%BACKUP_DIR%%VM%" /S /Q
 
IF %errorlevel% NEQ 0 (
    SET ERROR=5
    GOTO end
)
 
:end
IF %ERROR% GTR 0 (
    ECHO Error ^(%ERROR%^) while creating backup VM "%VM%".
) ELSE (
    ECHO Backup finished OK.
)

ive also followed this instruction here to little success

https://forums.bunsenlabs.org/viewtopic.php?id=1688

______________________________________________________________________


edit, ok no, there probably is a proper issue somewhere.

I created a simple BAT file to see weather or not im just too stupid to work with these things, but when i did something really simple like this

Code: Select all

cd C:\Program Files\Oracle\VirtualBox
VBoxManage controlvm Iway savestate


cmd /k

i still get

Code: Select all

VBoxManage.exe: error: Code E_FAIL (0x80004005) - Unspecified error (extended info not available)
VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Shared)" at line 388 of file VBoxManageControlVM.cpp

returned, so i think there must be something wrong with my installation...

in any event im happy to provide any information needed! thanks a lot!
andyp73
Volunteer
Posts: 1631
Joined: 25. May 2010, 23:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Assorted Linux, Windows Server 2012, DOS, Windows 10, BIOS/UEFI emulation

Re: Autom. Backup via bat file

Post by andyp73 »

The batch file looks to have quotes used incorrectly when setting variables and running commands. I think the fixed version is:

Code: Select all

@ECHO OFF
CLS

SET VM=Ubuntu Server 14.04
SET VM_DIR=C:\VirtualBox VMs\
SET BACKUP_DIR=%VM_DIR%backup\
SET VBOXMANAGE=C:\Program Files\Oracle\VirtualBox\VBoxManage.exe
SET RAR=C:\Program Files\WinRAR\Rar.exe
SET CICLOS=10
SET PAUSAS=5
SET SOLICITUD_APAGADO=0
SET ERROR=0
SET RUNNING_INICIAL=2
 
ECHO Starting backup VM "%VM%"...
 
:check_running
"%VBOXMANAGE%" list runningvms > %TEMP%\runningvms.txt
FIND "%VM%" %TEMP%\runningvms.txt > nul
 
IF %errorlevel% EQU 0 (
    SET RUNNING=1
) ELSE (
    SET RUNNING=0
)
 
IF %RUNNING_INICIAL% EQU 2 (
    SET RUNNING_INICIAL=%RUNNING%
)
 
IF %CICLOS% GTR 0 (
    IF %RUNNING% EQU 1 (
        IF %SOLICITUD_APAGADO% EQU 0 (
            SET SOLICITUD_APAGADO=1
            ECHO Power off VM "%VM%"...
            "%VBOXMANAGE%" controlvm "%VM%" acpipowerbutton
        )
 
        ECHO Waiting VM "%VM%" shut down...
        TIMEOUT /t %PAUSAS% /nobreak > nul
        SET /a CICLOS-=1
        GOTO check_running
    ) ELSE (
        ECHO VM "%VM%" is power off now...
    )
)
 
DEL %TEMP%\runningvms.txt
 
IF %RUNNING% EQU 1 (
    SET ERROR=1
    GOTO end
)
 
:copy_vm
ECHO Copying VM "%VM%"...
TIMEOUT /t 3 /nobreak > nul
XCOPY "%VM_DIR%%VM%" "%BACKUP_DIR%%VM%" /E /I /Y /Q
 
IF %errorlevel% NEQ 0 (
    SET ERROR=2
    GOTO end
)
 
:start_vm
IF %RUNNING_INICIAL% EQU 1 (
    ECHO Starting VM "%VM%"...
    "%VBOXMANAGE%" startvm "%VM%"
     
    IF %errorlevel% NEQ 0 (
        SET ERROR=3
        GOTO end
    )
)
 
:compress_backup
ECHO Compressing backup VM "%VM%"...
SET FECHA=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
"%RAR%" a -r -ep1 -o+ "%BACKUP_DIR%%VM% %FECHA%.rar" "%BACKUP_DIR%%VM%"
 
IF %errorlevel% NEQ 0 (
    SET ERROR=4
    GOTO end
)
 
:delete_uncompressed_backup
ECHO Removing uncompressed backup VM "%VM%"...
RMDIR "%BACKUP_DIR%%VM%" /S /Q
 
IF %errorlevel% NEQ 0 (
    SET ERROR=5
    GOTO end
)
 
:end
IF %ERROR% GTR 0 (
    ECHO Error ^(%ERROR%^) while creating backup VM "%VM%".
) ELSE (
    ECHO Backup finished OK.
)
-Andy.
My crystal ball is currently broken. If you want assistance you are going to have to give me all of the necessary information.
Please don't ask me to do your homework for you, I have more than enough of my own things to do.
SkyCaptain4052
Posts: 8
Joined: 12. Oct 2017, 10:50

Re: Autom. Backup via bat file

Post by SkyCaptain4052 »

Hi Andy,

Thanks for your reply. I've gotten to the point of writing my own little bat file, ill post the code further bellow.
very important though, something that took my a while... you have to right click and run the bat file as administrator for anything to work.

all in all a pretty decent solution. the 100GB vhd file gets compressed to somewhere around 7GB (the server hard drive is pretty empty, thats probably helping a lot) and i can keep a rough 200 days of backup on the hard drive. So if anything ever goes bad on this ancient server, theres enough Backups to go wayyyyy back to when things were still working

anyways, heres the scrip:

Code: Select all

@echo off
echo ========================================================================================
echo ========================================================================================
echo ===   ### #     #    #    #     #    ######     #     #####  #    # #     # ######   === 
echo ===    #  #  #  #   # #    #   #     #     #   # #   #     # #   #  #     # #     #  ===
echo ===    #  #  #  #  #   #    # #      #     #  #   #  #       #  #   #     # #     #  ===
echo ===    #  #  #  # #     #    #       ######  #     # #       ###    #     # ######   ===
echo ===    #  #  #  # #######    #       #     # ####### #       #  #   #     # #        ===
echo ===    #  #  #  # #     #    #       #     # #     # #     # #   #  #     # #        ===
echo ===   ###  ## ##  #     #    #       ######  #     #  #####  #    #  #####  #        ===
echo ========================================================================================
echo ========================================================================================
echo Starting 1 
echo starting 2
echo starting......................3
@echo on

cd C:\Program Files\Oracle\VirtualBox
	VBoxManage controlvm Iway savestate

timeout 10

robocopy D:\iway\ C:\backup_vm

timeout 10

cd C:\Program Files\Oracle\VirtualBox
VBoxManage startvm Iway 

cd "C:\Program Files\7-Zip
	7z.exe a -t7z -m0=LZMA2:d64k:fb32 -ms=8m -mmt=30 -mx=1 "C:\backup_vm\"%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%_Backup".7z" "C:\backup_vm\test_iway.vhd" -mx9


pause
cheers
Emmanuel
Post Reply