Backup file's database - sql-server

I use Sql Server 2005.
I wanna write code for restore a backup file, and I need to know what is database's name that backup file belongs to it.
How can I know?

I think the closest you can get is restore filelistonly ...
RESTORE FILELISTONLY From DISK = 'C:\temp\mybackup.bak' with file = 1
This returns data and the LogicalName field is the SQL Filename of File 1 in the database before it was backed up

Related

I can not restore my database SQL Server Management Studio can't recognize my .bak files

I'm trying to restore a DB from the class. However, when I try to restore the .bak file, it seems like SSMS doesn't recognize it.
I gave full permissions to the folder which contains the .bak file (it is the default backup folder of ms SQL studio).
Steps I've taken to restore the .bak file:
Right click on DB -> Restore DB -> From device (selected the .bak file location) -> To Database (selected the DB destination)
Like here:
C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
The DB is not corrupt I checked 2 other DB same result so I have no idea what is the problem
Here images:
I can not see the DB and I have almost the latest version of MS SQL studio so I can not understand what is going on here and why I can not restore DBs
I checked here:
restore database in ssms 2017 selected bak file in device option and showing nothing in Backup sets to restore option and disable Ok button too
and here:
SQL Server Management Studio can't recognize .bak file
https://www.youtube.com/watch?v=U0FpXwQfBaU
It should be easy and simple like the video above but like I said above something is wrong here and I do not know what is it.
Image of my SQL version
Once you (i mean the service account) got full permissions on the folder where the .bak file sitting and file not corrupted you should be able to restore without any issue, but there are times things fail in GUI and work perfectly with command line. not sure the issue might be exists one of the fix of SSMS release notes
However, you probably want to try following:
Verify the backup file - it does the verification of backup file and detects any error/corruption within backup file
RESTORE VERIFYONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak';
Read the header - it returns information of backup file i.e. DB Version, appended backup files, LSN info and database recovery model etc..
RESTORE HEADERONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak';
Get list of file names from backup - This will be helpful if the file location is different from the source server from where the backup was created
RESTORE FILELISTONLY FROM DISK = 'C:\YourbackupLocation\DbName.bak' ;
Restore backup - actual restore via T-SQL
RESTORE Backup DBName FROM DISK = 'C:\YourbackupLocation\DbName.bak' with replace, recovery, stats;
To relocate files
RESTORE Backup DBName FROM DISK = 'C:\YourbackupLocation\DbName.bak'
with replace, recovery, stats
--- Get the logical name from the result of "RESTORE FILELISTONLY" command
move 'DBName' to 'C:\NewLocation\DBName.mdf',
move 'DBName_Log' to 'C:\NewLocation\DBName_log.ldf' ;

Checking multiple SQL Server .bak backup files are from the same backup

What would be the best way to check that SQL Server .bak files are from the same backup? Say I backup a database like this:
BACKUP DATABASE AdventureWorksDW2017_2
TO DISK = 'C:\backup\advBackup2Peices\1.bak',
DISK = 'C:\backup\advBackup2Peices\2.bak',
DISK = 'C:\backup\advBackup2Peices\3.bak'
GO
Now if I run LABELONLY, HEADERONLY or FILELISTONLY on one of these backup files, I would get information about this backup - like this:
RESTORE FILELISTONLY
FROM DISK = 'C:\backup\advBackup2Peices\1.bak'
Without running the actual backup command, I would like to figure out this information below about these files (maybe using the above commands or any other commands)
How can I make sure that all these .bak files belong to the same backup?
Also, how to make sure the number of files this backup should contain (like above backup contains 3 files)?
The BackupSetGUID value of RESTORE HEADERONLY is the same for files of the same backup. The FamilyCount of RESTORE LABELONLY is the number of backup files.

Restoring .File Extension SQL Server have empty content

Can someone help me restore this backup database?
I backup my database to my D drive it created file with .file extension after that i uninstall my sql server then re install it when i try to restored the database it showed no database.
i ve tried to restore using File and Filegroups but there is no database to select.
this is my file
https://drive.google.com/file/d/0B-VCua2N1hePQjNxa3hCSy0tZjQ/view
it only contain created table with no data
Run something like this, just specify correct path to a file and where to restore a database:
USE [master]
RESTORE DATABASE Test FROM
DISK = N'D:\....\TreasuryDB' WITH FILE = 1, NOUNLOAD,
MOVE N'TreasuryDB' TO N'D:\....\Data\Test.mdf',
MOVE N'TreasuryDB_log' TO N'D:\....\Log\Testlog.ldf'

no backup selected for restore sql server 2012

I am trying to restore db from my db backup file when i select and add the db .bak file
I have seen that error "No back upset selected to be restored"
I also tried from script to restore it but failed.
kindly tell me some effective way to do that??
I have already tried this but failed
RESTORE DATABASE <Your Database>
FROM DISK='<the path to your backup file>\<Your Database>.bak'
Try RESTORE HEADERONLY to see if there are several backup sets in your bak file
RESTORE HEADERONLY
FROM DISK='<the path to your backup file>\<Your Database>.bak'
http://msdn.microsoft.com/en-us/library/ms178536.aspx
and if there are several sets, choose the one you want :
RESTORE DATABASE <Your Database>
FROM DISK='<the path to your backup file>\<Your Database>.bak'
WITH FILE = <you set number>;

Restore MSSQL database backup to a new database

I need to make clean database template backup and then generate a new one database with another name, but with backuped content.
Backup script is:
sqlcmd -S %DB_HOST% -Q "BACKUP DATABASE %DB_NAME% TO DISK = '%BACKUP_FILE%'"
Restore script is:
sqlcmd -S %DB_HOST% -Q "RESTORE DATABASE %DB_NAME% FROM DISK = '%BACKUP_FILE%' WITH REPLACE"
And it works on the same database name.
But if i backup DB 'ORIGINAL_DB_NAME' and then restore it to 'NEW_DB_NAME' it will raise exception:
Msg 1834, Level 16, State 1, Server <HOST>, Line 1
The file '.....\MSSQL\Data\ORIGINAL_DB_NAME.mdf' cannot be overwritten. It is being used by database 'ORIGINAL_DB_NAME'.
....
You need to specify a new location for the data/log files.
See this MSDN article, Example D
RESTORE DATABASE AdventureWorks2012
FROM AdventureWorksBackups
WITH NORECOVERY,
MOVE 'AdventureWorks2012_Data' TO
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'AdventureWorks2012_Log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH RECOVERY;
You can also avoid that conundrum using SSMS. Under the Options tab, after you have selected a source for your restore direct the data and log files to either the files you want to overwrite, or point them to the directory you need and name them appropriately.
If you go to Files on the Restore Configuration, make sure that the Original File Name is not the same as Restore As, that should solve your issue.

Resources