This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am going share scripts to take all database and log backups using SQL Server PowerShell.
Using T-SQL, we can also take all databases backup using one single script, but some of the Database Administrators like to perform this kind of task using PowerShell because sometimes it is much faster and it is executing from outside of the SQL Server.
SQL Server 2012 cmdlet introduced a new command to take database backups and the command name is “Backup-SqlDatabase”.
Execute below command in PowerShell to get the help of “Backup-SqlDatabase”:
1 |
help Backup-SqlDatabase -full |
PowerShell script to take all database backups:
Generating file name with the date.
1 2 3 4 5 |
$BackupDate = Get-Date -Format MMddyy foreach($database in (Get-ChildItem)) { $dbName = $database.Name $bakFilePath = "C:\temp\" + $dbName + "_" + $BackupDate + ".bak" Backup-SqlDatabase -Database $dbName -BackupFile $bakFilePath -Initialize } |
PowerShell script to take all database log backups:
Generating file name with the date.
1 2 3 4 5 |
$BackupDate = Get-Date -Format MMddyy_hhmm foreach($database in (Get-ChildItem)) { $dbName = $database.Name $trnFilePath = "C:\temp\" + $dbName + "_" + $BackupDate + ".trn" Backup-SqlDatabase -Database $dbName -BackupAction Log -BackupFile $trnFilePath -Initialize} |