November 3, 2011 at 10:33 pm
I'm in the process of creating a database restore script for my staging server.
Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.
Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?
November 4, 2011 at 12:10 am
November 4, 2011 at 8:41 pm
sqlguy-736318 (11/3/2011)
I'm in the process of creating a database restore script for my staging server.Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.
Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?
What is the format of the filenames that you have? I ask because if they look like the following (has an ISO-style date and time embedded in the name), there is a VERY simple way to do this without using xp_CmdShell, SQLCLR, VBScripts, Powershell, or any other non-built-in method...
dbname_backup_200810292330.bak
--Jeff Moden
Change is inevitable... Change for the better is not.
November 5, 2011 at 1:33 am
Jeff Moden (11/4/2011)
sqlguy-736318 (11/3/2011)
I'm in the process of creating a database restore script for my staging server.Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.
Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?
What is the format of the filenames that you have? I ask because if they look like the following (has an ISO-style date and time embedded in the name), there is a VERY simple way to do this without using xp_CmdShell, SQLCLR, VBScripts, Powershell, or any other non-built-in method...
dbname_backup_200810292330.bak
Hi Jeff - The format of the file is MyDB_20111105_1030.bak.
November 24, 2011 at 4:08 pm
Oh, shoot. My apologies. I lost track of this thread. Here's the solution given the file name pattern you posted. I hope I'm not too terribly late with it.
DROP TABLE #File
GO
--===== Create a holding table for the file names
CREATE TABLE #File
(
FileName SYSNAME,
Depth TINYINT,
IsFile TINYINT
)
;
--===== Capture the names in the desired directory
-- (Change "C:\Temp" to the directory of your choice)
INSERT INTO #File
(FileName, Depth, IsFile)
EXEC xp_DirTree 'C:\Temp\',1,1
;
--===== Find the latest file using the "constant" characters
-- in the file name and the ISO style date.
SELECT TOP 1
FileName
FROM #File
WHERE IsFile = 1
AND FileName LIKE 'MyDB__20[0-9][0-9][0-1][0-9][0-3][0-9]__[0-2][0-9][0-5][0-9].bak' ESCAPE '_'
ORDER BY FileName DESC
;
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy