January 11, 2012 at 8:22 am
Hi all,
I have been working on this script all day trying to get it to work.
Just can't figure it out. There are no error messages but nothing happens.
The script was copied of the internet and adjusted for my purposes. I have a large number of database that will have to be created soon. All I will get is a folder with a llot of backupfiles and possibly a list with databasenames .
Any help would be greatly appreciated,
-- Use VARCHAR as the restore statement doesn't like NVARCHAR
DECLARE
@data_file_path VARCHAR(512)
, @data_file_1_path VARCHAR(512)
, @log_file_path VARCHAR(512)
, @backup_path VARCHAR(512)
, @backup_extension VARCHAR(4)
, @mdf_extension VARCHAR(4)
, @ndf_extension VARCHAR(4)
, @ldf_extension VARCHAR(4)
-- ** VARIABLES THAT MUST BE SET **--
SET @data_file_path = 'D:\Data\'
SET @data_file_1_path = 'D:\Data\'
SET @log_file_path = 'D:\Logs\'
SET @backup_path = 'D:\Klantenmap\'
-- **----------------------------**--
SET @backup_extension = '.bak'
SET @mdf_extension = '.mdf'
SET @mdf_extension = '.ndf'
SET @ldf_extension = '.ldf'
DECLARE @DATABASES_TO_RESTORE TABLE
(rownum int IDENTITY (1,1) PRIMARY KEY NOT NULL,
backup_name VARCHAR(64),
restore_as VARCHAR(64));
-- ** Declare the Databases to be Restored ** -- INSERT INTO @DATABASES_TO_RESTORE
SELECT '25000994', '25000994'
UNION
SELECT '25001905', '25001905'
-- ** -------------------------------------** --
DECLARE @max_rows INT, @Row_Count INT
SET @Row_Count = 1
SELECT @max_rows=count(*) FROM @DATABASES_TO_RESTORE
WHILE @Row_Count <= @max_rows
BEGIN
DECLARE
@backup_nameVARCHAR(32)
, @restore_asVARCHAR(32)
, @logical_data_nameVARCHAR(64)
, @logical_data_1_nameVARCHAR(64)
, @logical_log_nameVARCHAR(64)
, @data_file_full_pathVARCHAR(512)
, @data_file_1_full_pathVARCHAR(512)
, @log_file_full_pathVARCHAR(512)
, @full_backup_pathVARCHAR(MAX)
, @cmdVARCHAR(128)
SELECT
@backup_name = backup_name,
@restore_as = restore_as
FROM @DATABASES_TO_RESTORE
WHERE rownum = @Row_Count
SET @full_backup_path = @backup_path + @backup_name + @backup_extension
DECLARE @filelist TABLE
(LogicalNameNVARCHAR(128) NOT NULL,
PhysicalNameNVARCHAR(260) NOT NULL,
[Type]CHAR(1) NOT NULL,
FileGroupNameNVARCHAR(120) NULL,
SizeNUMERIC(20, 0) NOT NULL,
MaxSizeNUMERIC(20, 0) NOT NULL,
FileIDBIGINT NULL,
CreateLSNNUMERIC(25,0) NULL,
DropLSNNUMERIC(25,0) NULL,
UniqueIDUNIQUEIDENTIFIER NULL,
ReadOnlyLSNNUMERIC(25,0) NULL ,
ReadWriteLSNNUMERIC(25,0) NULL,
BackupSizeInBytesBIGINT NULL,
SourceBlockSize INT NULL,
FileGroupIDINT NULL,
LogGroupGUIDUNIQUEIDENTIFIER NULL,
DifferentialBaseLSN NUMERIC(25,0)NULL,
DifferentialBaseGUID UNIQUEIDENTIFIER NULL,
IsReadOnlyBIT NULL,
IsPresentBIT NULL,
TDEThumbprintVARBINARY(32) NULL)
INSERT into @filelist
EXEC ('RESTORE FilelistOnly FROM DISK = ''' + @full_backup_path + '''')
IF @@ROWCOUNT = 2
BEGIN
--SELECT @logical_data_name = LogicalName FROM @filelist WHERE [Type] = 'D'
--SELECT @logical_log_name = LogicalName FROM @filelist WHERE [Type] = 'L'
SET @data_file_full_path = '@data_file_path' + '@restore_as' + '@mdf_extension'
SET @data_file_1_full_path = '@data_file_1_path' + '@restore_as' + '@ndf_extension'
SET @log_file_full_path = '@log_file_path' + '@restore_as' + '@ldf_extension'
RESTORE DATABASE @restore_as
FROM DISK = @full_backup_path WITH FILE = 1,
MOVE N'U4S33_Data' TO @data_file_full_path,
MOVE N'U4S33_Data1' TO @data_file_1_full_path,
MOVE N'U4S33_Log' TO @log_file_full_path
END
ELSE
PRINT 'CANNOT RESTORE DATABASE ' + @restore_as + ' THE BACKUP CONTAINS MORE THAN 1 BACKUP SET'
SELECT @Row_Count = @Row_Count + 1
END
January 11, 2012 at 8:35 am
Usually, we backup all databases with a script. Restore is usually per database (as on need). Why do you want to restore all databases? How many databases you need to restore with this script?
Copying from blogs & running it blindly on PROD server is very dangerous. Test your scripts on Test Server first.
January 11, 2012 at 8:44 am
Hi,
The databases are for a cloud invironment.
Al large number of customers will have to be created on the databaseserver.
The customerdatabases will be delivered as .bak files.
Cheers,
Cor
January 11, 2012 at 8:55 am
If itβs less than 20, I would still prefer writing individual restore commands else your approach.
January 11, 2012 at 8:57 am
Actually it os somewhere between 300 and 700
Cheers,
Cor
January 11, 2012 at 9:08 am
Now I agree with your approach.
Please add a PRINT statement in IF block for debugging and post the results.
IF @@ROWCOUNT = 2
January 11, 2012 at 9:12 am
Looking at the restore command itself, logically it makes sense. You're going to have to break down & issue print statements to validate that everything is working. Instead of trying to run the restores, just issue a select statement so that you see all the data coming back and you can ensure that it's correct to fill in the properties for the restore statement. Then try using the data in a single restore statement. Nothing is jumping out as being especially problematic.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
January 11, 2012 at 9:16 am
Hi ,
Actually I added serveral ' print step(n) ' to the script to see how far it would go.
It stops short of this part:
-- ** -------------------------------------** --
DECLARE @max_rows INT, @Row_Count INT
SET @Row_Count = 1
SELECT @max_rows=count(*) FROM @DATABASES_TO_RESTORE
The piece that procedes it works just fine.
SELECT '25000994', '25000994'
UNION
SELECT '25001905', '25001905'
A select on the temporary database shows the values.
But after this nothing is printed anymore despite the print statements that follow.
cheers,
Cor
January 11, 2012 at 9:21 am
Please alter your script with following & let us know the result.
--IF @@ROWCOUNT = 2
IF 1 = 1
January 11, 2012 at 9:25 am
Hi,
Same as before I am afraid.
(2 row(s) affected)
The values in the temporary database are shown. Nothing more.
cheers,
Cor
January 11, 2012 at 9:26 am
Also, if that insert statement is commented out, you'll never enter the WHILE loop.
January 11, 2012 at 9:32 am
I feel really stupid π
I have been looking at it all day and never saw the insert statement commented out.
Thanks!!
There are some more problems now but I will look at it tomorrow,
Looks like the variable substitution is failing.
Thanks all so far!!
Msg 5105, Level 16, State 2, Line 102
A file activation error occurred. The physical file name '@data_file_path@restore_as@mdf_extension' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 3156, Level 16, State 3, Line 102
File 'U4S33_Data' cannot be restored to '@data_file_path@restore_as@mdf_extension'. Use WITH MOVE to identify a valid location for the file.
Msg 5105, Level 16, State 2, Line 102
A file activation error occurred. The physical file name '@data_file_1_path@restore_as@ndf_extension' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 3156, Level 16, State 3, Line 102
File 'U4S33_Data1' cannot be restored to '@data_file_1_path@restore_as@ndf_extension'. Use WITH MOVE to identify a valid location for the file.
Msg 5105, Level 16, State 2, Line 102
A file activation error occurred. The physical file name '@log_file_path@restore_as@ldf_extension' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 3156, Level 16, State 3, Line 102
File 'U4S33_Log' cannot be restored to '@log_file_path@restore_as@ldf_extension'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 102
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 102
RESTORE DATABASE is terminating abnormally.
January 11, 2012 at 9:46 am
No worries, done it plenty of times myself π
Your set statements are just setting your paths to e.g. '@data_file_path@restore_as@mdf_extension'
Drop all the ' from them and you should be ok.
January 12, 2012 at 12:32 am
Indeed that does seem to solve the problem except for 1 thing:
Msg 8114, Level 16, State 12, Line 103
Error converting data type varchar to nvarchar.
it seems to be here:
RESTORE DATABASE @restore_as
FROM DISK = @full_backup_path WITH FILE = 1,
MOVE 'U4S33_Data' TO @data_file_full_path,
MOVE 'U4S33_Data1' TO @data_file_1_full_path,
MOVE 'U4S33_Log' TO @log_file_full_path
I tried setting @restore_as to NVARCHAR but that didn't work.
I then added these satements to the script
print @data_file_full_path
print @data_file_1_full_path
print @log_file_full_path
Results:
D:\Data\25000994.ndf
D:\Logs\25000994.ldf
The first SET statement is not shown.(.mdf file) So I think the problem is there.
Cheers,
Cor
January 12, 2012 at 12:47 am
It turned out there was a typo in the
SET @ndf_extension = '.ndf' statement.
everything works now
Thanks to all for your help!
Much appreciated!
Cheers,
Cor
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply