Solved

Sql backup & restore batch file needed.

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

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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