Dynamic restore multiple databases from a folder - sql

I am tasked with restoring several SQL Server databases from .bak files generated from another server in another organization. I need to be able to add automation to this process as it will have to occur daily.
The challenge I am having is that the .bak files have different names every day, like: DatabaseName_Full_Backup_Date_Time_xxxxxx.bak. Also the data and log files need to be moved (MOVE) during the restore to my server's data and log file locations.
I decided to use T-SQL, hoping to find a robust solution that could be implemented from a SQL Server Agent scheduled job that restores the databases, applies the original permissions, and moves\drops the old .bak files to an archive location.
I tried solving this with PowerShell but the -relocate file did not work. Also tried this stored procedure (dbo.spRestore_All_Backups_From_Folder) proposed on this website here which is very close to what I need but in my SQL Server 2016 machine, I get an error when restoring backups generated from another server.
The script works with .bak files from databases that have been backed up on the same server and no MOVE of files is needed.
The error I get when I attempt a restore of a .bak file from another server is:
Msg 213, Level 16, State 7, Line 300
Column name or number of supplied values does not match table definition.
Msg 3013, Level 16, State 1, Line 300
RESTORE FILELIST is terminating abnormally.
Thank you for all input and advice!

Related

Can I use a script to restore multiple SQL databases from .bak files?

I'm migrating 100 plus SQL databases over to a new server in a new location. I have the maintenance plan in place to take full back ups and then a script to copy over the .bak files to a temp directory. What I'm looking for is a way to mass restore those .bak files so that I don't have to spend all weekend doing it manually.
I found a decent stored procedure here : SQL Restore
I put it in place and tested and I'm getting the following error:
Msg 3118, Level 16, State 1, Line 1
The database "TF058" does not exist. RESTORE can only create a database when restoring either a full backup or a file backup of the primary file.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
I'm running SQL Server 2012 and the databases are in FULL recovery mode.

SQL restore database from .bak to a different machine - Errors

I am trying to restore a database from a backup I got on the hosting server.
I have checked the logical names using:
restore FILELISTONLY from
disk = 'c:\DBBackup2_17092013.bak'
and used them to perform the following query:
restore database StadlerTest
from disk = 'c:\DBBackup2_17092013.bak'
with replace,
move 'Stadler_base' to 'C:\Program Files\Microsoft SQL
Server\MSSQL10.SQLEXPRESS\MSSQL\StadlerTest.mdf',
move 'Stadler_base_log' to 'C:\Program Files\Microsoft SQL
Server\MSSQL10.SQLEXPRESS\MSSQL\StadlerTest.ldf'
I run the above two queries from master after I created the empty database StadlerTest but I am getting a series of error that sound like these (translation from italian):
""Message 3634, level 16, state 1, row 1
The operating system returned error '5(Access denied.)' during the attempt to 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\StadlerTest.mdf'.""
I am doing something wrong but I do not know what is wrong, I already checked several answers given on SO and other sites but I am not getting it right. Some help will be appreciated. Thanks.
Sounds like a permissions error. SQL Server may not have permissions to create files there. Remember, SQL Server is trying to create the database under its service account, not your windows login. Check the permissions of the SQL Server account, found with all other logins under Security -> Logins in management studio.
However, you should not create databases on the C drive. If they grow and the C drive runs out of space your whole computer and operating system will tank. If at all possible, put the database files on a different drive. If this is just for personal testing use, that is fine but not for any important system.

how can i attach an MDF file to a server with the same LDF name?

