Solved

Sql backup & restore batch file needed.

Posted on 2014-09-21
6
1,885 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 25

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 47

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

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 25

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 4

Author Closing Comment

by:bfuchs
ID: 40338178
Thank you!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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 article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

19 Experts available now in Live!

Get 1:1 Help Now