Skip to content

Instantly share code, notes, and snippets.

@LetsGoRafting
Created August 2, 2018 23:55
Show Gist options
  • Save LetsGoRafting/12fb757c64856738c6144f8b73944d4a to your computer and use it in GitHub Desktop.
Save LetsGoRafting/12fb757c64856738c6144f8b73944d4a to your computer and use it in GitHub Desktop.
DECLARE @dbName sysname
DECLARE @backupPath NVARCHAR(500)
DECLARE @cmd NVARCHAR(500)
DECLARE @fileList TABLE (backupFile NVARCHAR(255))
DECLARE @lastFullBackup NVARCHAR(500)
DECLARE @lastDiffBackup NVARCHAR(500)
DECLARE @backupFile NVARCHAR(500)
SET @dbName = 'someDBName'
SET @backupPath = 'Z:\BACKUP\LOCATION'
-- 3 - get list of files
SET @cmd = 'dir /OD /B ' + @backupPath
INSERT INTO @fileList(backupFile)
EXEC master.sys.xp_cmdshell @cmd
DECLARE backupFiles CURSOR FOR
SELECT backupFile
FROM @fileList
WHERE backupFile LIKE '%.trn'
OPEN backupFiles
-- Loop through all the files for the database
FETCH NEXT FROM backupFiles INTO @backupFile
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd = 'RESTORE LOG ' + @dbName + ' FROM DISK = '''
+ @backupPath +'\'+ @backupFile + ''' WITH NORECOVERY'
print @cmd
FETCH NEXT FROM backupFiles INTO @backupFile
END
CLOSE backupFiles
DEALLOCATE backupFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment