0

I am trying to take SQL DB backup from PowerShell using backup-sqldatabase module using the below script. The module works fine with taking DB backup however I am unable to figure out the backup file name for further use.

I have executed the below script but it is not taking $filename given by me also $db is null once the backup is completed.

I need to get the backup file name once the backup is completed.


$FileNAme="$backupUrlContainer"+"automationPOC.bak"

$db=Backup-SqlDatabase -BackupContainer $backupUrlContainer -SqlCredential $credentialName -Compression On -ServerInstance 'bp1amsdb419' -Database "Automation" -BackupFile $FileNAme ```
SQLDoctor
  • 343
  • 7
  • 16
  • I don't know if it's just a typo in your example but the first line is commented out (starts with #). – notjustme Apr 01 '19 at 15:28
  • Yes I have commented it. Even though I uncomment it. Powershell would create a backup file in BLOB with a random name – SQLDoctor Apr 01 '19 at 15:30
  • From the backup-sqldatabase help; `The BackupContainer parameter cannot be used with the BackupFile parameter` – notjustme Apr 01 '19 at 15:37
  • Ok thanks for that note. How can I give a custom file name for the DB backup. If that’s not possible how can I retrieve the backup file name that powershell has used to take the backup – SQLDoctor Apr 01 '19 at 15:49
  • Thanks for the help I have figured out the required command – SQLDoctor Apr 02 '19 at 12:26

1 Answers1

0

Below is the script in case we want to give a custom file name

$FileNAme="$backupUrlContainer"+"/"+"DBName.bak"
$db=Backup-SqlDatabase  -CompressionOption On -ServerInstance $serverName -Database $DBName -BackupFile $FileNAme -SQLCredential $credentialName
SQLDoctor
  • 343
  • 7
  • 16