Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to backup SQL Server database using Powershell

I am trying to backup a SQL Server 2008 R2 database, name of the database is test.

I am using the following PowerShell script but I get an error message that database does not exist, but I can see the test database and tables and I can query it.

Backup-SqlDatabase -ServerInstance testpc-001\SQLEXPRESS -Database test -BackupAction   
Database

and if I try an edit the above script using

$date = Get-Date -Format yyyyMMddHHmmss
$dbname = 'test'
Backup-SqlDatabase -ServerInstance SQLEXPRESS -Database $dbname -BackupFile   
"C:\DBbackups\$($dbname)_db_$($dt).bak"

I am getting an error failed to connect to SQLEXPRESS

like image 545
user3770612 Avatar asked Sep 22 '26 10:09

user3770612


1 Answers

$date = Get-Date -Format yyyyMMddHHmmss
$dbname = 'test'
Backup-SqlDatabase -ServerInstance testpc-001\SQLEXPRESS  -Database $dbname -BackupFile   
"C:\DBbackups\$($dbname)_db_$($date).bak"

there was a typo in the -ServerInstance parameter

like image 108
Jason Horner Avatar answered Sep 24 '26 06:09

Jason Horner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!