Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Restore from .BAK file
Message
 
 
À
03/01/2014 19:02:35
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Copies de sauvegarde
Versions des environnements
SQL Server:
SQL Server 2012
Application:
Web
Divers
Thread ID:
01591308
Message ID:
01591328
Vues:
43
>>>I need to "move" a bunch of databases from one computer to another. I have created backup of all databases to an external drive. That is, the external drive now has several .BAK files. Do I need to create the databases on the target computer before I can restore from the .BAK files? Or can I restore and create database from .BAK in one step?
>>>
>>>TIA
>>
>>In order to write a script that would restore all databases from a certain folder - without hard-coding each database name - I would need to scan all files with extension *.BAK. How can I create a cursor and scan all files from a folder into the cursor (using T-SQL)?
>>Or is there a better way to approach the task?
>
>Not quite sure you can use variables of any kind in the restore script. At least, it was impossible nine years ago on SQL 2000 when I last tried it.
>
>You may have a template in Fox where you'd merge the filenames and logical names as literals, but then I really don't know if you'd know these names from the bak files themselves. You may need to execute a query to get the database names on the first computer... Perhaps if you had created the backups programmatically, or had some kind of naming convention so you'd know the database's logical name and filename from the name of the .bak file, or if you had all three in some metadata list (even a text file would do).
>
>IOW, if you have less than a dozen, it's not worth trying to automate the task. Just create a script to restore one, then edit it to change the names. Mind you, you need absolute paths of each file.
>
>Here's the script I came up with back then, which I'm editing for each case (but then I have just a few databases each year):
>
>
use master
>
>IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'dbName')
>	DROP DATABASE [dbName]
>create database dbName on 
>	(name = 'dbName',
>	filename=N'x:\path\to\new\db\dbName.mdf')
>	log on (name = 'dbName_log',
>	filename=N'x:\path\to\new\db\dbName.ldf')
>restore filelistonly
>	from disk='y:\path\to\backup\dbname_backup.bak'
>
>restore database  [dbName]
>	from disk='y:\path\to\backup\dbname_backup.bak'
>	with replace, 
>	move 'dbName' to 'x:\path\to\new\db\dbName.mdf',
>	move 'dbName_log' to 'x:\path\to\new\db\dbName.ldf'
>
>
>The first two commands may not be necessary nowadays, but I keep them just in case. I'm not really keen on knowing what each does and why.

First, I agree, that for the number of files I have (about 15) I can simply create hard-coded script and execute it. I don't understand everything in your script so I will have to study it. Like, for example, you said that the first two commands may not be necessary. Which commands and why not?
Also I found the following Restore script online (in one of the forums):
RESTORE DATABASE [db1] FROM  DISK = N'C:\folder\db1.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 10
But I don't understand the purpose of "WITH FILE =1, NOUNLOAD, STATS = 10." A lot to learn.
Thank you for your help.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform