Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,44 MB
Nội dung
This page intentionally left blank
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Administering
SQL Server
PART
IV
LEARN TO:
• Perform basic
administrative tasks
• Automate administration
• Provide security in
SQL Server 2000
2627ch16.qxt 8/22/00 10:58 AM Page 571
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
This page intentionally left blank
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 16
Basic
Administrative
Tasks
FEATURING:
Backing Up Your Data 574
Restoring Databases 596
Devising a Backup Strategy 604
Maintaining Indexes 608
Reading the Logs 613
Copying Databases 614
Summary 620
2627ch16.qxt 8/22/00 10:58 AM Page 573
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
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. SQLServer 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 SQLServer error logs
and what to do with the information you find there. SQLServer 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 SQLServer 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 SQLServer 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, SQLServer records the current LSN. Then, once the
backup is complete, SQLServer 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 SQLServer 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, SQLServer backs up all of the pages of the database that actu-
ally contain data (no need to back up the empty ones). Finally, SQLServer 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). SQLServer is not aware of the various
forms of media attached to your server, so you must inform SQLServer 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 SQLServer 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 SQLServer 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. SQLServer 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 SQLServer 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.
[...]... it to another location Finally, before SQLServer will allow you to restore a database, SQLServer performs a safety check to make sure you are not accidentally restoring the wrong database The first thing SQLServer does is compare the database name that is being restored with the name of the database recorded in the backup device If the two are different, SQLServer will not perform the restore This... let’s make one of the databases suspect so that you can see exactly what SQLServer does to restore it Specifically, let’s blow away Northwind: 1 Open the SQLServer Service Manager from the Start menu 2 Select the MSSQLServer service and click the Stop button (the red square) 3 You will be asked whether you wish to stop the MSSQLServer service and then whether you wish to stop all dependent services... 4 Find the filename northwnd.mdf (usually in C:\program files\microsoft sql server\ mssql\data\) 5 Rename the file northwnd.old 6 Find the file named northwnd.ldf and rename it northwndlog.old 7 From the Service Manager, restart the SQLServerAgent and MSSQLServer services 8 Open Enterprise Manager and expand databases under your server name Northwind should be gray and marked Suspect Please purchase... when restoring the differential backup, SQLServer will not allow you to restore any other backups; you have told SQLServer in effect that you are done restoring, so let everyone start using the database again If you have more than one file to restore, you need to specify NORECOVERY on all restores except the last one Another feature to point out is that SQLServer remembers where the original files... remove this watermark 597 PA R T IV Administering SQLServer 2627ch16.qxt 2627ch16.qxt 598 8/22/00 10:58 AM Page 598 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS NOTE You needed to stop all of the SQLServer services because while they are running, all of the databases are considered open files, and you would not be able to work with them outside of SQL Server Now that you have a suspect database on your... partial restores work: 1 Open Query Analyzer by selecting it from the SQL Server 2000 group under Programs on the Start menu 2 Enter and execute the following code to restore the sales database to a new database named sales_part: RESTORE DATABASE sales_part1 FILEGROUP = ‘secondary’ FROM DISK=’F:\Program Files\Microsoft SQL Server\ MSSQL\BACKUP\NwindFull.BAK’ WITH FILE=6,RECOVERY,PARTIAL, MOVE ‘sales_data’... This means that if you backed up files from the D drive, SQL Server will restore them to the D drive This is great unless your D drive has completely failed and you need to move your database to the E drive You will also run into this problem if you have backed up a database on a server at the home office and need to restore the database to a server at a child office In this instance, you need to use... the last transaction log backup It is easier to understand the role of the transaction log backup if you think of the transaction log the way SQLServer does, as a separate object If you think of the transaction log as a separate object, it makes sense that SQLServer 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... and select Restore Database 3 Notice here that SQLServer remembers where you backed everything up Make sure that all of the backups are checked 4 On the Options tab, select Leave Database Operational Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 8/22/00 10:58 AM Page 599 RESTORING DATABASES 599 PA R T IV Administering SQLServer 2627ch16.qxt 5 Click OK to perform the... performed, but it is still a great deal smaller than the full backup, which makes a differential faster than a full backup SQLServer 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 SQLServer finds any updated data pages, it will back up the entire extent (eight contiguous pages) of data, rather than . 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. 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