Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Last active October 27, 2023 10:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbrnc/4560608 to your computer and use it in GitHub Desktop.
Save fbrnc/4560608 to your computer and use it in GitHub Desktop.
Daily backup of my devbox virtualbox machine during lunch time
@ECHO OFF
SET BACKUPSTORAGEPATH=\\AOE-nas\fabrizio.branca\backup\Virtualbox\
SET VBOXMANAGE="C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"
SET VMNAME=devbox
IF NOT EXIST %BACKUPSTORAGEPATH% GOTO NOBACKUPDIRFOUND
echo Starting backup of VM '%VMNAME%' to %BACKUPSTORAGEPATH%
:: Check if vm is currently running
%VBOXMANAGE% list runningvms > runningvms.txt
FINDSTR /m %VMNAME% runningvms.txt > nul
if %errorlevel%==0 (
SET RUNNING='1'
echo VM is running
echo Saving VM state...
%VBOXMANAGE% controlvm %VMNAME% savestate
) else (
SET RUNNING='0'
echo VM is not running
)
:: Deleting old backup if exists
IF NOT EXIST %BACKUPSTORAGEPATH%\%VMNAME%.ova.old GOTO SKIPDELOLD
echo Deleting old backup
del "%BACKUPSTORAGEPATH%\%VMNAME%.ova.old"
:SKIPDELOLD
:: Move current backup
IF NOT EXIST %BACKUPSTORAGEPATH%\%VMNAME%.ova GOTO SKIPMOVE
echo Moving current backup to old backup
ren "%BACKUPSTORAGEPATH%\%VMNAME%.ova" "%VMNAME%.ova.old"
:SKIPMOVE
:: Do the actual backup
echo Backing up to %BACKUPSTORAGEPATH%\%VMNAME%.ova. Go and get lunch. This will take some time!
%VBOXMANAGE% export %VMNAME% --output "%BACKUPSTORAGEPATH%\%VMNAME%.ova"
:: Restart VM if needed
if %RUNNING%=='1' (
echo VM was running before. So starting it again.
%VBOXMANAGE% startvm %VMNAME%
) else (
echo VM wasn't running before. So it won't be started now.
)
del runningvms.txt
echo Backup completed. Have a nice day.
pause
exit
:NOBACKUPDIRFOUND
echo No backup dir found in %BACKUPSTORAGEPATH%. Skipping backup.
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment