I
f you were to buy a brand-new car, how long do you think it would continue to
run without any maintenance? It may last a few months, maybe even a year,
before it finally breaks down and quits functioning altogether. If you want to
keep your car running in top shape for years to come, you have to perform regu-
lar maintenance, such as changing the oil, rotating the tires, etc. SQL Server is no dif-
ferent; you must perform regular maintenance if you want to keep your server in top
running condition.
The first maintenance task we will explore is probably the most important: You must
perform regular backups. Without a backup strategy, you can—no, you will—lose data.
Therefore you will want to pay close attention as we discuss each of the four types of
backup (full, differential, transaction log, and filegroup) and how to use each one.
Another important topic that we will cover is how to read the SQL Server error logs
and what to do with the information you find there. SQL Server keeps its own error
logs apart from the Windows NT logs that you may be used to reading in Event
Viewer, so this section of the book will serve you well.
Finally we will delve into the depths of index maintenance. We created indexes in
Chapter 12; now we need to know how to keep them running by performing regular
maintenance on them. We’ll start by looking into backups.
Backing Up Your Data
A backup is a copy of your data that is stored somewhere other than the hard drive of
your computer, usually on some type of tape (a lot like the kind you listen to), but a
backup can also be stored on a hard drive on another computer connected over a
local area network. Why would you want to keep a copy of your data in two places?
There are many reasons.
The first reason for keeping a backup is hardware failure. Computer hardware has a
Mean Time Between Failures (MTBF) that is measured in hours. This means that every
4000 hours or so, a piece of hardware is going to fail, and there is little you can do
about it. True, you could implement fault tolerance by providing duplicate hardware,
but that is not a complete guarantee against data loss. So if you don’t want to lose
your data when a hard disk goes bad, it is best to back up.
Another reason that comes to mind is natural disaster. No matter how much
redundant hardware you have in place, it is not likely to survive the wrath of a tor-
nado, hurricane, earthquake, flood, or fire. To thwart the wrath of the elements, you
need to back up your data.
A final reason is provoked by all of the injustice we see in today’s world. Many
employees are angry with their boss or the company in general, and the only way
2627ch16.qxt 8/22/00 10:58 AM Page 574
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
575
they see to get revenge is by destroying or maliciously updating sensitive data. This is
the worst kind of data loss, and the only way to recover from it is by having a viable
backup.
Now that you have some very good reasons to back up your data, you need to
know how to do it. We’ll look into four different types of backup that you can per-
form to protect your data, but first you need to know how the backup process works.
How Backups Work
Some things are common to all types of backup. For instance, you may be wondering
when you are going to be able to get your users off the database long enough to per-
form a backup. Stop wondering—all backups in SQL Server are online backups, which
means that your users can access the database while you are backing it up. How is this
possible? Transaction logs make this possible.
In Chapter 3, you learned that SQL Server issues checkpoints on databases to copy
committed transactions from the transaction log to the database. The transaction log
is a lot like a diary; in a diary, you put a date next to everything that happens to you.
It might look as follows:
12-21-99 Bought a car
12-22-99 Drove new car to show off
12-23-99 Drove car into tree
12-24-99 Started looking for new car
Much like a diary, a transaction log also puts a log sequence number (LSN) next to
each line of the log. A transaction log would look as follows:
147 Begin Tran 1
148 Update Tran 1
149 Begin Tran 2
150 Update Tran 2
151 Commit Tran 1
152 Checkpoint
153 Update Tran 2
154 Commit Tran 2
When a backup is started, SQL Server records the current LSN. Then, once the
backup is complete, SQL Server backs up all of the entries in the transaction log from
the LSN it recorded at the start of the backup to the current LSN. Here’s an example of
BACKING UP YOUR DATA
Administering SQL
Server
PART
IV
2627ch16.qxt 8/22/00 10:58 AM Page 575
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 16 • BASIC ADMINISTRATIVE TASKS
576
how it works: First SQL Server checkpoints the data and records the LSN of the oldest
open transaction (in this case, 149 Begin Tran 2, because it was not committed before
the checkpoint). Next, SQL Server backs up all of the pages of the database that actu-
ally contain data (no need to back up the empty ones). Finally, SQL Server grabs all of
the parts of the transaction log that were recorded during the backup process—that is,
all of the lines of the transaction log with an LSN higher than the LSN recorded at the
start of the backup session (in this case, 149 and above). In this way your users can
still do whatever they want with the database while it is being backed up.
To perform any type of backup, though, you need a place to store it. The medium
that you will use to store a backup is called a backup device. Let’s see how to create
them now.
Creating a Backup Device
Backups are stored on a physical backup media, which can be a tape drive or a hard
disk (local or over a network connection). SQL Server is not aware of the various
forms of media attached to your server, so you must inform SQL Server where to store
the backups. That is what a backup device is for; it is a representation of the backup
media. There are two types of backup devices to create: permanent and temporary.
Temporary backup devices are created on the fly, when you perform the actual
backup. They are very useful for making a copy of a database to send to another office
so that they can have a complete copy of your data. Or you may want to consider
using a temporary backup device to make a copy of your database for permanent off-
site storage (usually for archiving).
NOTE Although it is true that you could use replication (discussed in Chapter 27) to
copy a database to a remote site, backing up to a temporary backup device may be faster
if your remote site is connected via a slow WAN link (such as 56K frame relay).
Permanent backup devices can be used over and over again, and you can even
append data to them, making them the perfect device for regularly scheduled backups.
Permanent backup devices are created before the backup is performed and, like tempo-
rary devices, can be created on a local hard disk, on a remote hard disk over a local area
network, or on a local tape drive. Let’s create a permanent backup device now:
1. Open Enterprise Manager by selecting it from the SQL Server 2000 group under
Programs on the Start menu and expand your server, then Management.
2. Click Backup in the contents pane.
2627ch16.qxt 8/22/00 10:58 AM Page 576
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
577
3. On the Action menu, select New Backup Device.
4. In the Name box of the Backup Device Properties dialog box, enter NwindFull.
Notice that the filename and path are filled in for you; make sure you have
enough free space on the drive that SQL Server has selected.
5. Click OK to create the device.
If you go to Windows Explorer and search for a file named NwindFull.bak right
now, don’t be too surprised if you don’t find one. SQL Server hasn’t created a file just
yet; it simply added a record to the sysdevices table in the master database telling SQL
Server where to create the backup file the first time you perform a backup to the
device. So don’t worry, it will be there as soon as you perform a backup. In fact, let’s
work with full backups right now.
TIP If you are using a tape drive as a backup medium, it must be physically attached to
the SQL Server machine. The only way around this is to use a third-party backup solution.
Performing a Full Backup
Just as the name implies, a full backup is a backup of the entire database. It backs up
the database files, the locations of those files, and portions of the transaction log
(from the LSN recorded at the start of the backup to the LSN at the end of the
backup). This is the first type of backup you need to perform in any backup strategy
because all of the other backup types depend on the existence of a full backup. This
means that you cannot perform a differential or transaction log backup if you have
BACKING UP YOUR DATA
Administering SQL
Server
PART
IV
2627ch16.qxt 8/22/00 10:58 AM Page 577
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 16 • BASIC ADMINISTRATIVE TASKS
578
never performed a full backup. To create your baseline (which is what the full backup
is called in a backup strategy), let’s back up the Northwind database to the permanent
backup device you created in the last section of this chapter:
1. Open Enterprise Manager and expand your server, then databases.
2. Right-click Northwind and select Properties.
3. On the Options tab, clear the Select Into/Bulk Copy and Truncate Log on
Checkpoint boxes so you can perform a transaction log backup later.
4. Click OK to apply the changes.
5. Select Northwind under Databases and on the Action menu, point to All Tasks
and select Backup Database.
6. In the Backup dialog box, make sure Northwind is the selected database to back
up and the name is Northwind Backup.
7. In the Description box, type Full Backup of Northwind.
8. Under Backup, select Database – Complete (this is the full backup).
9. Under Destination, click Add.
2627ch16.qxt 8/22/00 10:58 AM Page 578
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
579
10. In the Select Backup Destination box, click Backup Device, select NwindFull,
and click OK.
11. In the Backup dialog box, under Overwrite, select Overwrite Existing Media.
This will initialize a brand-new device or overwrite an existing one.
12. On the Options tab, select Verify Backup upon Completion; this will check the
actual database against the backup copy to see whether they match after the
backup is complete.
BACKING UP YOUR DATA
Administering SQL
Server
PART
IV
2627ch16.qxt 8/22/00 10:58 AM Page 579
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 16 • BASIC ADMINISTRATIVE TASKS
580
13. Click OK to start the backup.
You now have a full backup of the Northwind database. Let’s look inside the
NwindFull device to make sure that the backup is there:
1. In Enterprise Manager, expand Management and click Backup.
2. Right-click NwindFull and select Properties.
3. In the Properties dialog box, click View Contents.
4. In the View Backup Media Contents dialog box, you should see the full backup
of Northwind.
5. Click Close, then OK to get back to Enterprise Manager.
2627ch16.qxt 8/22/00 10:58 AM Page 580
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
581
Now that you have a full backup in place, you can start performing other types of
backups. Let’s look at differential backups now.
Performing Differential Backups
Differential backups are designed to record all of the changes made to a database since
the last full backup was performed. This means that if you perform a full backup on
Monday and then a differential backup on Tuesday, the differential would record all
of the changes to the database since the full backup on Monday. Another differential
backup on Wednesday would record all of the changes made since the full backup on
Monday. The differential backup gets a little bigger each time it is performed, but it is
still a great deal smaller than the full backup, which makes a differential faster than a
full backup.
SQL Server figures out which pages in the backup have changed by reading the last
LSN of the last full backup and comparing it with the data pages in the database. If
SQL Server finds any updated data pages, it will back up the entire extent (eight con-
tiguous pages) of data, rather than just the page that changed.
Performing a differential backup is almost the same process as that of a full backup.
Let’s perform a differential backup on the Northwind database to the permanent
backup device you created earlier:
1. Open Enterprise Manager and expand your server, then databases.
2. Select Northwind.
3. On the Action menu, point to All Tasks and select Backup Database.
BACKING UP YOUR DATA
Administering SQL
Server
PART
IV
2627ch16.qxt 8/22/00 10:58 AM Page 581
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 16 • BASIC ADMINISTRATIVE TASKS
582
4. In the Backup dialog box, make sure Northwind is the selected database to back
up and the name is Northwind Backup.
5. In the Description box, type Differential Backup of Northwind.
6. Under Backup, select Database – Differential.
7. Under Destination, click Add.
8. In the Backup Destination box, make sure NwindFull is listed; if not, click
Backup Device, select NwindFull, and click OK.
9. Under Overwrite, select Append to Media so that you do not overwrite your
existing full backup.
10. On the Options tab, select Verify Backup upon Completion.
11. Click OK to start the backup.
Now you need to verify that the differential and full backups are on the NwindFull
device where they should be:
1. In Enterprise Manager, expand Management and click Backup.
2. Right-click NwindFull and select Properties.
3. In the Properties dialog box, click View Contents.
2627ch16.qxt 8/22/00 10:58 AM Page 582
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
583
4. In the View Backup Media Contents dialog box, you should see the differential
backup of Northwind.
5. Click Close, then OK to get back to Enterprise Manager.
Performing just full and differential backups is not enough, though; if you don’t
perform transaction log backups, your database could stop functioning, so it is impor-
tant to understand them.
Performing Transaction Log Backups
Although they still rely on the existence of a full backup, transaction log backups don’t
actually back up the database itself. This type of backup only records sections of the
transaction log, specifically since the last transaction log backup. It is easier to under-
stand the role of the transaction log backup if you think of the transaction log the
way SQL Server does, as a separate object. If you think of the transaction log as a sepa-
rate object, it makes sense that SQL Server would require a backup of the database as
well as the log.
Besides the fact that a transaction log is an entity unto itself, there is another rea-
son to back them up—a very important one. This type of backup is the only type that
will clear old transactions out of the transaction log; neither full nor differential will
do this. Therefore, if you were to perform only full and differential backups, the trans-
action log would eventually fill to 100% capacity, and your users would be locked out
of the database.
WARNING When a transaction log becomes 100% full, users are denied access to
the database until an administrator clears the transaction log. The best way around this is
to perform regular transaction log backups.
BACKING UP YOUR DATA
Administering SQL
Server
PART
IV
2627ch16.qxt 8/22/00 10:58 AM Page 583
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Không có nhận xét nào:
Đăng nhận xét