Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use Powershell to backup SQL Database

Posted on 2014-09-03
6
Medium Priority
?
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 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