Clicky

20230221

Backup script for Xenserver/XCP-ng Virtual Machine OVA files

The next batch file creates a backup ("vm-export") of Xenserver and XCP-ng Virtual Machines.

Features:

  • On demand backups or scheduled backups
  • Creates logfile on local computer
  • Does not have credentials in the script
  • Will show all VMs when the script is invoked without parameters
  • Shows time/duration of the backup
  • Makes a local copy first (speed!)
  • Can copy to NAS other network location

Maybe first install the XCP-ng-Center-20.04.01.33.msi software.

Running the script without a parameter:

C:\scripts>backupXenVM.bat
*** Tue 02/21/2023 14:38:00.02 --- Start OVA backup -------------------------------------------------------------------
*** Tue 02/21/2023 14:38:00.02 VMs:
*** Tue 02/21/2023 14:38:00.04
     name-label ( RW): Control domain on host: xcp-ng-abcdefg
     name-label ( RW): x16. Airsonic
     name-label ( RW): x14. mailAdmin
     name-label ( RW): x10. ARCHDC001
     name-label ( RW): x15. Roundcube
*** Tue 02/21/2023 14:38:00.29
*** Tue 02/21/2023 14:38:00.29 Usage: backupXenVM.bat "{name-label}"
*** Tue 02/21/2023 14:38:00.29

 

A typical run of the script looks like:

C:\scripts>backupXenVM.bat "x15. Roundcube"
*** Tue 02/21/2023 14:40:11.15 --- Start OVA backup -------------------------------------------------------------------
The command completed successfully.

New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           E:        \\192.168.1.3\Backups\xe
                                                Microsoft Windows Network
The command completed successfully.

*** Tue 02/21/2023 14:40:11.46 Tue 02/21/2023 14:40:11.44 - Backup VM: "x15. Roundcube"
*** Tue 02/21/2023 14:40:11.48
uuid ( RO)           : d2dccf2b-d9d9-e801-1e3f-59f426bc0bb1
     name-label ( RW): x15. Roundcube
    power-state ( RO): running


*** Tue 02/21/2023 14:40:11.66 Shutdown VM with name-label: "x15. Roundcube"...
*** Tue 02/21/2023 14:40:18.56 Backup of VM with name-label: "x15. Roundcube"...
*** Tue 02/21/2023 14:41:39.84 Restarting VM with name-label: "x15. Roundcube"...
*** Tue 02/21/2023 14:41:39.89 Copying file to NAS...
*** Tue 02/21/2023 14:42:50.75 Backup procedure of VM "x15. Roundcube" ready.
02/21/2023  02:41 PM     4,969,158,144 x15. Roundcube.ova
*** Tue 02/21/2023 14:42:51.35 Start  : Tue 02/21/2023 14:40:11.15
*** Tue 02/21/2023 14:42:51.35 VM up  : Tue 02/21/2023 14:41:39.89
*** Tue 02/21/2023 14:42:51.37 End    : Tue 02/21/2023 14:42:51.35
*** Tue 02/21/2023 14:42:51.37 Ready.


@echo off

    cls

::--- User Specific vars and constants...
    set Xenserver=192.168.1.5
    set UNC=\\192.168.1.2\Backups\xe
    set DRV=E:

::--- Vars and constants...
    set xe="C:\Program Files (x86)\XCP-ng Center\xe.exe"
    set t=%date% %time%
    set userName=root
    REM --- Store the Xen server 'root' password in seperate file. Still in plain text, but not part of the script..
    for /f %%i in (c:\scripts\pw.txt) do set pw=%%i
    set localBackupFolder=d:\temp\xe
    md "%localBackupFolder%" >nul 2>nul

    ::--- Remove quotes from VM label-name...
    set nameLabel=%~1
    ::--- Set backup paths...
    set localBackupFile=%localBackupFolder%\%nameLabel%.ova
    set remotefileName="%DRV%\%nameLabel%.ova"

    call :print --- Start OVA backup -------------------------------------------------------------------

::--- No VM label-name provided...
    if %1.==. (

        call :print VMs:
        %xe% -s %Xenserver% -u %userName% -pw %pw% vm-list | find  "name-label"

        call :print
        call :print Usage: %0 "{name-label}"

        goto :EOF

    )


::--- Check VM label-name---
    %xe% -s %Xenserver% -u %userName% -pw %pw% vm-list | find "%nameLabel%" >nul
    if %errorlevel% NEQ 0 (

        call :print VM not found...
        goto :EndAll

    )  


:doBackup
   
    setlocal ENABLEDELAYEDEXPANSION

    net use %DRV% /d /y >nul 2>nul
    md "%DRV%\previous" >nul 2>nul
    net use %DRV% %unc% >nul 2>nul
    net use %DRV%


   
    call :print %date% %time% - Backup VM: "%nameLabel%"
    call :print

    ::--- Show VM data...
    %xe% -s %Xenserver% -u %userName% -pw %pw% vm-list name-label="%nameLabel%"

    ::--- Delete 'previous' backup file and move current backup to 'previous' folder ...
    del /q "%DRV%\previous\%nameLabel%.ova" >nul 2>nul
    move   "%remoteFileName%" "%DRV%\previous\" >nul 2>nul

    ::--- Shutdown VM...
    call :print Shutdown VM with name-label  : "%nameLabel%"...
    %xe% -s %Xenserver% -u %userName% -pw %pw% vm-shutdown name-label="%nameLabel%"
    ping -n 3 localhost >nul

    ::--- Backup VM to local storage...
    call :print Backup of VM with name-label : "%nameLabel%" to "%localBackupFile%"...
    %xe% -s %Xenserver% -u %userName% -pw %pw% vm-export name-label="%nameLabel%" filename="%localBackupFile%" >nul

    ::--- Restart VM...
    call :print Restarting VM with name-label: "%nameLabel%"...
    %xe% -s %Xenserver% -u %userName% -pw %pw% vm-start name-label="%nameLabel%"    
    set VMstart=%date% %time%
   
    ::--- Copy backup to NAS...
    call :print Copying file to NAS...
    dir "%localBackupfile%" | find "%nameLabel%"
    xcopy /y "%localBackupFile%" %DRV%\ >nul
    del   /q "%localBackupFile%" >nul 2>nul
   
    call :print Backup procedure of VM "%nameLabel%" ready.


:EndAll

    net use %DRV% /d /y >nul 2>nul

    call :print Scripts start : %t%
    call :print VM start      : %vmstart%
    call :print Script end    : %date% %time%

    call :print Ready.

goto :EOF

   
:print
   
    echo *** %date% %time% %*
    echo *** %date% %time% %* >> "c:\logs\%nameLabel%.log"

goto :EOF

No comments :

Post a Comment

Real Time Web Analytics