SQL Databases
PowerShell script to create backups of all SQL databases on a device.
Overview
This script uses the SqlServer module to automate the creation of SQL database backups.
Prerequisites:
For this script to run successfully, the user running the script will need to have permission to perform SQL database backups on all databases, including system databases. It is recommended to run the script as SYSTEM and set permissions on each database using the process below.
Granting backup database permissions to SYSTEM:
Install & login to Microsoft SQL Server Management Studio (SSMS) on the SQL Server
Navigate to Server\Instance > Security > Logins
Right-click
NT AUTHORITY\SYSTEMand choose PropertiesUnder Server Roles, make sure
publicis checkedUnder User Mapping, check each database and select the following database role memberships for each:
publicdb_backupoperator



After database permissions are set, you can deploy the script via your RMM.
Notes:
Unless changed manually, the default SQL backup location is
C:\Program Files\Microsoft SQL Server\MSSQL.[INSTANCENAME]\MSSQL\Backup
Script
Backups are not performed on databases named tempdb.
Examples
Example 1
This example creates a backup of all SQL databases on every detected instance, excluding temp databases.
Example 2
This example displays database backups performed in the last month. It does not perform new backups.
Parameters
-AuditOnly
Optional switch parameter that outputs a history of all database backups for the last month. No additional backups are performed when using this parameter.
Last updated
Was this helpful?