Sql-server – How to schedule a weekly backup of a database only if it has been changed since last backup

backupsql serversql-server-2008-r2stored-procedures

I´m new to SQL Server administration.

I´d like to create automatically backup database every Sunday night, but only if there was any change in previous week (if not, backup is unnecessary).

Is there some way how to do that only with SQL Server, like some automatic procedure or something like that?

Best Answer

Welcome to MS SQL :-)

To start with your question:

Yes there is. First you make a full database backup:

BACKUP DATABASE yourdatabasename
TO DISK = "pathtobackupdir\backupfilename.bak"

Now from that moment on a so called differential bitmap is keeping track of all pages changed.

From that moment every time you create a DIFFERENTIAL backup, only the pages that changed since your last FULL backup will be backupped.

 BACKUP DATABASE yourdatabasename
    TO DISK = "pathtobackupdir\backupfilename.bak" WITH DIFFERENTIAL

So every sunday, asuming you had changes every week, the diff backup will be bigger then the previous one. Untill one sunday, it's just as big as a full backup would be. That would imply that all data pages had changed. At that moment you want to do a new full backup. Since that will reset the differential bitmap and your next diff backup will be small again. (depending on the amount of changes)

You could automate the process of making diff backups untill a certain percentage of the data has changed. Have a look at this blog post: http://www.sqlskills.com/BLOGS/PAUL/post/New-script-How-much-of-the-database-has-changed-since-the-last-full-backup.aspx It explains a way on how to programatically look up the percentage of database changes. That way you could say.. make diff backups if percentage changed < 75% otherwise make a full backup.

Now could you tell me why you want this? Because this isn't a setup I would advise a starter (with exceptions). So please elaborate a bit and we can see if this is the best solution for your setup. How big is your database? how much changes do you have? is batch based changes? Which version and Edition of SQL do you have do you have?