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

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.

Related

Backup database - SQL Server

I need make a backup of my SQL Server database. When I try, I get this error:
System.Data.SqlClient.SqlError: Read on "c:..." failed: 23(...)(Data error (cyclic redundancy error))
Now, I'm trying to run this command:
DBCC CheckDB ('MYDATABASE') WITH NO_INFOMSGS, ALL_ERRORMSGS
But I get this error
Msg 8921, Level 16, State 1, Line 18
Check terminated. A failure was detected while collecting facts. Possibly tempdb out of space or a system table is inconsistent. Check previous errors.
What can I do? I just need make a backup.
I'm using Microsoft SQL Server Management Studio.
First of all, check the Service Account used for the SQL Server Instance from Services.
Ensure the service account have enough permission for read/write at the exact location for Backup in Physical Disk.
Ensure the the user (the user you using to login in SQL Instance) have enough permission to perform backup.
Final option to recover the data from the database is create another database with same tables (blank) in different machine in different SQL instance, then Export all the database to new database using Management studio (Right click on the Database > task > Export Data)

Dynamic restore multiple databases from a folder

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!

SQL Server 2000 backup recovering on SQL Server 2005

I've got a SQL Server database backup (file extension .bak) from an project of 12 y/a which I tried to restore on SQL Server 2005.
But this gave me an error that it was not the right version..
So I'm trying to find a SQL Server 2000 version to see if I can restore it with this but I can't find any version that works.
Is there another program to save my backup?
Copied over from msdn forums:
1) Go to restore database
2) Select the database that you want to back up to
3) Locate the backup file on disk. You may have to put it into the MSSQL Server -> MSSQL.1-> MSSQL -> Backup Folder. It must be a .bak file.
4) Select the back that want to restore from the available backups.
5) Go to the top left "options" property and when you do that select "overrite existing database".
6) Now make sure that the path to the files on database to be restored are correct in this same dialog view. Look at the paths to the database file and the log file and make damn sure that they are the correct ones for the database to be restored. The problem here is that those paths are going to be for the filesystem that the backup came from, not the one you are goning to put the restore onto.
Just to add to that, if you are restoring a database to a new server that does not have the database already in it (to restore to), create a sham database with the same name, then restore to it with the overwrite settings mentioned above.

How do I copy SQL Server 2012 database to localdb instance?

I'm looking to copy a SQL Server 2012 Standard database to my localdb instance. I've tried the wizard which complains that localdb isn't a SQL Server 2005 or later express instance. I also did a backup/restore but upon the restore in my localdb I get the following error...
Running this...
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
REPLACE
Error message I get...
Processed 8752 pages for database 'CSODev', file 'CSOdev_Data' on file 1.
Processed 5 pages for database 'CSODev', file 'CSOdev_Log' on file 1.
Msg 1853, Level 16, State 1, Line 1
The logical database file 'CSOdev_Log' cannot be found. Specify the full path for the file.
Msg 3167, Level 16, State 1, Line 1
RESTORE could not start database 'CSODev'.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
The database ends up in "Recovery Pending" mode. It seems like it has issues with the log file. I have tried 2 different backups in case one was just corrupted.
There is known limitation (a real bug, in fact) for localDB. It will fail any RESTORE with MOVE whenever your database files are located in different folders.
You have to restore in the original folders (no MOVE). Use cmd tool such as SUBST if you need to fake a drive:/path.
I had the same problem. What eventually did work was this:
Trying to restore the database (getting the error in the OP)
Detaching the database
Reattaching the database
What happened in the last step was that SSDT performed an upgrade of the data files, that apparently was in an older format. When that was finished, the database started working without any problem!
I had the same issue, and after doing a little online research I came across an ingenious way to get it to work (albeit quite hacky). Basically, you:
Create a SqlLocalDb instance (SqlLocalDb c tmp -s).
Restore the database as you did above (e.g., SqlCmd -E -S <localdb connection string> -Q "RESTORE DATABASE ...").
Stop the SqlLocalDb instance (SqlLocalDb p tmp).
Delete the SqlLocalDb instance (SqlLocalDb d tmp).
Create a new SqlLocalDb instance (SqlLocalDb c persistent -s).
Create the database in the new instance by attaching it (SqlCmd -E -S <persistent connection string> -Q "Create Database <dbname> On (Filename = '<Mdf file location'), (Filename = '<Ldf Filename'>) For Attach".
And hopefully it should work. See here for original idea.
Edit: Added Jason Brady's correction of the create command.
Try scripting your database as schema and data and then running the script locally.
RESTORE FILELISTONLY
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
ALTER DATABASE yourdatabasename
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE yourdatabasename
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
with replace,
move 'logical name from file stream' to
'C:\yourdatabase.mdf',
move 'logical name from file stream' to 'C:\Yourdatabase.ldf'
ALTER DATABASE Qatar_DB SET MULTI_USER
Same problem, thanks for the help.
My local database is MS SQL 2014.
Open "SQL Server 2014 Management Studio"
Right click the database, go to "Tasks", click "Take Offline"
Detach the database
Attach the database
It work for me.
After you backup the database, you can restore the database without error.
Thanks.
I had the same problem. Try running visual studio as Administrator and try the following command
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH NORECOVERY, MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
UPDATE: This did not work exactly!
Although the above statement does not produce any errors and completes successfully, the database remains in "PENDING RECOVERY" state and cannot be accessed in any way. When I tried to 'RESTORE WITH RECOVER' to bring the database online I got the same error as in the question above.
So in my case I ended up restoring the backup to a DEV server I have running with MSSQL 2008 R2 and then chose: Tasks -> Generate Scripts -> chose objects to script & Next -> click on "Advanced" button -> select "types of data to script" : Schema & data.
Now run the generated script against the local db.
Try these scripts (example with adventureworks2012 that I personally tested):
RESTORE FILELISTONLY
FROM DISK = 'c:\temp\adv2012.bak'
This will bring up the filenames as:
AdventureWorks2012 C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012.mdf
AdventureWorks2012_log C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012_log.ldf
Use these filenames to cinstruct your final script as this:
RESTORE DATABASE AdventureWorks2012
FROM DISK = 'C:\temp\adv2012.bak'
WITH MOVE 'AdventureWorks2012' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012.mdf',
MOVE 'AdventureWorks2012_log' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012_log.ldf',
REPLACE;
BTW I run these through Visual Studio (SQL Server Object explorer), but I strongly suspect this could be run on SSMS easily ;-)
You can do it manually. this can be done by using dot net and opening two kinds of connections and forwarding data from one of them to the other. but this needs to create the same types of columns in the local one.
You can check the importing options of MS Access 2007

