Solved

How to use Powershell to backup SQL Database

Posted on 2014-09-03
6
388 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 29

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

929 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

12 Experts available now in Live!

Get 1:1 Help Now