Logbackup sequence to restore and automatically to move the files

S_NO 21 Reputation points
2023-03-04T16:39:33.8466667+00:00

Hi ALL,

I would like to have a restore sequence commands to generate WITH NORECOVERY

exi- restore log <dbname> from disk='f:\backuo\a.trn' with norecovery,stats=10

because Logbackup configured to every 15mins,Make a note I have taken mannual full backup at 8AM for the database and in b/w there is another schedule full backup runs at 5PM ..so need it like - complete restore sequence script since from 8AM ->logbackup restore sequence->5PM full backup->log backup restore sequence

is there any script exists.

ALso

I want to automate all the log backup files to move from one server to another server apart from logshipping.

Like xcopy- this should be like shouldnt copy the duplicate and sequence to be copy properly.

any help much appreciated!

SQL Server | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,586 Reputation points
    2023-03-06T07:59:28.22+00:00

    Hi @S_NO ,

    To restore a full backup followed by multiple log backups, you can use the following sequence of commands:

    RESTORE DATABASE <dbname> FROM DISK='<path to full backup file>' WITH NORECOVERY;
    RESTORE LOG <dbname> FROM DISK='<path to first log backup file>' WITH NORECOVERY, STATS=10;
    RESTORE LOG <dbname> FROM DISK='<path to second log backup file>' WITH NORECOVERY, STATS=10;
    ...
    RESTORE LOG <dbname> FROM DISK='<path to last log backup file>' WITH RECOVERY, STATS=10;
    

    Make sure to specify WITH NORECOVERY for all log backups except for the last one, which should be followed by WITH RECOVERY to bring the database online.

    To copy log backup files from one server to another server while avoiding duplicates and maintaining the correct sequence, you can use robocopy or PowerShell script to automate the process. Here is an example of PowerShell:

    $sourcePath = "<path to source folder>"
    $destinationPath = "<path to destination folder>"
    
    # Get a list of all log backup files in the source folder
    $logFiles = Get-ChildItem $sourcePath -Filter "*.trn" | Sort-Object LastWriteTime
    
    # Check if the destination folder exists; create it if it doesn't
    if (!(Test-Path $destinationPath)) {
        New-Item -ItemType Directory -Path $destinationPath
    }
    
    # Copy each log backup file to the destination folder
    foreach ($logFile in $logFiles) {
        $destinationFile = Join-Path $destinationPath $logFile.Name
        if (!(Test-Path $destinationFile)) {
            Copy-Item $logFile.FullName $destinationFile
        }
    }
    
    

    Best regards,

    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.