Sql Server 2005 Restore Failing

Running sql server 2005 I have database A. I am trying to restore from a backup of A to database B. I want to retain the database A and create a new testing database B from a previous set of data.
I tried to create B and restore from the .bak AND restore database to B from management studio.
The error is...
TITLE: Microsoft SQL Server Management
Studio
Restore failed for Server
'195448-APP2'.
(Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476
------------------------------ ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The
backup set holds a backup of a
database other than the existing 'B'
database. (Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&LinkId=20476
------------------------------ BUTTONS:
OK
I found this snippet which I am hesitant to use and want to ask if it would solve my problem of changing the location of the mdf and ldf during the process of restoring the database or does it replace database A's items altogether.
ALTER DATABASE AdventureWorks
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE AdventureWorks
FROM DISK = 'C\:BackupAdventureworks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:\Data\datafile.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\Data\logfile.ldf',
REPLACE
[http://blog.sqlauthority.com/2007/04/30/sql-server-fix-error-msg-3159-level-16-state-1-line-1-msg-3013-level-16-state-1-line-1/][1]
and for me I would make it...
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'B' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'B_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
What I don't know is if it will affect database A at all. I am hoping the replace refers files associated with B.
or if it should be
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'A' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'A_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
If anyone could help me with the error and/or confirm this fix I would be very grateful as it is not my database I'm playing with.
Thanks.
You could simply use the Copy Database Wizard.
If you wanna do it like pros and use T-SQL the RESTORE .. MOVE ... REPLACE will do what you expected: move the two files at the locations you intend and replace database B with content from the backup. A will be unaffected.
I would use the wizard if I were you: In Sql Server Management Studio right click on "Databases" and select "Restore Database...". This dialog / wizard will do exactly what you are asking - simply select the source .bak file(s) / Database that you want to restore from, enter the name of the database you want to restore to and hit "Ok".
Some notes - if you enter the name of a database that doesnt yet exist (it sounds like this is what you want to do), it will create that database for you. If you enter the name of an existing database it will attempt to restore to that database. If you attempt to restore to an existing database from a backup made of a different database it will fail, however you can force the Sql Server to overwrite the existing database by going to "Options" and checking the "Overwrite the existing database" checkbox.
Also, if you are restoring a backup of an existing database to create a new second copy of that database you may find that the wizard fails as its attempting to create a database using the same database file paths as the ones currently in use by the source database. To fix this you need to click on "Options" and change all of the "Restore As" file paths to files that dont yet exist.
You can also get this wizard to generate an SQL script instead of actually performing the actual restore (click on the "script" button at the top), which is handy if you want learn how to do this sort of thing in raw SQL instead.
It is possible to restore a database from sql server 7, sql server 2000 to sql server 2005.
It can be achieved by using restore and with replace command
use master
restore database mydatabase from disk ='c:\mybackup.bak' with replace
It is easy to take a backup in sql server using this script without any external tools.
Check out Restore with Example.
drop database b first. then do the restore.