MSSQL Server database Backup Restore Common SQL statements and note

Source: Internet
Author: User
Tags mssql mssql server

1. Backing Up the database

Backup Database Db_nameto disk= ' D:\db_name.bak ' with format

--by using the with format, you can overwrite any existing backup and create a new media set.
--You can avoid some errors caused by backup settings.
--explanation on the Help document:
--FORMAT specifies that the media header should be written to all volumes used for this backup operation.
-Any existing media headers are rewritten. The format option invalidates the entire media content.
--and ignore any existing content.

2. Backing up logs

Backup log log_nameto disk= ' D:\log_name.log ' with NORECOVERY

--When backing up the log, use Norecovery to back up the tail log.
--When you back up the log, the database is in a recovery state,
--Other processes can no longer access the database, the log will no longer increase,
-This will restore the normal recovery time.
--When restoring a backup, if you want to restore the log at the same time,
--must be added norecovery to restore the log, if there are multiple log backups,
--In addition to the last log backup, restoring other logs must also be accompanied by norecovery to revert to the last log.
-In the final analysis, this is a database requirement.

3. Restore the Database
Take the database offline first
ALTER DATABASE db_name set offline with rollback immediate
To restore again

Restore Database Db_namefrom disk= ' D:\db_name.bak ' with replace


Attention:
1. Make sure the database is online before backing up
2. Make sure the database is offline before restoring

4. Offline and on-line database
Offline (disconnect from other users)

ALTER DATABASE db_name set offline with rollback immediate


On-Line (open connection to other users)

ALTER DATABASE db_name set online with  rollback immediate

 

MSSQL Server database Backup Restore Common SQL statements and note

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.