1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu MASTERING SQL SERVER 2000- P12 ppt

50 287 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Basic Administrative Tasks in SQL Server
Trường học University of the People
Chuyên ngành Database Administration
Thể loại Phần đầu tài liệu hướng dẫn quản trị SQL Server 2000
Năm xuất bản 2000
Thành phố Unknown
Định dạng
Số trang 50
Dung lượng 1,44 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 ofbackup full, differential, transaction log

Trang 2

• Automate administration

• Provide security in SQL Server 2000

Trang 4

CHAPTER 16

Basic Administrative Tasks

Trang 5

If 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 tokeep 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 toprunning condition

The first maintenance task we will explore is probably the most important: You mustperform 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 ofbackup (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 logsand what to do with the information you find there SQL Server keeps its own errorlogs apart from the Windows NT logs that you may be used to reading in EventViewer, so this section of the book will serve you well

Finally we will delve into the depths of index maintenance We created indexes inChapter 12; now we need to know how to keep them running by performing regularmaintenance 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 abackup can also be stored on a hard drive on another computer connected over alocal 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 aMean 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 doabout 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 loseyour 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 muchredundant 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, youneed to back up your data

A final reason is provoked by all of the injustice we see in today’s world Manyemployees are angry with their boss or the company in general, and the only way

Trang 6

they see to get revenge is by destroying or maliciously updating sensitive data This isthe worst kind of data loss, and the only way to recover from it is by having a viablebackup.

Now that you have some very good reasons to back up your data, you need toknow 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 wonderingwhen 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 thispossible? Transaction logs make this possible

In Chapter 3, you learned that SQL Server issues checkpoints on databases to copycommitted 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 car12-22-99 Drove new car to show off12-23-99 Drove car into tree12-24-99 Started looking for new carMuch like a diary, a transaction log also puts a log sequence number (LSN) next toeach line of the log A transaction log would look as follows:

Trang 7

how it works: First SQL Server checkpoints the data and records the LSN of the oldestopen transaction (in this case, 149 Begin Tran 2, because it was not committed beforethe 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 ofthe 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 thestart of the backup session (in this case, 149 and above) In this way your users canstill 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 harddisk (local or over a network connection) SQL Server is not aware of the variousforms of media attached to your server, so you must inform SQL Server where to storethe backups That is what a backup device is for; it is a representation of the backupmedia There are two types of backup devices to create: permanent and temporary.Temporary backup devices are created on the fly, when you perform the actualbackup 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 considerusing 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) tocopy 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 evenappend 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 areanetwork, 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.

Trang 8

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 haveenough 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.bakrightnow, don’t be too surprised if you don’t find one SQL Server hasn’t created a file justyet; it simply added a record to the sysdevices table in the master database telling SQLServer where to create the backup file the first time you perform a backup to thedevice So don’t worry, it will be there as soon as you perform a backup In fact, let’swork with full backups right now

TIP If you are using a tape drive as a backup medium, it must be physically attached tothe 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 thebackup) This is the first type of backup you need to perform in any backup strategybecause all of the other backup types depend on the existence of a full backup Thismeans that you cannot perform a differential or transaction log backup if you have

Trang 9

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

Trang 10

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 thebackup is complete

Trang 11

13 Click OK to start the backup.

You now have a full backup of the Northwind database Let’s look inside theNwindFull 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.

Trang 12

Now that you have a full backup in place, you can start performing other types ofbackups 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 onMonday 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 differentialbackup on Wednesday would record all of the changes made since the full backup onMonday The differential backup gets a little bigger each time it is performed, but it isstill a great deal smaller than the full backup, which makes a differential faster than afull backup

SQL Server figures out which pages in the backup have changed by reading the lastLSN of the last full backup and comparing it with the data pages in the database IfSQL 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 permanentbackup device you created earlier:

1 Open Enterprise Manager and expand your server, then databases.

Trang 13

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

Trang 14

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’tperform 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 thetransaction 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 theway 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 aswell as the log

Besides the fact that a transaction log is an entity unto itself, there is another son to back them up—a very important one This type of backup is the only type thatwill clear old transactions out of the transaction log; neither full nor differential will

rea-do this Therefore, if you were to perform only full and differential backups, the action log would eventually fill to 100% capacity, and your users would be locked out

