?
Solved

Sql backup & restore batch file needed.

Posted on 2014-09-21
6
Medium Priority
?
2,144 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
[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
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 2000 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 51

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

801 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