Backup Mssql Database
Zuletzt geändert: 14.03.2026 05:56

Backup-MssqlDatabase #

ÜBERSICHT #

Erstellt ein Backup einer SQL Server-Datenbank und überträgt es optional per SFTP und/oder in ein lokales Archiv.

SYNTAX #

Backup-MssqlDatabase [[-RemoteServer] <string>] [[-Port] <int>] [[-Certificate] <string>] [[-User]
    <string>] [[-PasswordPath] <string>] [[-Password] <securestring>] [[-PlainPassword] <string>]
    [[-RemoteFolder] <string>] [[-StorageFolder] <string>] [-RemoveBak] [[-History] <int>] [[-Conn]
    <__ComObject>] [[-Udl] <string>] [[-ConnStr] <string>] [<CommonParameters>]

BESCHREIBUNG #

Sichert eine Datenbank via T-SQL BACKUP DATABASE in das Standard-Backupverzeichnis der SQL-Instanz. Das Backup-Verzeichnis wird automatisch per SQL-Query ermittelt – eine lokale SQL-Server-Installation ist nicht erforderlich. Die Ermittlung erfolgt bevorzugt über SERVERPROPERTY('InstanceDefaultBackupPath') (ab SQL Server 2019, keine besonderen Rechte), mit Fallback auf xp_instance_regread (alle Versionen, erfordert sysadmin).

Bei Remote-SQL-Servern, deren Backup-Verzeichnis vom Client nicht erreichbar ist, wird das SQL-Backup trotzdem erstellt. ZIP-Komprimierung, StorageFolder, SFTP-Upload und RemoveBak werden in diesem Fall übersprungen (mit Warning-Meldung).

Bei lokalem Zugriff auf das Backup-Verzeichnis stehen alle Optionen zur Verfügung:

  • .zip-Komprimierung,
  • Kopie ins lokale Storage mit History-Cleanup,
  • SFTP-Upload mit Ordneranlage und History-Cleanup,
  • Löschung der .bak nach Verarbeitung. DB-Verbindung über genau einen der Parameter -Conn, -Udl oder -ConnStr.

PARAMETER #

-Conn #

Type: __ComObject

Offene ADODB.Connection. Alternative zu -Udl oder -ConnStr.

-Udl #

Type: string

Pfad zu einer UDL-Datei mit Verbindungsdaten.

-ConnStr #

Type: string

Verbindungszeichenfolge als Text.

-StorageFolder #

Type: string

Lokales Ziel (z. B. NAS). Dorthin wird die ZIP mit Zeitstempel kopiert. History wird dort angewendet.

-RemoveBak #

Type: switch

Löscht die erzeugte .bak nach erfolgreicher Verarbeitung.

-History #

Type: int
Default: 3

Anzahl der aufzubewahrenden ZIPs je Ziel (lokal & remote). 0 ⇒ alles ist „veraltet“.

-RemoteServer #

Type: string

SFTP-Server (Name oder IP).

-Port #

Type: int
Default: 22

SFTP-Port (Standard: 22).

-User #

Type: string

SFTP-Benutzername.

-PasswordPath #

Type: string

Pfad zu Passwortdatei (CLIXML mit SecureString oder Klartext). Höchste Priorität.

-Password #

Type: securestring

SFTP-Passwort als SecureString.

-PlainPassword #

Type: string

SFTP-Passwort im Klartext.

-Certificate #

Type: string

Optionaler Pfad zu privatem Schlüssel/Zertifikat für Key-Auth (falls deine SFTP-Wrapper das unterstützen).

-RemoteFolder #

Type: string

Zielordner auf dem SFTP-Server. Die Funktion hängt zusätzlich den SQL-Instanznamen an.

AUSGABEN #

string

Vollständiger Pfad der finalen ZIP- oder BAK-Datei (auf dem SQL-Server).

BEISPIELE #

# Backup als ZIP, zusätzlich ins NAS, History 5
Backup-MssqlDatabase -Udl 'C:\Eulanda\EULANDA_1.udl' -StorageFolder '\\nas\sqlbackup' -History 5
# Backup als ZIP per SFTP (Passwort aus Datei), danach .bak löschen
Backup-MssqlDatabase -Udl 'C:\Daten\prod.udl' -RemoteServer 'sftp.example.com' -User 'backup' -PasswordPath "$HOME\.secrets\sftp.xml" -RemoteFolder '/backup/sql' -RemoveBak
# Reines SQL-Backup auf einem Remote-Server (ohne ZIP/Upload)
Backup-MssqlDatabase -Udl 'C:\Daten\remote-server.udl'