Trang 15

There are not a lot of steps to performing a transaction log backup, so let’s gothrough them In this section, you are going to perform a transaction log backup onthe Northwind database using the backup device created earlier in this chapter:

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.

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 Transaction Log Backup of Northwind.

6 Under Backup, select Transaction Log.

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 Also on the Options tab, make certain that the Remove Inactive Entries box is

checked This will remove completed transactions from the log, allowing SQLServer to use that space

Trang 16

12 Click OK to start the backup.

It is again prudent for you to manually verify that you did not accidentally write the full and differential backups that were stored in your backup device:

over-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 transaction

log backup of Northwind

5 Click Close, then OK to get back to Enterprise Manager.

Full, differential, and transaction log backups are great for small to large databases,but there is a type of backup specially designed for very large databases that are usu-ally terabytes in size Let’s look into filegroup backups to see how they can be used insuch a scenario

Performing Filegroup Backups

A growing number of companies out there today have databases that are reaching theterabyte range With good reason, these are known as very large databases (VLDBs)

Imagine trying to perform a backup of a 2TB database on a nightly, or even weekly,basis Even if you have purchased the latest, greatest hardware, you are looking at avery long backup time Microsoft figured out that you don’t want to wait that longfor a backup to finish, so they gave you a way to back up small sections of the data-

base at a time—a filegroup backup.

Trang 17

We discussed filegroups in Chapters 3 and 10, so we won’t rehash much detail

here A filegroup is a way of storing a database on more than one file, and it grants you

the ability to control on which of those files your objects (such as tables or indexes)are stored This way a database is not limited to being contained on one hard disk; itcan be spread out across many hard disks and thus can grow quite large Using a file-group backup, you can back up one or more of those files at a time rather than theentire database all at once

There is, however, a caveat to be aware of when using filegroup backups to acceleratethe backup process for VLDBs Filegroups can also be used to expedite data access byplacing tables on one file and the corresponding indexes on another file Although thissteps up data access, it can slow the backup process because you must back up tablesand indexes as a single unit, as shown in Figure 16.1 This means that if the tables andindexes are stored on separate files, the files must be backed up as a single unit; you can-not back up the tables one night and the associated indexes the next

To perform a filegroup backup, you need to create a filegroup Let’s add a file to thesales database that you created earlier:

1 Open Enterprise Manager and expand your server, then databases.

2 Right-click the sales database and select Properties.

Table 1 Index 1

Table 2 Index 2

File 1Backup Monday

File 2Backup Tuesday

Table 1 Table 2

Index 1 Index 2

File 1Backup Monday

File 2Backup Monday

Trang 18

3 On the General tab, under File Name, add a file named Sales_Data_2 with a size

of 5MB

4 In the Filegroup column, enter Secondary to create a new filegroup.

5 Click OK to create the second file.

Now you need to add a table to that filegroup and create a record in it so that youwill be able to test the restore process later in this chapter:

1 In Enterprise Manager, expand the sales database and click the Tables icon.

2 On the Action pull-down menu, select Create New Table.

3 Under Column Name in the first row, enter Emp_Name.

4 Next to Emp_Name, select varchar as the datatype with a length of 20.

5 Just below Emp_Name in the second row, type Emp_Number as the column

name with a type of varchar and a length of 10

Trang 19

6 Click the Table and Index Properties button on the toolbar (it looks like a hand

pointing at a table)

7 Change the Table Filegroup and Text Filegroup to Secondary and click Close.

Trang 20

8 Click the Save button to create the new table (it looks like a floppy disk on the

toolbar) and enter Employees for the table name.

9 Close the table designer by clicking the small X button in the top-right corner of

the window

Now you need to add some data to the new table so that you will have something

to restore from the backup you are about to make:

1 Select Query Analyzer from the Tools menu in Enterprise Manager.

2 To add records to the employees table, enter and execute the following code

(note that the second value is arbitrary):

USE SalesINSERT EmployeesVALUES(‘Bob Smith’, ‘VA1765FR’)INSERT Employees

VALUES(‘Andrea Jones’, ‘FQ9187GL’)

3 Close Query Analyzer.

With a second filegroup in place that contains data, you can perform a filegroupbackup:

1 Under Databases, select Sales.

