Windows Update Script

Hello,

i have written a script to Update my openHAB 2.0 instance to a new Snapshoot.
I looked at the unix update script as a starting point but my folder strukture is different.
Please be careful and make a Backup … its my first Version :wink:
I hope it will help or is good for the dokumentation? Feel free to use it.

I have a Intel NUC28xx with Windows 10 on a SSD and my Strukture under C:\ is

  • Services
    • openHAB2
      • aktuell (for the current running)
      • new (for the unziped Snapshoot Folder)
        • openhab-2.1.0 (example of a new Snapshoot)
      • backup (will be created during the Upgrade)
      • tmp (will be created during the Upgrade for log and fallback)
      • _archiv (vor old backups)

1.) You need to download a Snapshoot to the new folder and unzip it.
2.) Delete Backup and tmp …or move it to _Archiv
3.) Create a Batch “openHABupdate.bat” and copy the Script to it
4.) Stop openHAB

@echo off
set WorkingDir=%cd%
set TempDir=tmp
set NewDir=new
set BackupDir=backup
set CurrentDir=aktuell

set ErrorMsg=""

echo.
echo.#########################################
echo.          Todos before start
echo.#########################################"

echo.
echo.Stop your running openHAB instance
echo.Make a Backup of your existing running instance. THIS IS IMPORTEND!!!
echo.Run the Script one step Before the openHAB Folder. So you can see a folder like .\openHAB-2.0.0
echo.Please copy the new Version to the %NewDir% folder and unzip it. Like .\new\openhab-2.1.0.
echo.When ready press a key ...
@pause > NUL

echo.
echo.Set the temporary directories.
echo.==============================
  if exist %TempDir% rmdir %TempDir% /S /Q && echo.Old TempDir %TempDir% Removed || set ErrorMsg=Failed to delete old: %TempDir%
  if ERRORLEVEL 1 goto error

  mkdir %TempDir% && echo %time% - Start > .\tmp\LOG-%date%.txt || set ErrorMsg=Failed to make temporary directory: %TempDir%
  if ERRORLEVEL 1 goto error

  echo.New TempDir %TempDir% created
  echo.

echo.Check if there is a new Version
echo.===============================

  set newVersion=empty
  for /F "delims=" %%i IN ('dir "%WorkingDir%\%NewDir%" /b /ad-h /t:c /od') do set newVersion=%%i

  if newVersion==empty (
    set ErrorMsg=No new Version found
    goto error
  ) else (
    echo %time% - New openHAB Verison found: %newVersion% >> .\tmp\LOG-%date%.txt
    echo.New openHAB Verison found: %newVersion% 
  )
  echo.

echo.Make a Backup of the Current Installation
echo.=========================================

  if exist %BackupDir% (
    set ErrorMsg=There is already a BackupDirectory!!!
    goto error
  ) else (
    @timeout 5
    echo %time% - Backup openHAB current Directory: %CurrentDir% to %BackupDir% >> .\tmp\LOG-%date%.txt
    echo.Backup openHAB current Directory: %CurrentDir% to %BackupDir%
    robocopy %CurrentDir% %BackupDir% /E
    echo %time% - Backup finished >> .\tmp\LOG-%date%.txt
  ) 


echo.
echo.#########################################
echo.       openHAB 2.x.x update script
echo.#########################################"
echo.

  echo.The script will attempt to update openHAB to version %newVersion%

  choice /c "YN" /M "Is this okay"

  if errorlevel 1 echo %time% - Update accepted >> .\tmp\LOG-%date%.txt 
  if errorlevel 2 (
    echo.Cancelling update...
    rmdir %TempDir% /S /Q
    goto exit
  )

echo.Remove only the files that are to be replaced.
echo.==============================================
  echo.Removing openHAB System Files...
  echo %time% - Removing openHAB System Files... >> .\tmp\LOG-%date%.txt 
  
  mkdir "%TempDir%\userdata\etc"
  move "%CurrentDir%\runtime"                                "%TempDir%\"
  move "%CurrentDir%\userdata\etc\all.policy"                "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\branding.properties"       "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\branding-ssh.properties"   "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\config.properties"         "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\custom.properties"         "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\version.properties"        "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\distribution.info"         "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\jre.properties"            "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\profile.cfg"               "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\startup.properties"        "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\org.apache.karaf"*         "%TempDir%\userdata\etc\"
  move "%CurrentDir%\userdata\etc\org.ops4j.pax.url.mvn.cfg" "%TempDir%\userdata\etc\"

echo.Clearing the cache and tmp folders is necessary for upgrade.

echo.============================================================
  echo.Clearing cache...
  echo %time% - Clearing cache... >> .\tmp\LOG-%date%.txt 
  rmdir /s /q "%CurrentDir%\userdata\cache"

  rmdir /s /q "%CurrentDir%\userdata\tmp"


echo.Copy the downloaded folder into openHAB's directory WITHOUT replacing any existing files.

echo.=========================================================================================
  echo.Updating openHAB...
  echo %time% - Updating openHAB... >> .\tmp\LOG-%date%.txt 
  @timeout 5
  robocopy %NewDir%\%newVersion% %CurrentDir% /e /xc /xn /xo

  goto exit

:error
  echo.
  echo ERROR: %ErrorMsg%
  pause
  quit

:exit
  echo SUCCESS: openHAB updated to %newVersion%
  echo %time% - USUCCESS: openHAB updated to %newVersion% >> .\tmp\LOG-%date%.txt 
  pause

What’s wrong with the official Windows update script? Not sure it is helpful, if different scripts are maintained at different places, so better help improving the existing one to make it work for as many people as possible.

2 Likes

Hello Kai,

i will try the official script and help to improve it. It makes no sense to support two. I will test and use the official script.

I think my problem was that in the official documentation http://docs.openhab.org/installation/windows.html#updating-the-openhab-runtime is no hint for a windows script. And In the runtime\bin folder is only a linux script.

I just did not know that one exists. I was looking for it.

Jörg

Yes, you are indeed right - apart from the 2.1 release notes, it is nowhere mentioned in the docs yet.
I have just commented on the according issue, so hopefully this willl change soon.

My experience with the official update script is the following:

  1. Local backup is working
  2. download of any Version I try to install is NOT working and there is no error handling inside the script that reflect the missing download
  3. Maybe because of the experience above, the update script is not mentioned in the Windows Installation doc, even if the note from Kai is one year old

I would appreciate any improvements in the script that make the whole process more robust and maybe having it documented will help spread the news that such a script can be leveraged.

@markusth There are two open PRs here and here, which should improve the update script for Windows. Any help on these by Windows users is highly appreciated!