Solved

Sql backup & restore batch file needed.

Posted on 2014-09-21
6
1,806 Views
Last Modified: 2014-09-22
Hi Experts,

I am looking for a batch file that would accomplish the following:

1- create a backup of a given sql database
2- copy that backup to another location (a different pc on the network)
3- Upon completing steps 1 & 2, restore the database on that other location.
0
Comment
Question by:bfuchs
6 Comments
 
LVL 4

Expert Comment

by:colditzz
ID: 40335821
I don't have an example for you, but you would need to utilise T-SQL inside the batch file, I have done this before successfully.

These links should help;
- Backup (http://msdn.microsoft.com/en-us/library/ms186865.aspx)
- Restore (http://msdn.microsoft.com/en-us/library/ms186858.aspx)

I would use 'copy' in the batch file to move the DB (.mdf & .ldf pair) to the other server/instance.

Hope this helps
0
 
LVL 24

Accepted Solution

by:
Mohammed Khawaja earned 500 total points
ID: 40335862
The following batch file could do it for you assuming you are using the logged on account to backup database named DB1 (i.e. NT/AD account and not SQL account) with following assumptions:
Server1 = Source Server
Server2 = Destination Server
DB1 database exists on both servers
InstanceName = Name of SQL instance, if using default SQL then just put the server name
Logged on account has admin privileges to both SQL servers (i.e. sysadmin roles)

Below is the batch file and it is running from the BINN folder where SQLCMD.EXE resides:


SqlCmd -E -S Server1\InstanceName -Q “BACKUP DATABASE DB1 TO Disk=’D:\DB1.bak’”

copy D:\DB1.bak \\server2\d$\db1.bak /y

SQLCmd -E -S Server2\InstanceName -Q "Restore DATABASE DB1 FROM DISK='D:\DB1.bak'"
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40336079
bfuchs, what's the idea of your question?
Since you are asking for a batch file, can we assume that will be a schedule task? Something that you need to do often?
And which kind of use you are giving to the restored database?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Author Comment

by:bfuchs
ID: 40337895
@Mohammed Khawaja,

I tested yours and works fine, just wonder
1- what are the meaning of all those switches (-e,-s,-q)?
2-when i created a script from the wizard, it gave me in addition to restore the following "WITH  FILE = 1, NOUNLOAD,  REPLACE,  STATS = 10", do I need it?

@Vitor
Our server is going bad lately and my manager would like we should be backed up at any given moment..

@All
Is something wrong by performing backups while users are in the database?
and what is about copying an Access database while users are in (part of our data is still in Access), can this cause any type of corruption?


Thanks
Ben
0
 
LVL 24

Expert Comment

by:Mohammed Khawaja
ID: 40338090
-E is the default and can be ignored
-S is  to define the server name
-Q is to define the query which goes in quotes

A backup can be taken at any time with or without users connected.  Script I provided you performs full backup of the database.
0
 
LVL 3

Author Closing Comment

by:bfuchs
ID: 40338178
Thank you!
0

Featured Post

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

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

22 Experts available now in Live!

Get 1:1 Help Now