What is COPY_ONLY?


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.

First setup

-- Run an initial full, a COPY_ONLY full and a log backup
USE Test;
INSERT INTO LoadTable VALUES ('a','Yes');
INSERT INTO LoadTable VALUES ('c','No');

Now test

	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';

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!

4 thoughts on “What is COPY_ONLY?

  1. 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.

  2. […] 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 […]

  3. […] 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, […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,755 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: