Part I of II (had to split my answer)
There are one or two things to think about here. In my opinion you may have taken a shortcut in the thought process. I'll explain while I'm making this assumption...
You might want to have a quick look at my accepted answer here which was posted in reference to the question Need suggestion on Back up Strategy [closed] to give you some ideas about RTO and RPO.
Why? Because setting @CleanupTime=0
might be a bad idea...
First to answer your question.
Is it a bug in Ola's script? - No. Ola designed his script to check for the last BACKUP DATABASE...
(Full Backup) and then to delete the transaction logs backups (BACKUP LOG ...
) which have occurred before that backup and only if the @CleanupTime
is lower than when the FULL Backup occurred.
This is a fail-safe mechanism and not meant to be used as a general feature.
Works as designed, because a BACKUP DATABASE ... WITH COPY_ONLY...
is still a Full Backup.
Safety first
Creating a backup with the BACKUP DATABASE ... COPY_ONLY...
option and then creating BACKUP LOG...
will still permit you to restore the database to a consistent state using the FULL COPY_ONLY
backup and the available Transaction Log backups.
I tested this using my StackExchange database:
- Creating a
COPY_ONLY
backup manually
- Modifying some data
- Performing a
BACKUP LOG ...
using Ola's scripts
- Restoring the database
Manual backup with COPY_ONLY
-------------------------------------------------
BACKUP DATABASE [StackExchange] TO DISK = 'C:\adhoc\StackExchange_Full_CopyOnly_20171221_082242.bak'
WITH COPY_ONLY, COMPRESSION, RETAINDAYS = 3, NOFORMAT, NOINIT, NAME = N'StackExchange-Full Backup Sichern', SKIP, NOREWIND,
NOUNLOAD, STATS = 10, CHECKSUM
-------------------------------------------------
10 percent processed.
21 percent processed.
31 percent processed.
40 percent processed.
50 percent processed.
61 percent processed.
70 percent processed.
80 percent processed.
91 percent processed.
Processed 376 pages for database 'StackExchange', file 'StackExchange' on file 1.
100 percent processed.
Processed 2 pages for database 'StackExchange', file 'StackExchange_log' on file 1.
BACKUP DATABASE successfully processed 378 pages in 0.027 seconds (109.103 MB/sec).
-------------------------------------------------
Finished
Backup of the Transaction Log with Ola's scripts
Date 21.12.2017 08:29:06
Log Job History (OLA DatabaseBackup - USER_DATABASES - LOG)
Step ID 1
Server MYNOTEBOOK
Job Name OLA DatabaseBackup - USER_DATABASES - LOG
Step Name OLA DatabaseBackup - USER_DATABASES - LOG
Duration 00:00:01
Sql Severity 0
Sql Message ID 0
Operator Emailed
Operator Net sent
Operator Paged
Retries Attempted 0
Message
Executed as user: NT Service\SQLSERVERAGENT. ...557.0 Edition: Developer Edition (64-bit) Procedure: [msdb].[dbo].[DatabaseBackup] Parameters: @Databases = 'USER_DATABASES', @Directory = 'C:\SQL\Backup', @BackupType = 'LOG', @Verify = 'Y', @CleanupTime = 48, @CleanupMode = 'AFTER_BACKUP', @Compress = NULL, @CopyOnly = 'N', @ChangeBackupType = 'N', @BackupSoftware = NULL, @CheckSum = 'Y', @BlockSize = NULL, @BufferCount = NULL, @MaxTransferSize = NULL, @NumberOfFiles = NULL, @CompressionLevel = NULL, @Description = NULL, @Threads = NULL, @Throttle = NULL, @Encrypt = 'N', @EncryptionAlgorithm = NULL, @ServerCertificate = NULL, @ServerAsymmetricKey = NULL, @EncryptionKey = NULL, @ReadWriteFileGroups = 'N', @OverrideBackupPreference = 'N', @NoRecovery = 'N', @URL = NULL, @Credential = NULL, @MirrorDirectory = NULL, @MirrorCleanupTime = NULL, @MirrorCleanupMode = 'AFTER_BACKUP', @AvailabilityGroups = NULL, @Updateability = 'ALL', @LogToTable = 'Y', @Execute = 'Y' Source: https://ola.hallengren.com Date and time: 2017-12-21 08:29:06 Database: [AdminDB2] Status: ONLINE Standby: No Updateability: READ_WRITE User access: MULTI_USER Is accessible: Yes Recovery model: FULL Encrypted: No Differential base LSN: 36000001056400037 Last log backup LSN: 36000001061600001 Date and time: 2017-12-21 08:29:06 Command: DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_create_subdir N'C:\SQL\Backup\MYNOTEBOOK\AdminDB2\LOG' IF @ReturnCode 0 RAISERROR('Error creating directory.', 16, 1) Outcome: Succeeded Duration: 00:00:00 Date and time: 2017-12-21 08:29:06 Date and time: 2017-12-21 08:29:06 Command:
[shortened]
... Process Exit Code 0. The step succeeded.
Restore
USE [master]
-- Restore the COPY_ONLY Backup
RESTORE DATABASE [StackExchange] FROM DISK = N'C:\adhoc\StackExchange_Full_CopyOnly_20171221_082242.bak'
WITH REPLACE, FILE = 1, NORECOVERY, NOUNLOAD, STATS = 5
-- Restore an OLA Transaction Log backup
RESTORE LOG [StackExchange] FROM DISK = N'C:\SQL\Backup\MYNOTEBOOK\StackExchange\LOG\NB31710_StackExchange_LOG_20171221_082906.trn'
WITH FILE = 1, NOUNLOAD, STATS = 5
You may not the difference in the timestamps is approx. 7 minutes.
Results
6 percent processed.
10 percent processed.
16 percent processed.
21 percent processed.
25 percent processed.
31 percent processed.
36 percent processed.
40 percent processed.
46 percent processed.
50 percent processed.
55 percent processed.
61 percent processed.
65 percent processed.
70 percent processed.
76 percent processed.
80 percent processed.
86 percent processed.
91 percent processed.
95 percent processed.
100 percent processed.
Processed 376 pages for database 'StackExchange', file 'StackExchange' on file 1.
Processed 2 pages for database 'StackExchange', file 'StackExchange_log' on file 1.
RESTORE DATABASE successfully processed 378 pages in 0.021 seconds (140.276 MB/sec).
100 percent processed.
Processed 0 pages for database 'StackExchange', file 'StackExchange' on file 1.
Processed 2 pages for database 'StackExchange', file 'StackExchange_log' on file 1.
RESTORE LOG successfully processed 2 pages in 0.005 seconds (2.441 MB/sec).
Summary
Ola's script works as designed. We have verified that a BACKUP DATABASE ... WITH COPY_ONLY...
and an additional BACKUP LOG ...
will work together. The fail-safe feature ensures you can restore your database with the last full backup (be it COPY_ONLY or not) and the available Transaction Log backups.
(Please read Part II of II for more answers)