Skip to content
English
  • There are no suggestions because the search field is empty.

๐Ÿ”ง Automatic Backup with Firebird (Windows Batch Script)

This article explains how to create and schedule an automatic backup of a Firebird database using a simple batch script. The script uses the built-in Firebird tool gbak.exe, adds detailed logging, and performs automatic cleanup of old backup files.

๐Ÿ“‹ Requirements

Before you begin, make sure that:

  • Firebird (e.g. version 4.0) is installed on your system,

  • you have write permissions for the backup folder,

  • the file gbak.exe exists (usually in the Firebird installation path),

  • and the Windows Task Scheduler is available.

๐Ÿ’ป Firebird Silent Automatic Backup Script

Filename: portier_backup.bat

@echo off
setlocal EnableExtensions EnableDelayedExpansion

:: ================== Settings ==================
set "FBPATH=C:\Program Files (x86)\Firebird\Firebird_4_0\gbak.exe"
set "DBPATH=C:\portier\vision5\PORTIERVISION.GDB"
set "BKPATH=C:\portier\Backup"
set "USER=SYSDBA"
set "PASS=masterkey"
set "RETENTION_DAYS_BACKUPS=14"
set "RETENTION_DAYS_LOGS=30"
:: ====================================================

:: --- Pre-checks & create folder ---
if not exist "%BKPATH%" mkdir "%BKPATH%" 2>nul

if not exist "%BKPATH%" exit /b 2
if not exist "%DBPATH%" exit /b 3
if not exist "%FBPATH%" exit /b 4

:: --- Timestamp for unique filenames ---
for /f %%i in ('powershell -NoProfile -Command "(Get-Date).ToString(\"yyyy-MM-dd_HH-mm-ss\")"') do set "STAMP=%%i"

set "BKFILE=%BKPATH%\portier_%STAMP%.fbk"
set "LOGFILE=%BKPATH%\backup_log_%STAMP%.txt"

(
  echo ==================================================
  echo Backup started: %DATE% %TIME%
  echo gbak: "%FBPATH%"
  echo DB  : "%DBPATH%"
  echo Target: "%BKFILE%"
  echo ==================================================
)>>"%LOGFILE%"

:: --- Execute backup silently ---
"%FBPATH%" -b -v -user %USER% -pas %PASS% "%DBPATH%" "%BKFILE%" >>"%LOGFILE%" 2>&1
set "RC=%ERRORLEVEL%"

if "%RC%"=="0" (
  echo [%DATE% %TIME%] Backup OK >>"%LOGFILE%"
) else (
  echo [%DATE% %TIME%] ERROR, Code %RC% >>"%LOGFILE%"
)

:: --- Cleanup old backups ---
echo.>>"%LOGFILE%"
echo [%DATE% %TIME%] Cleaning: *.fbk older than %RETENTION_DAYS_BACKUPS% days >>"%LOGFILE%"
forfiles /p "%BKPATH%" /m *.fbk /d -%RETENTION_DAYS_BACKUPS% /c "cmd /c del /q /f @path" 2>>"%LOGFILE%"

:: --- Cleanup old logs ---
echo [%DATE% %TIME%] Cleaning: Logs older than %RETENTION_DAYS_LOGS% days >>"%LOGFILE%"
forfiles /p "%BKPATH%" /m backup_log_*.txt /d -%RETENTION_DAYS_LOGS% /c "cmd /c del /q /f @path" 2>>"%LOGFILE%"

echo ==================================================>>"%LOGFILE%"
echo Backup completed. >>"%LOGFILE%"

endlocal & exit /b %RC%

๐Ÿง  Explanation of Key Sections

Section Description
Settings Defines paths and credentials for Firebird, database, and backup folder, as well as file retention days.
Pre-checks Verifies required folders and files exist; exits with an error code if not.
Timestamp Generates a unique timestamp (yyyy-MM-dd_HH-mm-ss) for each backup and log file.
Silent Backup Execution Runs gbak.exe silently, logging all output instead of displaying it on screen.
Error Handling Writes success or failure (with return code) to the log file.
Cleanup Routine Automatically deletes old backups and log files older than the defined retention period.
Exit Codes Returns an exit code to the system (0 = OK, 3 = database missing, etc.), useful for monitoring tools.

๐Ÿงพ Example Log Output

 

==================================================
Backup started: 13.10.2025 07:00:01
gbak: "C:\Program Files (x86)\Firebird\Firebird_4_0\gbak.exe"
DB  : "C:\portier\vision5\PORTIERVISION.GDB"
Target: "C:\portier\Backup\portier_2025-10-13_07-00-01.fbk"
==================================================
Starting backup...
gbak: creating file C:\portier\Backup\portier_2025-10-13_07-00-01.fbk
gbak: writing data...
gbak: finishing, closing, and going home
[13.10.2025 07:01:12] Backup OK

๐Ÿ•’ How to Run It Automatically (Windows Task Scheduler)

To run the backup automatically (for example, every night at 2:00 AM):

  1. Open Windows Task Scheduler (taskschd.msc)

  2. Click Create Task

  3. Under General:

    • Name: portier Firebird Backup Silent

    • Check Run with highest privileges

    • Check Run whether user is logged on or not

  4. Under Triggers:

    • New โ†’ Daily โ†’ Time: 02:00

  5. Under Actions:

    • Start a program โ†’ Path: C:\portier\backup\portier_backup_silent.bat

  6. Save and test the task with Right-click โ†’ Run.

โœ… The script will now run silently in the background โ€” no console, no confirmation, just logs.

โœ… Conclusion

This silent Firebird backup script provides a simple and reliable way to protect your database automatically โ€” without user interaction.
It logs every operation, removes old files automatically, and can be scheduled to run unattended through the Windows Task Scheduler.