MASTERING SQL SERVER 2000- P12

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
42
lượt xem
4
download

MASTERING SQL SERVER 2000- P12

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'mastering sql server 2000- p12', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: MASTERING SQL SERVER 2000- P12

  1. This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  2. PA R T IV Administering SQL Server LEARN TO: • Perform basic administrative tasks • Automate administration • Provide security in SQL Server 2000
  3. This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  4. CHAPTER 16 Basic Administrative Tasks F E AT U R I N G : 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
  5. 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
  6. BACKING UP YOUR DATA 575 they see to get revenge is by destroying or maliciously updating sensitive data. This is PA R T the worst kind of data loss, and the only way to recover from it is by having a viable IV 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- Administering SQL form to protect your data, but first you need to know how the backup process works. How Backups Work Server 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
  7. 576 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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.
  8. BACKING UP YOUR DATA 577 3. On the Action menu, select New Backup Device. PA R T 4. In the Name box of the Backup Device Properties dialog box, enter NwindFull. IV 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. Administering SQL Server 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. TI P 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
  9. 578 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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.
  10. BACKING UP YOUR DATA 579 10. In the Select Backup Destination box, click Backup Device, select NwindFull, PA R T and click OK. IV Administering SQL Server 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.
  11. 580 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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.
  12. BACKING UP YOUR DATA 581 PA R T IV Administering SQL Server 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.
  13. 582 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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.
  14. BACKING UP YOUR DATA 583 4. In the View Backup Media Contents dialog box, you should see the differential PA R T backup of Northwind. IV Administering SQL Server 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. WARN I NG 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.
  15. 584 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS There are not a lot of steps to performing a transaction log backup, so let’s go through them. In this section, you are going to perform a transaction log backup on the 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 SQL Server to use that space.
  16. BACKING UP YOUR DATA 585 12. Click OK to start the backup. PA R T It is again prudent for you to manually verify that you did not accidentally over- IV write the full and differential backups that were stored in your backup device: 1. In Enterprise Manager, expand Management and click Backup. Administering SQL 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. Server 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 in such a scenario. Performing Filegroup Backups A growing number of companies out there today have databases that are reaching the terabyte 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 a very long backup time. Microsoft figured out that you don’t want to wait that long for 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.
  17. 586 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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; it can 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 the entire database all at once. There is, however, a caveat to be aware of when using filegroup backups to accelerate the backup process for VLDBs. Filegroups can also be used to expedite data access by placing tables on one file and the corresponding indexes on another file. Although this steps up data access, it can slow the backup process because you must back up tables and indexes as a single unit, as shown in Figure 16.1. This means that if the tables and indexes 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. FIGURE 16.1 Tables and indexes must be backed up as Table 1 Table 2 a single unit if they are stored on Index 1 Index 2 separate files. File 1 File 2 Backup Monday Backup Tuesday Table 1 Index 1 Table 2 Index 2 File 1 File 2 Backup Monday Backup Monday To perform a filegroup backup, you need to create a filegroup. Let’s add a file to the sales database that you created earlier: 1. Open Enterprise Manager and expand your server, then databases. 2. Right-click the sales database and select Properties.
  18. BACKING UP YOUR DATA 587 3. On the General tab, under File Name, add a file named Sales_Data_2 with a size PA R T of 5MB. IV 4. In the Filegroup column, enter Secondary to create a new filegroup. 5. Click OK to create the second file. Administering SQL Server Now you need to add a table to that filegroup and create a record in it so that you will 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.
  19. 588 CHAPTER 16 • BASIC ADMINISTRATIVE TASKS 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.
  20. BACKING UP YOUR DATA 589 8. Click the Save button to create the new table (it looks like a floppy disk on the PA R T toolbar) and enter Employees for the table name. IV 9. Close the table designer by clicking the small X button in the top-right corner of the window. Administering SQL 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 Server (note that the second value is arbitrary): USE Sales INSERT Employees VALUES(‘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 filegroup backup: 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.
Đồng bộ tài khoản