I have a .MDF backup of a database. I need some information from this .MDF.
When I try to attach this .MDF, it requires the log file with which it shipped.
I don't need the log file, but it insists on the .LDF file.
I tried to point it to the same log file with which it shipped, but I am getting the error:
(Since it is in use by the current version of the database.)
I am unable to attach it to a different server because it was originally on a SQL Server 2012, and that is my only SQL Server 2012 server.
How do I reattach the .MDF without the .LDF?
I am adding a text version in case somebody else is searching for a solution to this issue:
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "D:\SQL Logs....Custom_log.ldf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
File activation failure. The physical file name "D:\SQL Logs....Custom_log.ldf" may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'TestDb'. CREATE DATABASE is aborted.
I don't know if this is relevant, but I actually do not need to attach .MDF file if I don't have to. I only need to read one table from there.
if you use 2012
use "FOR ATTACH_FORCE_REBUILD_LOG"
CREATE DATABASE [XXXdatabasname] ON
( FILENAME = N'databaseXXXFilePath.mdf' )
FOR ATTACH_FORCE_REBUILD_LOG
You can attach an MDF file without it's corresponding LDF file. To do this, I would use sp_attach_single_file_db

Error restoring a filelistonly from .bak file

I am trying to restore the filelistonly from a .bak file and am getting the following error:
Msg 3201, Level 16, State 2, Line 1 Cannot open backup device
'C:\Users...\myFile.bak'. Operating system error 5(Access is
denied.).
The file was initially blocked, but I unblocked it, so that shouldn't be the problem.
The error message explains exactly what is happening: you've placed the .BAK file in a folder that the SQL Server service account does not have access to.
You could "fix" this by granting access to your profile folder to the SQL Server service account, but this is an unnecessary violation of security best practices.
Much easier to just move the file to a location where the SQL Server service account already has native access, like the backup or data directory. You can find the data folder (there may be multiple valid locations) by looking at the result of the following query:
SELECT DISTINCT SUBSTRING(physical_name, 1, LEN(physical_name)
- CHARINDEX('\', REVERSE(physical_name)) + 1)
FROM [master].sys.master_files
WHERE [type] = 0;
And if you've backed up any databases (let's hope you have!), you can find out some valid backup locations this way:
SELECT DISTINCT SUBSTRING(physical_device_name, 1, LEN(physical_device_name)
- CHARINDEX('\', REVERSE(physical_device_name)) + 1)
FROM msdb.dbo.backupmediafamily;
Of course these don't take things into account such as read-only data files on read-only volumes, volumes SQL Server might have access to but you don't, volumes without enough free space for your .BAK file, or backup locations that have been used in the past but are no longer present or accessible.

Restore SQL 2008 database fails with error on page xxx:xxx

This is the situation:
Got a full backup (.bak file) of a SQL 2008 database, with partitions.
The .bak file is 100gb.
I need to restore this database on a different server, to a new database.
So, command is like this:
Restore Database [newname] FROM DISK= N'D:\mydatabase.bak' WITH FILE = 1
MOVE 'mydatabasename' TO 'C:\mydatabase.mdf'
MOVE 'Partition1' TO 'C:\`mydatabase_1.ndf'
etc..
STATS = 1
After 52 percent processed, I get this error:
Msg 3183, Level 16, State 2, Line 1
RESTORE detected an error on page (8481:555819297) in database "dbname" as read from the backup set.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Before all default suggestions come up, this is what I have already done:
Checkdb on original database --> no errors
Restore the .bak file on my local machine --> no errors, so the backup is correct.
What can I do to troubleshoot this? How can I get to the actual problem?
Thanks for any suggestions.
I'd check the MD5 checksums of the .bak as created on the server and after it's been moved to the new home. I'll bet something small got tweaked in your copy as you moved it over; something in your process, maybe just a network hiccup, borked something or other.
Few MD5 checksum utilities here: http://www.thefreecountry.com/utilities/free-md5-sum-tools.shtml
Best of luck.
We were able to restore the backup on other servers.
After a while, we switched over to a new server.
The old one with the errors is on a test bench now.
Conclusion is that the backup file was correct, and the problem lies in a hardware problem on the server. Probably disk problemens, but when thats clear, i will post it here..
Thanks for the suggestions.
yeah for such restoration errors the problem lies in the drive from where you are trying to restore the backup,try changing the drive (e.g d to e )..it worked for me.
one can also try attaching mdf file directly.