Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use Powershell to backup SQL Database

Posted on 2014-09-03
6
Medium Priority
?
431 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 29

Expert Comment

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

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 29

Accepted Solution

by:
becraig earned 2000 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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

782 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