I am making exe using Bootstrapper WIX installer for my application. I am trying to get backup of the current SQL Server 2014 and restore to SQL Server 2022 after installation. Here is how my chain element looks like in the Bundle.wxs file.
<Chain>
<ExePackage SourceFile="..prerequisitesPakLogServerBackupDatabase2014.bat" DisplayName="Backing up 2014 Database.." Compressed ="yes" Vital ="no" Permanent ="yes" />
<ExePackage SourceFile="..prerequisitesPakLogServerRemoveDatabase2014.bat" DisplayName="Uninstalling 2014 database.." Compressed ="yes" Vital ="no" Permanent ="yes" />
<ExePackage SourceFile="..prerequisitesPakLogServerRemoveFiles.bat" DisplayName="Removing Existing Database Keys.." Compressed ="yes" Vital ="no" Permanent ="yes" />
<PackageGroupRef Id="Sql2022Express"/>
<ExePackage SourceFile="..prerequisitesPakLogServerRestoreDatabase2014.bat" DisplayName="Restoring 2014 Database records.." Compressed ="yes" Vital ="no" Permanent ="yes" />
</Chain>
The issue I am getting is with the Backup2014Database.bat file, when I run this individually it successfully takes the backup without any error. But while running the exe file generated using the WIX Installer I get this error.
Applying execute package: BackupDatabase2014.bat, action: Install, path: C:ProgramDataPackage Cache812A85A2036657975578443EC126F48F631CEC9EBackupDatabase2014.bat, arguments: '"C:ProgramDataPackage Cache812A85A2036657975578443EC126F48F631CEC9EBackupDatabase2014.bat"'
Error 0x80070001: Process returned error: 0x1
Error 0x80070001: Failed to execute EXE package.
Error 0x80070001: Failed to configure per-machine EXE package.
Here is the BackupDatabase2014.bat file
@echo off
SETLOCAL EnableExtensions
rem Check if the script is running with administrative privileges
net session >nul 2>&1
if %errorLevel% neq 0 (
echo This script requires administrative privileges. Please run it as an administrator.
exit /b 1
)
echo Checking for SQL Server instance XYZInstance...
reg query "HKLMSOFTWAREMicrosoftMicrosoft SQL ServerInstance NamesSQL" /v XYZINSTANCE | findstr /i "MSSQL12.XYZINSTANCE" > nul 2>&1
if %errorlevel% neq 0 (
echo XYZInstance instance not found in the registry. Exiting...
exit /b 1
)
echo Setting backup folder path...
set "BackupFolderPath=C:Database2014Backup"
echo Checking if the backup folder exists...
if not exist "%BackupFolderPath%" (
echo Creating backup folder...
mkdir "%BackupFolderPath%"
if %errorlevel% neq 0 (
echo Failed to create the backup folder. Exiting...
exit /b 1
)
)
echo Granting permissions...
icacls "%BackupFolderPath%" /grant "NT AUTHORITYSYSTEM":(OI)(CI)F
if %errorlevel% neq 0 (
echo Failed to grant permissions to the backup folder. Exiting...
exit /b 1
)
echo Setting variables for backup...
set "ServerName=%COMPUTERNAME%XYZInstance"
set "DatabaseName=XYZInstance"
set "BackupFileName=%DatabaseName%_Backup_%date:~4,2%-%date:~7,2%-%date:~10,4%.bak"
set "BackupFilePath=%BackupFolderPath%%BackupFileName%"
echo Checking if the database exists...
sqlcmd -S %ServerName% -Q "IF EXISTS(SELECT 1 FROM sys.databases WHERE name = '%DatabaseName%') SELECT 1 ELSE SELECT 0;" -h-1 -s" " -W > nul
if %ERRORLEVEL% EQU 0 (
echo Performing database backup...
sqlcmd -S %ServerName% -Q "BACKUP DATABASE [%DatabaseName%] TO DISK='%BackupFilePath%'"
if %errorlevel% neq 0 (
echo Database backup failed. Exiting...
exit /b 1
)
echo Checking if the backup was successful...
if exist "%BackupFilePath%" (
echo Database backup completed successfully.
) else (
echo Database backup failed. Backup file does not exist.
exit /b 1
)
) else (
echo Database %DatabaseName% does not exist. Skipping backup.
)
exit /b 0
The Sequence I am trying to achieve here is :
- Take Backup
- Remove the current instance of SQL Server 2014
- Remove the related registry keys
- Install the SQL Server 2022 instance.
- Restore the 2014 backup to the 2022 instance
I have tried changing the BackupDatabase2014.bat file code to make sure there is no permission issue, also the exe is executed with the administrator privileges. There are more bat files in the code which runs without any issue. I looked into multiple answers on stack overflow and other internet platforms none of them matches the case here.
I am stuck here please help, also I know using the bat file is not the best practice here, but it has been mandatory because of how the previous code of the installation was written.