What is COPY_ONLY?4
January 16, 2017 by Kenneth Fisher
You’ll frequently hear that you should add the COPY_ONLY clause when taking an ad-hoc full backup to avoid messing up the backup chain. Just in case you haven’t: You should add the COPY_ONLY clause when taking an ad-hoc full backup to avoid messing up the backup chain.
But you may be asking why? What exactly does COPY_ONLY do?
Let’s start with the BOL definition.
A copy-only backup is a SQL Server backup that is independent of the sequence of conventional SQL Server backups. Usually, taking a backup changes the database and affects how later backups are restored. However, occasionally, it is useful to take a backup for a special purpose without affecting the overall backup and restore procedures for the database. Copy-only backups serve this purpose.
And an exerpt from the BOL definition of the BACKUP DATABASE command.
When used with BACKUP DATABASE, the COPY_ONLY option creates a full backup that cannot serve as a differential base. The differential bitmap is not updated, and differential backups behave as if the copy-only backup does not exist. Subsequent differential backups use the most recent conventional full backup as their base.
Let me make a quick note that I’m going to concentrate on using COPY_ONLY with FULL backups although it can also be used with LOG backups.
So if we look at that second excerpt we get some interesting information:
- COPY_ONLY FULL backups can not be used as the base to restore differentials.
Ie we can’t restore a COPY_ONLY full backup, then restore a differential on top of it. It doesn’t say anything about logs, though, so that should be possible, but let’s test that in a minute.
- The differential bitmap is not updated.
Which begs the question “What’s the differential bitmap?” Well, simplifying a bit, it’s what tells SQL what data needs to be copied into a differential backup.
So to put that in simple terms. I have a database, Test. I take a full backup, changes happen, I take a differential backup, changes happen, I take a differential backup, etc. Ignoring all of the log backups that are happening if the database is in FULL recovery of course.
Currently, both differentials contain everything that has happened between the time of the full backup and the time the differential was taken. But what happens if I take a second full backup between the first and second differential? Now, that second differential will only contain data between the second full backup and the differential.
Sounds normal right? But why do we care? Well, let’s say that that second full backup was not part of our regular backup cycle. Let’s further say that this ad-hoc backup was put in a different location than the regular backups, and then deleted once it was no longer needed. What about that second differential? Or the next one? Now they aren’t completely useless (we will get into that in a future post, soon I promise) but restores just got a whole lot more complicated. We can’t restore that first full backup and then the second differential any more. And if that’s what’s expected, what we have scripted, then it could cause a real headache if we actually have a disaster.
So to avoid that we use COPY_ONLY on the second full backup. Now it has no effect on the differentials and our original logic holds. The second differential has all of the information between when it was taken and the first full. Our planning, and scripts, are still in effect. Making a disaster, maybe not less of a disaster, but hopefully not more.
And again, I’m going to point out, because this is important, it has no effect whatsoever on the log backups.
Ok, so we know from earlier that a COPY_ONLY full backup can’t have a differential applied to it, but what about a log? Well, let’s give it a shot.
-- Run an initial full, a COPY_ONLY full and a log backup BACKUP DATABASE Test TO DISK = 'C:\backups\Test.BAK' WITH COMPRESSION, INIT; GO USE Test; GO INSERT INTO LoadTable VALUES ('a','Yes'); GO BACKUP DATABASE Test TO DISK = 'C:\backups\Test_CO.BAK' WITH COMPRESSION, COPY_ONLY; GO INSERT INTO LoadTable VALUES ('c','No'); GO BACKUP LOG Test TO DISK = 'C:\backups\Test.TRN' WITH COMPRESSION; GO
RESTORE DATABASE Test_CO FROM DISK = 'C:\backups\Test_CO.BAK' WITH NORECOVERY, MOVE 'Test' TO 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016CS\MSSQL\DATA\Test_CO.mdf', MOVE 'Test_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016CS\MSSQL\DATA\Test_CO_log.ldf'; RESTORE LOG Test_CO FROM DISK = 'C:\backups\Test.TRN' WITH RECOVERY;
And it worked!
So a COPY_ONLY full backup can be used as the base for a log backup but not a differential. Good to know!
Category: Backups, Microsoft SQL Server, SQLServerPedia Syndication | Tags: backups, best practices, microsoft sql server
4 thoughts on “What is COPY_ONLY?”
Leave a Reply to Be warned: The way I’m feeling right now I might actually give you sysadmin when you ask for it. | SQL Studies Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
And if you use DBCC TRACEON(3004, 3605) before the backup, you can see it clearing the diff bitmap for a regular backup and that it’s skipped for a copy_only. That’s only if you wanted proof of the differences.
Very cool. I wasn’t aware of 3004.
[…] to (_CO.BAK). This is is because this is for ad-hoc backups and they will almost always be copy only so I want a CO extension on the end. I just need to type in the path (that I’ll get from the […]
[…] hitch though. It says you took a backup the day before yesterday and you forgot to use copy_only. You don’t know what that is? No problem, I’ll show you once we get this fixed. Now, […]