As I starting working with my log-shipping job, I realized that I needed a second table. One to stage all my powershell loaded filenames and one that I could use to build the restores. I also wanted to be able to truncate the Logshipping Table to empty it before loading the filenames so I won’t miss any, but not have to redo work in my main table that had already been done. At the beginning of my SQL Agent job, I have the step that Truncates the Logshipping Table. Then I load the filenames with the powershell script, I update the created dates and then I load it all into my new RestoreFile Table.
CREATE TABLE [dbo].[RestoreFile]( [Id] [int] IDENTITY(1,1) NOT NULL, [FileName] [varchar](500) NULL, [FileLocation] [varchar](1000) NULL, [FileType] [char](1) NULL, [Origin] [varchar](50) NULL, [ForDatabase] [varchar](50) NULL, [CreatedDate] [datetime2](7) NULL, [IsApplied] [bit] NOT NULL, [IsFinalLogFile] [bit] NOT NULL, CONSTRAINT [PK_RestoreFile] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[RestoreFile] ADD CONSTRAINT [DF_RestoreFile_IsApplied] DEFAULT ((0)) FOR [IsApplied] GO ALTER TABLE [dbo].[RestoreFile] ADD CONSTRAINT [DF_RestoreFile_IsFinalLogFile] DEFAULT ((0)) FOR [IsFinalLogFile] GO
I removed the IsApplied column from the Logshipping table because I didn’t need it. The IsFinalLogFile becomes super important when we go to bring the database into Standby mode.
Next, I need to move the data from Logshipping into RestoreFile:
INSERT INTO RestoreFile(FileName, FileLocation, FileType, Origin, ForDatabase, CreatedDate) SELECT A.FileName, A.FileLocation, A.FileType, A.Origin, A.ForDatabase, A.CreatedDate FROM [dbo].[LogshippingFile] A LEFT JOIN [dbo].[RestoreFile] B ON A.FileName = B.FileName WHERE B.FileName IS NULL
I am comparing the filenames so that I don’t get duplicates. Thanks again to the date stamping in the names, I won’t get the same name twice, but I run this load frequently during the day and don’t want to bloat my table.
Now, we have all the pieces in place to do our restores! Get ready for some fun!
The song for this post is Vance Joy’s Missing Piece