---
title: "Back up the Firebird database automatically (Windows batch script)"
description: "A batch script backs up your Firebird database every night in the background, with a log, a cleanup routine and a check that proves the backup can actually be restored."
url: https://help.portiervision.com/en/articles/114-back-up-the-firebird-database-automatically-windows-batch-script
collection: "Integrations & IT Setup"
updated: 2026-08-28
language: en
alternates: [de, fr]
---

# Back up the Firebird database automatically (Windows batch script)

A batch script backs up your Firebird database every night in the background, with a log, a cleanup routine and a check that proves the backup can actually be restored.

## What this script is for

portier Vision has no backup function you can start from inside the program. If your database runs on Firebird, the batch script below backs it up every night in the background.

If your database runs on Microsoft SQL Server, this script does not help. `gbak.exe` backs up Firebird only. Your SQL Server is backed up with its own tools, normally a maintenance plan or a SQL Server Agent job. portier ships nothing for that, it belongs to whoever looks after your database server.

## Requirements

-   Firebird is installed and `gbak.exe` sits in the Firebird folder.
    
-   You have write permissions on the backup folder.
    
-   You know the SYSDBA password of your Firebird installation.
    
-   Windows Task Scheduler is available.
    

If the database sits on a server, set up the script and the task on that server, not on a workstation.

## Step 1: Read the database path

Do not guess the path. It is in portier Vision Companion.

1.  Open the Companion. On the sign-in screen, **Open database configurator** takes you straight there. After signing in, use the **Database Configuration** tab.
    
2.  **Database Type** reads **Firebird**.
    
3.  Note the value in **Database Path**. It goes into the `DBPATH` line.
    
4.  If **Host** is not `localhost`, the database sits on another machine. Set the backup up there.
    

![The value in the Database Path field goes into the DBPATH line of the script.](https://staticfiles.gleap.io/p69f19d405a24ffe33e3bc1d5/HlpkzMsH1ME9rE79bIR00NewDIHVHfP3mZSqP3cJECp926FnqfmNvBXfJJfUXDvEa8aRJJWyiqx.png)

_The value in the Database Path field goes into the DBPATH line of the script._

## Step 2: Save the script

Save the text below as `C:\portier\backup\portier_backup.bat`. Remember this path exactly. The scheduled task has to point at the same file.

```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=YOUR_SYSDBA_PASSWORD"
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%
```

## Step 3: Adjust the settings

Only the block at the top changes.

-   `FBPATH`: path to `gbak.exe`. Check it in Explorer, the Firebird folder is named differently per version.
    
-   `DBPATH`: the value from **Database Path**, step 1.
    
-   `BKPATH`: folder for backups and logs.
    
-   `PASS`: your SYSDBA password.
    
-   `RETENTION_DAYS_BACKUPS` and `RETENTION_DAYS_LOGS`: retention in days.
    

**About the password.** `masterkey` is the default password of every fresh Firebird installation. If it is still in place on your system, change it before you go any further. The password sits in this file in clear text, so restrict permissions on `C:\portier\backup` to the account the task runs under.

## Step 4: Run it once by hand

Start the file with a double click. No window opens. Then look in `C:\portier\backup`. Two new files must be there, a `.fbk` and a log. If they are missing, check the return codes below.

## Step 5: Create the scheduled task

1.  Open **Task Scheduler** (`taskschd.msc`).
    
2.  Choose **Create Task**.
    
3.  Under **General**: name it `portier Firebird Backup`, tick **Run with highest privileges** and **Run whether user is logged on or not**.
    
4.  Under **Triggers**: new, daily, 02:00.
    
5.  Under **Actions**: start a program, `C:\portier\backup\portier_backup.bat`. Character for character the same path as in step 2.
    
6.  Save, then right-click the task and choose **Run**.
    

**A typo in step 5 goes unnoticed.** If the task points at a file that is not there, it starts at the scheduled time, does nothing and reports nothing on screen. No backup is written and no log either, because the script never runs. The only hint sits in the **Last Run Result** column. So on the morning after the first scheduled run, check whether the backup folder holds a `.fbk` dated today.

## Step 6: Prove the backup restores

A backup that has never been restored is a guess. Test once after setup, then once or twice a year.

```bat
"C:\Program Files (x86)\Firebird\Firebird_4_0\gbak.exe" -c -user SYSDBA -pas YOUR_SYSDBA_PASSWORD "C:\portier\backup\portier_2026-08-28_02-00-01.fbk" "C:\portier\backup\verify.fdb"
```

-   Take the newest `.fbk` from the backup folder.
    
-   `-c` creates a **new** database. The target is a test file. Never point it at `PORTIERVISION.GDB`.
    
-   The command has to finish without an error, and `verify.fdb` has to be roughly the size of your live database.
    
-   Delete `verify.fdb` afterwards.
    

If the command fails, your backup is not usable. Sort that out before you need it.

## Return codes

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Value</p></th><th colspan="1" rowspan="1"><p>Meaning</p></th></tr><tr><td colspan="1" rowspan="1"><p>0</p></td><td colspan="1" rowspan="1"><p>Backup successful</p></td></tr><tr><td colspan="1" rowspan="1"><p>2</p></td><td colspan="1" rowspan="1"><p>Backup folder could not be created, check BKPATH</p></td></tr><tr><td colspan="1" rowspan="1"><p>3</p></td><td colspan="1" rowspan="1"><p>Database not found, check DBPATH</p></td></tr><tr><td colspan="1" rowspan="1"><p>4</p></td><td colspan="1" rowspan="1"><p>gbak.exe not found, check FBPATH</p></td></tr><tr><td colspan="1" rowspan="1"><p>any other</p></td><td colspan="1" rowspan="1"><p>Return code from gbak.exe, details are in the log</p></td></tr></tbody></table>

Every run writes its own timestamped log. A good run looks like this:

```
==================================================
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"
==================================================
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
```

## What the script does not do

-   It gets nothing off the machine. A backup on the same disk as the database is gone with the disk. Copy the `.fbk` to a second medium as well.
    
-   It does not back up Microsoft SQL Server.
    
-   It does not answer who notices tomorrow morning that the backup did not happen.
    

One last point. Never copy `PORTIERVISION.GDB` with Explorer while the database is running, the copy is unusable. `gbak` is allowed to back up during operation, Vision can stay open.
