Greetings and Salutations!
I am a new PS scripter and am working on a backup script for some vendor servers I have inherited. The script is run as a scheduled task every night at 01:00.
I am running into an issue backing up the SQL 2012 databases. Below is the SQL backup portion of my script. I get a full backup on the first of the month or whatever day I run it. The problem arises the next night when the script is run, I get an error that the differential cannot run because a current database backup does not exist (error code below script snippet).
Any assistance, guidance, glances in the right direction, would be most thankfully welcome.
Thank you.
- Ken
---- Script Snippet ----
$dom = Get-Date -UFormat %e
$srvname = gc env:computername
$root_dir = \\backup_path\
import-module sqlps -disablenamechecking
Set-Location SQLSERVER:\SQL\$srvname\DEFAULT\Databases
If ($dom -eq " 1") {
foreach ($db in (gci)) {
$dbname = $db.Name
$dt = Get-Date -Format yyyyMMdd
$sqlbufile = $root_dir + "\SQL\"+ $dbname + "\" + $dbname + "_FULL_" + $dt + ".bak"
Backup-SQLdatabase -Database $dbname -BackupAction Database -CompressionOption On -Backupfile $sqlbufile }
}
Else {
foreach ($db in (gci)) {
$dbname = $db.Name
$dt = Get-Date -Format yyyyMMdd
$sqlbufile = $root_dir + "\SQL\"+ $dbname + "\" + $dbname + "_" + $dt + ".bak"
Backup-SQLdatabase -Database $dbname -BackupAction Database -Incremental -CompressionOption On -Backupfile $sqlbufile }
}
-------------------------
---- Error Message ----
Backup-SQLdatabase : System.Data.SqlClient.SqlError: Cannot perform a differential backup for database "main", because a current database backup does not exist.
Perform a full database backup by reissuing BACKUP DATABASE, omitting the WITH DIFFERENTIAL option.
At C:\folder\script-backup.ps1:122 char:3
+ Backup-SQLdatabase -Database $dbname -BackupAction Database -Incremental -Comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Backup-SqlDatabase], SmoException
+ FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.BackupSqlDatabaseCommand
-------------------------