Automatically generate SQL Server recovery scripts from backup files in one directory

Source: Internet
Author: User

Problem

One of the immediate challenges for database administrators is to back up and restore databases. Backups are done in the automatic scheduling table, but the recovery can take many different versions, you may need to restore a product database, recover a development package, or test the database, or create a copy of the database in another place. There are ways to automate the recovery process and create scripts, but this method shows a way to read only the contents of the directory that exists in the backup file.

Expert answers

Here's a simple way to read the contents of a directory and create a recovery command that needs to be performed to recover the database. This script can be used for all backups, differential backups, and transaction log backups.

Before we begin, the following scripting scenario assumes the following:

1. The restored database has the same name as the backup database.

2. The restored database and backup database are stored in the same location.

3, the file name has the following format

Dbname_yyyymmddhhmm.xxx

4. The file name extension is as follows

Full backup –bak

Differential backup –dif

Transaction log Backups –trn

5, xp_cmdshell is available

6. There is no missing transaction log that could destroy the recovery chain

So, let's follow these steps to create our own backups:

Make full backups at midnight

Start a differential backup every three hours 3:15

Starting at 1 in the morning. Log backups every half hour

At nine o'clock in the morning, we may get the following backup file created on September 10, 2008, which complies with the above rules and is named customer.

Customer_200809100000.bak

Customer_200809100100.trn

Customer_200809100130.trn

Customer_200809100200.trn

Customer_200809100230.trn

Customer_200809100300.trn

Customer_200809100315.dif

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.