2 On the Action menu, point to All Tasks and select Backup Database.

3 In the Backup dialog box, make sure sales is the selected database to back up and

the name is Sales Backup

4 In the Description box, type Filegroup Backup of Sales.

5 Under Backup, select File and Filegroup.

6 Click the ellipsis button (…), check the box next to Secondary, and click OK.

Trang 21

7 Under Destination, click Add.

8 In the Backup Destination box, click Backup Device, select NwindFull, and

click OK

9 Under Overwrite, select Append to Media so that you do not overwrite your

existing backups

10 On the Options tab, select Verify Backup upon Completion.

11 Click OK to start the backup.

Now that you have backed up a single file of the sales database, let’s verify that itmade it to the backup device:

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 filegroup

backup of sales

Trang 22

5 Click Close, then OK to get back to Enterprise Manager.

NOTE We could have backed up the Sales database to another backup device namedSales, but for simplicity’s sake, we backed it up to an existing device so that the exercisewould move along faster

That takes care of the mechanics of all four types of backup Now let’s look at atechnique to make the backups even faster—parallel striping

Performing Parallel Striped Backups

Thus far you’ve seen how to perform backups to a single backup device If you reallywant to speed things up, you can perform backups to multiple devices at the same

time This type of backup is called a parallel striped backup and can be performed on

the hard disk, network, or local tape drive, just like a normal backup

NOTE If you want to do this with tape devices, you need more than one local tapedrive in the SQL Server machine

Trang 23

This type of backup is called a parallel backup because multiple devices are used inparallel It is called a striped backup because of the way that the data is stored on themedium You may expect that one device will be filled to capacity and then you move

on to the next, but that is not what happens The data is “striped” across all of themedia at the same time, which means that all of the devices are written to at once;that is what makes a parallel striped backup faster than using a single device

There is just one, small drawback: Once you combine backup devices, they cannot

be used separately As shown in Figure 16.2, this means that if you back up wind to three devices (BD1, BD2, and BD3), you cannot back up another database tojust BD3; you would have to use all three devices for the backup All three devices arenow considered part of a media set and cannot be used separately without losing all

North-of the backups stored on the set

FIGURE 16.2

The backup devices in

a media set cannot be used for individual backups.

NOTE You can separate the files in a media set by formatting the files in the media set,but by doing so you render the entire media set useless—you should format all of thedevices in the set

Trang 24

To perform a parallel striped backup, you need to create two more backup devicesand then perform a backup Let’s do that now:

1 Open Enterprise Manager and expand your server, then Management.

2 Click Backup in the contents pane.

3 On the Action menu, select New Backup Device.

4 In the Name box of the Backup Device Properties dialog box, enter PSDev1.

Notice that the filename and path are filled in for you; make sure you haveenough free space on the drive that SQL Server has selected

5 Click OK to create the device.

6 On the Action menu, select New Backup Device.

7 In the Name box of the Backup Device Properties dialog box, enter PSDev2.

Notice that the filename and path are filled in for you; make sure you haveenough free space on the drive that SQL Server has selected

8 Click OK to create the device.

Now that you have multiple devices, you can perform a parallel striped backup Inthis instance, you are going to perform a full backup of the pubs database:

1 Open Enterprise Manager and expand your server, then databases.

2 Select the pubs database under databases.

3 On the Action menu, point to All Tasks and select Backup Database.

4 In the Backup dialog box, make sure pubs is the selected database to back up

and the name is Pubs Backup

5 In the Description box, type Full Backup of Pubs.

6 Under Backup, select Database – Complete (this is the full backup).

7 Under Destination, click Add.

8 In the Backup Destination box, click Backup Device, select PSDev1, and click OK.

9 Under Destination, click Add.

10 In the Backup Destination box, click Backup Device, select PSDev2, and click OK.

11 Under Overwrite, select Overwrite Existing Media because this is the first time

you are writing to this device

Trang 25

12 On the Options tab, select Verify Backup upon Completion.

13 Click OK to start the backup.

Now you are ready to verify that the backup is on the two devices that you specified:

1 In Enterprise Manager, expand Management and click Backup.

2 Right-click PSDev1 or PSDev2 (it doesn’t matter which) 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 pubs

Ngày đăng: 21/01/2014, 08:20