Solved

Sql backup & restore batch file needed.

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

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 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 3

Author Closing Comment

by:bfuchs
ID: 40338178
Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

13 Experts available now in Live!

Get 1:1 Help Now