I am trying to come up with a script to dynamically do 'Restore Database' in SQL Server 2019 with multiple BAK files that are located in one folder on a daily basis.
I will do the manual full backup first, and this task will be scheduled to run after on a daily basis.
The plan is go thru each BAK files (located in the same folder) sequentially (for example here: 04 --> 12).
There will be another Python script which will wipe out old files, so I am only focusing on execution of Restore database with BAK files that are located in this folder.
What should be the best approach?
I have attached a screenshot that has all BAK files.
I have some T-SQL code here (that I copied from other thread), but I am not sure how to loop over each BAK file sequentially.
And I am not sure whether I have to deal with mdf and ldf files as listed here.
RESTORE DATABASE [Test] FROM DISK ='\\Folder\LOG...04.bak'
WITH
--what ever options... but likely a file move
MOVE 'data_file_1' TO 'E:\somefolder\data.mdf',
MOVE 'db_log' TO 'E:\somefolder\log.ldf',
REPLACE, --overwrites the database
RECOVERY --sets the DB to READ/WRITE. Use NORECOVERY if you need to restore
logs / differentials
GO
RESTORE LOG Test
FROM '\\Folder\Log...04.bak' --assuming this is a log
WITH FILE = 1, --this is the first log
WITH NORECOVERY; --keep in norecovery to restore other logs...
GO
Related
I'm using microsoft SQL server. I have a database that we can call database1 and its backup in .bak format.
I need to restore the backup to another, blank database that we can call database_temp, so both database must exist. I need to get and confirm some data from the database_temp archive and then delete it when I'll end my job.
what I did:
1) created new database "database_blank"
2) tasks - restore database. In source I choose "device" and then my .bak file, in destination I select database_temp.
3) in option I choose Overwrite the existing database with replace.
I got the error "Restore of database failed the file cannot be overwritten it is being used by database1 database"
You have to move the database files.
e.g.
RESTORE DATABASE [AdventureWorks] FROM DISK = 'c:\backup\Adv.bak'
WITH CHECKSUM,
MOVE 'AdventureWorks_Data' TO 'c:\mssql\data\AdventureWorksCopy_Data.mdf',
MOVE 'AdventureWorks_Log' TO 'c:\mssql\log\AdventureWorksCopy_Log.ldf',
RECOVERY, REPLACE, STATS = 10;
The first part of the move is the logical file name of the database file. Right click on the original database - Properties - Files to get the right names.
The full documented syntax can be found here
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' ;
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.
I am using SSMS 2017 and attempting to restore a SQL Server 2016 backup (.bak) file to a new database (the data file and log file names in the original backup are different from the new database I am restoring to), so for example, db1.bak is being used to create a new database called db2 through a restore operation.
After the restore completes, all stored procedures and views are present, but none of the tables are there.
These are the steps I've taken in SSMS to perform the restore:
Right click the Databases folder
Select "Restore Database"
Under the General Page: For "Source" I choose Device and select the db1.bak file, and for "Destination" I enter the name of the new database to be created from the backup (db2)
Under the Files Page I select Relocate all files to folder (both Data and Log folder paths have been pre-populated with the correct names) as have the Logical File Name and the Restore As paths.
Under the Options Page I either select nothing or select Overwrite the existing database (WITH REPLACE).
Once the restore completes "successfully" I expand the newly created db2 and click to expand the tables and receive this error message:
What am I doing wrong?
Here is the script:
USE [master]
RESTORE DATABASE [PdsClone] FROM DISK = N'F:\SQLBackups\PDS_backup_2017_12_12_001015_0199267.bak' WITH FILE = 1, MOVE N'PDS_Data' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone.mdf', MOVE N'PDS_Log' TO N'E:\Program Files\Microsoft SQL Server\MSSQL13.PDS\MSSQL\DATA\PdsClone_log.ldf', NOUNLOAD, REPLACE, STATS = 5 GO
UPDATE
I had the server group add another disk to that server and moved the SQL backups there, and I was able to successfully complete the restore this morning. I had previously cleaned off most of the backup files, but could it have been a space issue that prevented the tables from showing up in the restore? Every try it indicated success but no tables. They are there now, and I am thankful for your suggestions and help!
Not really an answer to what happened, but perhaps SSMS was doing something a little buggy as #JeroenMostert suggested.
SSMS 2012
Production server
Production database has Full recovery option.
I want to take a copy of this and restore on to same server with different name.
So I have taken a backup and choose restore but there is a message in the top left corner of the dialog which reads:
a tail-log backup of the source database will be taken...
Additionally, at this point I would rename the destination database, make sure that the rename has renamed the mdf and ldf files and that would be good to go. In this case it hasn't renamed the files so it looks like I will have to do that manually so as not to overwrite the prod database.
I haven't dealt with full recovery databases before. So questions:
What does the tail-log message mean and what are the implications?
Why did the destination database Rename not rename the mdf and ldf files - that's pretty dangerous huh?
What does the tail-log message mean and what are the implications?
Tailog backup message translates to "there is some transaction log generated after your last log backup,so please backup this log so that you can restore to any point in time until this backup"
Why did the destination database Rename not rename the mdf and ldf files - that's pretty dangerous huh?
backup copies only the files,it won't rename them and i don't see why this is dangerous..
I see you trying to restore to the same instance with different name,so tail log backup is not required,since you tried to overwrite the existing database might be the reason for showing tail log backup message