Solved

How to use Powershell to backup SQL Database

Posted on 2014-09-03
6
384 Views
Last Modified: 2014-09-16
Hi, I have been referred to this article to use Powershell to backup my SQL database -

http://msdn.microsoft.com/en-us/library/ms187510.aspx

Notice the code is as follows
Backup-SqlDatabase -ServerInstance [b]Computer\Instance[/b] -Database [b]MyDB[/b] -BackupAction Database

Open in new window


Well I do not quite understand how to customise it for my server/database, so say my server is 'servername' and my instance is 'SQLEXPRESS' and my DB is 'database'.

I have put in bold what I think I need to change - is this correct?
0
Comment
Question by:fuzzyfreak
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 40301560
Backup-SqlDatabase -ServerInstance servername\SQLEXPRESS  -Database MyDB -BackupAction Database
0
 
LVL 28

Expert Comment

by:becraig
ID: 40309377
Do you have any additional questions on this issue ?
0
 
LVL 4

Author Comment

by:fuzzyfreak
ID: 40317023
Thank you very much, this works.
Yes, I'd like to specify a path for the backup to go to,
I would also like to schedule this in Task Scheduler, how do I put it in a script?

Thanks
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 40318474
Yes, I'd like to specify a path for the backup to go t
o,
(Specify backupfile)
import-module sqlps
Backup-SqlDatabase -ServerInstance servername\SQLEXPRESS  -Database MyDB -BackupFile C:\folder\file.bak -BackupAction Database 

Open in new window



I would also like to schedule this in Task Scheduler, how do I put it in a script?
Here is a nice walkthrough on setting up a scheduled task.

http://clickhome.freshdesk.com/support/articles/107608-how-to-setup-windows-auto-scheduler-task-to-restart-clickhome-windows

Simply save your script and ensure you load the required module as a part of your script.
your script from the scheduled task should be called this way:
powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File <script_name.ps1>
0
 
LVL 4

Author Comment

by:fuzzyfreak
ID: 40325605
Thanks, this is all up and running now.
0
 
LVL 4

Author Closing Comment

by:fuzzyfreak
ID: 40325606
Excellent and comprehensive reply.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now