--run-script-before windows batch file

Greetings,

I have two questions related to this topic:

  1. Why does the batchfile get executed before I try to see my restore points? Do I have to set something in the advanced settings so it only starts when I start a backup?

  2. This is my current script but it won’t work right because it seems like it gets included in duplicati and not executed as standalone script:

@echo off
setlocal enabledelayedexpansion

REM Set your SQL Server instance name
set server=localhost
set sqlCmdOptions=-S %server% -E -h-1 -W -s "," -w 700

REM Set the path where you want to store the backups and zip files
set "backup_dir=D:\MSSQL_Backup_Path"
set "7z_dir=!backup_dir!\7z"

cd /d "%backup_dir%"

REM Get current date
for /f "tokens=1-3 delims=." %%a in ('echo %date%') do (
    set "day=%%a"
    set "month=%%b"
    set "year=%%c"
)

REM Loop through each database and perform backup
for /f "tokens=*" %%a in ('sqlcmd %sqlCmdOptions% -Q "SET NOCOUNT ON; SELECT [name] FROM sys.databases WHERE [state_desc] = 'ONLINE' AND [name] NOT IN ('master', 'tempdb', 'model', 'msdb')" ') do (
   
	set "dbName=%%a"
	set "mydate=!day!_!month!_!year!"
	
	
    echo !mydate!	!dbName!	Backing up database: !dbName!
	
    set "backup_file=%backup_dir%\!dbName!_backup.bak"
    set "zip_file=%backup_dir%\!dbName!_backup.zip"
    
	if exist "!backup_file!" (
		del "!backup_file!"
	)
	
	if exist "!zip_file!" (
		del "!zip_file!"
	)
	
    REM Perform backup
    sqlcmd %sqlCmdOptions% -Q "BACKUP DATABASE [!dbName!] TO DISK='!backup_file!'" >nul 2>nul
    REM Zip the backup file
	echo !mydate!	!dbName!	ZIP wird erstellt
	
    "7z/7z.exe" a "!zip_file!" "!backup_file!" -aoa >nul

    REM Delete the .bak file after zipping
    echo !mydate!	!dbName!	.BAK wird gelöscht
	del "!backup_file!"
)

I get some errors when it gets executed:

2024-03-16 01:17:55 +01 - [Warning-Duplicati.Library.Modules.Builtin.RunScript-StdErrorNotEmpty]: Das Skript “D:\MSSQL_Backup_Path\datensic_execute.bat” berichtete Fehlermeldungen: Der Befehl “@echo” ist entweder falsch geschrieben oder konnte nicht gefunden werden.

But if I dont write @echo OFF at the beginning I get these errors:

2024-03-15 12:53:35 +01 - [Warning-Duplicati.Library.Modules.Builtin.RunScript-StdErrorNotEmpty]: Das Skript “D:\MSSQL_Backup_Path\datensic_execute.bat” berichtete Fehlermeldungen: Der Befehl “setlocal” ist entweder falsch geschrieben oder konnte nicht gefunden werden. Der Befehl “”!7z_dir!7z.exe"" ist entweder falsch geschrieben oder konnte nicht gefunden werden.

Thanks for your help!

  1. For maximum flexibility, the script is ran every time Duplicati does anything for that backup, including restore or listing available versions. To only run for backups, you need to add a check for DUPLICATI__OPERATIONNAME. There is more info in these example scripts:
  1. Looks like a file encoding issue to me,  is a utf8 BOM that the editor inserted at the beginning. Do you get the same error when running it directly? Try to save it in ANSI encoding or utf8 without BOM.

What does “included” mean? Duplicati does read stdout and stderr script makes.

Turning off echo probably reduces script noise into stdout, so reduces risks from:

REM All Duplicati options can be changed by the script by writing options to
REM stdout (with echo or similar). Anything not starting with a double dash (–)
REM will be ignored

This bites some scripts. Yours might be fine, but this is one way it’s not standalone.

Another way is the script writing errors to stderr make helpful (?) warnings such as:

I’m not sure why script is complaining, but maybe limiting it to backup will quiet that.

Thank you @Jojo-1000 ! The documentation helped a lot getting to understand the backup-flow - In 30 minutes I’ll see if it worked fine without errors, at least the " DUPLICATI__OPERATIONNAME" fixed my restore problem and the ANSI fixed the @echo issue, sometimes it can be so easy - I’ll keep you updated if it worked fine tonight! :slight_smile: