Greetings,
I have two questions related to this topic:
-
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?
-
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!