This .NET console application scans your running Docker containers, detects common database images, and creates compressed backups into a local backups folder.
Supported database types:
- PostgreSQL: images/names containing
postgres - MySQL / MariaDB: images/names containing
mysqlormariadb - SQL Server: images/names containing
mssqlorsqlserver
- Windows 11
- Docker Desktop (or other Docker installation) with the
dockerCLI available on yourPATH - .NET SDK (the project targets
net10.0using your installed SDK)
Your database containers should expose standard environment variables so the tool can connect:
- PostgreSQL
POSTGRES_USER(orPOSTGRES_USERNAME)POSTGRES_PASSWORDPOSTGRES_DB(falls back toPOSTGRES_USERif not set)
- MySQL / MariaDB
MYSQL_USER/MYSQL_USERNAME/MYSQL_ROOT_USER(defaults torootif none set)MYSQL_PASSWORDorMYSQL_ROOT_PASSWORDMYSQL_DATABASE
- SQL Server
SA_USERorMSSQL_USER(defaults tosaif not set)MSSQL_SA_PASSWORD(preferred) orSA_PASSWORDorMSSQL_PASSWORD– the password for the main SQL Server admin account (sa)- (Optional)
MSSQL_DATABASE/MSSQL_DB/MSSQL_DB_NAME– not required by this tool, since it backs up all non-system databases in the SQL Server instance
MSSQL_SA_PASSWORDis the password for the main SQL Server administrator account, calledsa.- This app uses that password only to connect and read your database so it can create a backup.
- You should choose a password that is easy for you to remember but hard for others to guess.
- Do not share this password with other people and do not write it in public places.
If MSSQL_SA_PASSWORD is missing or wrong, the app cannot connect to SQL Server and the backup for that container will fail.
For each running container:
- Lists containers via
docker ps. - Detects whether the container looks like a Postgres or MySQL/MariaDB database by image/name.
- Inspects env vars via
docker inspectto get credentials and database name. - Executes a backup inside the container:
- Postgres: uses
pg_dump -F c -Z 9(custom format, with compression). - MySQL/MariaDB: uses
mysqldump | gzipto produce a compressed.sql.gz. - SQL Server: uses
sqlcmdto back up all non-system databases with compression, writing one.bakfile per database.
- Postgres: uses
- Copies the backup file to the host using
docker cpinto a timestamped subfolder ofbackups. - Cleans up the temporary file inside the container.
Each run creates a folder like:
backups\20260225_153012\
With files named similar to:
my-postgres-postgres_postgres_20260225_153012.dumpmy-mysql-mariadb_mysql_appdb_20260225_153012.sql.gzmy-mssql_sqlserver_appdb_20260225_153012.bak
From the project root (DockerBackupAndRestore):
dotnet runThe app will:
- Print the backup destination folder.
- List any detected database containers.
- Attempt backups for each supported container and print a success/failure summary.
You can run it whenever you want to take a snapshot; each run creates a new timestamped subfolder under backups.
If you use Docker Desktop and are not familiar with command-line tools, you can follow these steps:
- Open Docker Desktop on your computer.
- Go to the Containers (or Containers / Apps) tab.
- Find your SQL Server container in the list (its image name usually includes
mssqlorsqlserver). - If the container is running, click Stop to stop it.
- Click on the container and look for an option like Duplicate / Edit or Edit (this may vary slightly by Docker Desktop version).
- In the settings screen, find the Environment variables section.
- Add a new variable:
- Name:
MSSQL_SA_PASSWORD - Value: a strong password that you will remember (for example, a sentence with numbers and symbols).
- Name:
- Make sure the other required SQL Server variables (like the database name) are also set if needed.
- Save the changes and start the container again (Docker Desktop may create a new container with the updated settings).
- After the container is running with
MSSQL_SA_PASSWORDset, rundotnet runagain from this app to create a backup.
Keep your MSSQL_SA_PASSWORD somewhere safe (for example, in a password manager). You may also use it later to connect from tools like SQL Server Management Studio.
If you (or someone helping you) uses a docker-compose.yml file, you can set MSSQL_SA_PASSWORD in the environment section. Here is a simple example:
services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2022-latest
container_name: my-sqlserver
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=YourStrongPassword123!
- MSSQL_DATABASE=MyAppDatabase
ports:
- "1433:1433"After changing docker-compose.yml, run:
docker compose up -dMore advanced users can also set the variable when starting a container directly with docker run, for example:
docker run -e MSSQL_SA_PASSWORD=YourStrongPassword123! -e ACCEPT_EULA=Y ...If you are not comfortable with these commands, it is usually easier to use Docker Desktop and follow the steps in the previous section.
Sometimes you might want to set the password only for one terminal window and reuse it in your Docker commands.
-
In PowerShell (recommended on Windows 11):
-
Open Windows Terminal or PowerShell.
-
Type this command (change the password to your own strong password) and press Enter:
$env:MSSQL_SA_PASSWORD = "YourStrongPassword123!"
-
Start your SQL Server container using that variable:
docker run -e MSSQL_SA_PASSWORD=$env:MSSQL_SA_PASSWORD -e ACCEPT_EULA=Y ...
-
The password stored in
$env:MSSQL_SA_PASSWORDexists only in this PowerShell window. If you close the window, you will need to set it again next time.
-
-
In Command Prompt (cmd.exe):
-
Open Command Prompt.
-
Type this command and press Enter:
set MSSQL_SA_PASSWORD=YourStrongPassword123!
-
Start your SQL Server container using that variable:
docker run -e MSSQL_SA_PASSWORD=%MSSQL_SA_PASSWORD% -e ACCEPT_EULA=Y ... -
The password stored in
MSSQL_SA_PASSWORDexists only in this Command Prompt window. If you open a new window, you will need to set it again.
-
If you are not comfortable with terminal commands, you can ignore this section and instead use Docker Desktop as described earlier.
The SQL Server backup feature in this app uses a tool called sqlcmd inside your SQL Server container. Many official SQL Server on Linux images already include it.
If sqlcmd is not available inside the container, this app will try to:
- Detect the Linux package manager in the container (for example,
apt-get,yum,dnf, orzypper). - For apt-based images (such as Ubuntu/Debian), automatically install the Microsoft SQL Server command-line tools (
mssql-tools18) and makesqlcmdavailable. - For other images or if the automatic installation fails, show a clear error message and ask you to install
sqlcmd(ormssql-tools) inside the container manually.
Automatic installation inside the container requires:
- Internet access from the container to reach Microsoft’s package servers.
- Permission to install packages (typical for official SQL Server images, which usually run as root).
You can also install sqlcmd on your own machine if you want to use it directly from Windows, macOS, or Linux:
-
Windows 11 (recommended: Go-based sqlcmd)
-
Open Windows Terminal or PowerShell.
-
Run:
winget install sqlcmd
-
This installs the modern cross-platform
sqlcmdtool. You can also follow the official Microsoft instructions at:https://learn.microsoft.com/sql/tools/sqlcmd/sqlcmd-download-install.
-
-
macOS
-
Install using Homebrew (you or someone helping you should have Homebrew installed):
brew install sqlcmd
-
For detailed steps, see the Microsoft docs:
https://learn.microsoft.com/sql/tools/sqlcmd/sqlcmd-download-install.
-
-
Linux (Ubuntu/Debian/Red Hat and others)
- For many Linux distributions, Microsoft provides packages for the SQL Server command-line tools (
sqlcmdandbcp). - The exact commands depend on your Linux version, so the easiest way is to follow the official guide:
https://learn.microsoft.com/sql/linux/sql-server-linux-setup-tools
- For many Linux distributions, Microsoft provides packages for the SQL Server command-line tools (
In most cases, you do not need to install sqlcmd on your Windows host for this backup tool to work, because the backup runs inside the SQL Server container. You only need to make sure your SQL Server image has sqlcmd available, or choose an official image that already includes it.
If you use Docker Desktop (with WSL2) on Windows 11, Docker stores its Linux data in a large VHDX file (a virtual disk file). Over time, that file can grow quite large. This app can optionally try to shrink that file using Windows' Optimize-VHD command after backups finish.
- You are running Windows 11.
- The Hyper-V PowerShell module is installed and the
Optimize-VHDcmdlet is available. - You have permissions to run PowerShell commands (administrator privileges may be required).
- The VHDX file is not in use when optimization runs.
For Docker Desktop / WSL2, that usually means:
- Quit Docker Desktop.
- Make sure WSL2 is stopped (for example, close any WSL terminals and run
wsl --shutdownin PowerShell or Command Prompt).
If the VHDX is still in use, Optimize-VHD will fail and the app will print an error, but the database backups will still be kept.
VHDX optimization is controlled via environment variables read by the app:
DB_BACKUP_ENABLE_VHD_OPTIMIZATION- Set to
true,yes, or1to enable optimization. - Leave unset or set to
false/0to disable it (default).
- Set to
DB_BACKUP_VHDX_PATH- Full path to the VHDX file you want to optimize.
- For Docker Desktop with WSL2, a common location is something like:
C:\Users\<YourUser>\AppData\Local\Docker\wsl\data\ext4.vhdx
Example (PowerShell, for Docker Desktop VHDX):
$env:DB_BACKUP_ENABLE_VHD_OPTIMIZATION = "true"
$env:DB_BACKUP_VHDX_PATH = "C:\Users\YourUser\AppData\Local\Docker\wsl\data\ext4.vhdx"
dotnet runWhen optimization is enabled and correctly configured:
- The app runs all database container backups as usual.
- After printing the backup summary, it runs a PowerShell command similar to:
Optimize-VHD -Path 'C:\path\to\disk.vhdx' -Mode Full
- If optimization succeeds, you will see a message saying VHDX optimization completed successfully.
- If it fails (for example, because the VHDX is in use), you will see the error details, but your backups are not deleted.
- The tool relies on standard environment variables commonly used by official images. If your container uses different variable names or custom users/databases, you may need to adjust the code accordingly.
- Password values must not contain single quotes for the current shell command construction.
- For SQL Server, the
sqlcmdtool must be available in the container (this is true for typical official SQL Server on Linux images, or after installing sqlcmd as described above). - If
MSSQL_SA_PASSWORD(or the other SQL Server password variables) is not set or is incorrect, the app will not be able to back up SQL Server and will show an error for that container. Once you setMSSQL_SA_PASSWORDand restart the container, runningdotnet runagain will include your SQL Server database in the backups.