sql还原数据库备份数据库
So far, we’ve discussed a lot about database backup-and-restore process. The backup database command is an online database copy of the SQL Server database and restore database command gives an option to test the consistency and integrity of the backup file.
到目前为止,我们已经讨论了很多有关数据库备份和还原过程的内容。 backup database命令是SQL Server数据库的联机数据库副本,restore database命令提供了一个选项来测试备份文件的一致性和完整性。
As we all know, the backup database command bound with many database options. Indeed, it facilitates the execution of specific backup database command that meets the business requirement.
众所周知,backup database命令绑定了许多数据库选项。 实际上,它有助于执行满足业务需求的特定备份数据库命令。
The backup database is further classified into two preliminary backup types
备份数据库进一步分为两种初步备份类型
Each of the above types defines the type of data.
以上每种类型都定义了数据类型。
Let’s deep dive and review each of the database backup command topics to get a better understanding of what it is all about. In this article, we will find an answer for FAQs about the SQL Server database backup. We will learn more about database backup.
让我们深入研究并复查每个数据库备份命令主题,以更好地了解它的全部含义。 在本文中,我们将找到有关SQL Server数据库备份的常见问题解答的答案。 我们将了解有关数据库备份的更多信息。
They is so important because of following reasons:
它们之所以如此重要是因为以下原因:
A Backup is a process to create a copy of data and it can be used to reconstruct the data in case of any failures.
备份是创建数据副本的过程,在发生任何故障的情况下,它可用于重建数据。
Backing up your SQL Server databases, running test restores procedures on your backups, and storing copies of backups in a safe, on-site, off-site, and cloud location protects and safeguard the data from potentially catastrophic or various types of data loss.
备份SQL Server数据库,运行测试将还原备份上的过程,并将备份的副本存储在安全的现场,异地和云位置,以保护和保护数据免于潜在的灾难性或各种类型的数据丢失。
The following are the different types of SQL Server database backups.
以下是不同类型SQL Server数据库备份。
The type of a database backup depends on the database recovery model of the database.
数据库备份的类型取决于数据库的数据库恢复模型。
The Recovery Model is a property of a database that controls how the transactions are logged.
恢复模型是控制交易记录方式的数据库的属性。
The design of entire database and recovery procedures based on the context of the recovery model of the database.
基于数据库恢复模型的上下文,设计整个数据库和恢复过程。
It controls and manages each transaction scope into its entirety. You can refer to it here
它完全控制和管理每个事务范围。 你可以在这里参考
In the following example, query the sys.databases catalog view the recovery model of the all the databases
在下面的示例中,查询sys.databases目录以查看所有数据库的恢复模型
SELECT name, recovery_model_desc FROM sys.databases;
The following example, the recovery model of the model database is set to FULL using the SET RECOVERY option of the ALTER DATABASE statement.
在以下示例中,使用ALTER DATABASE语句的SET RECOVERY选项将模型数据库的恢复模型设置为FULL。
ALTER DATABASE model SET RECOVERY FULL
What permission is required to take a database backup?
进行数据库备份需要什么权限?
By default to members of the sysadmin fixed server role and the db_owner and db_backupoperator fixed database roles.
缺省情况下,是sysadmin固定服务器角色以及db_owner和db_backupoperator固定数据库角色的成员。
In this backup type, the whole database is backed up. This is the base for any type of backups. In order to have further differential or transaction log backups, you must create the full database backup.
在这种备份类型中,将备份整个数据库。 这是任何类型备份的基础。 为了进一步进行差异日志或事务日志备份,必须创建完整的数据库备份。
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
How can I find the progress of percentage completion of the database backup?
如何找到数据库备份完成百分比的进度?
Use the keyword STATS in the T-SQL to monitor backup progress status. This can also be used with restore command to measure the progress.
在T-SQL中使用关键字STATS监视备份进度状态。 也可以将其与restore命令一起使用以测量进度。
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK' WITH STATS
Use the keyword FORMAT and COMPRESSION to format and compress the database backup.
使用关键字FORMAT和COMPRESSION格式化和压缩数据库备份。
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS,FORMAT, COMPRESSION;
On specifying the INIT keyword, all the backup sets are overwritten.
指定INIT关键字后,所有备份集将被覆盖。
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS,INIT
By default, the NOINIT option is enabled. It means that the backup will append to other backups in the file
默认情况下,启用了NOINIT选项。 这意味着备份将追加到文件中的其他备份
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS, NOINIT
OR
要么
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
You can query the sys,filegroups joined with sys. databases_files to get the filegroup related information.
您可以查询sys,与sys一起加入的文件组。 database_files获取与文件组相关的信息。
SELECT
dbf.name AS filename,
dbf.size/128 AS FileSizeMB,
dfg.name AS FGName,
dfg.type_desc,
dbf.physical_name AS Physicalpath
FROM
sys.database_files AS dbf
INNER JOIN sys.filegroups AS dfg
ON
dbf.data_space_id = dfg.data_space_id
The following the system stored procedures:
以下系统存储过程:
sp_helpdb ‘PowerSQL’
sp_helpdb'PowerSQL'
On specifying the database name, it displays the database information along with the list all the files and related filegroup of the database
指定数据库名称后,它将显示数据库信息以及列表中数据库的所有文件和相关文件组
sp_helpfile
sp_helpfile
sp_helpfile is a subset of sp_helpdb and it returns files, filegroup, and their properties.
sp_helpfile是sp_helpdb的子集,它返回文件,文件组及其属性。
sp_helpfile PowerSQL_1
sp_helpfile PowerSQL_1
On specifying the parameter, the file, it will list the details of that specified file and associated details.
在指定参数文件后,它将列出该指定文件的详细信息以及相关的详细信息。
sp_helpfilegroup
sp_helpfilegroup
sp_helpfilegroup lists all the filegroups and number of associated data files in each filegroup.
sp_helpfilegroup列出所有文件组以及每个文件组中关联数据文件的数量。
On specifying the filegroup, the output lists the specific filegroup and associated data file details.
指定文件组后,输出将列出特定的文件组和关联的数据文件详细信息。
A Differential backup is also a type of SQL Server database backup where it copies all the data that has changed since the last full backup.
差异备份也是SQL Server数据库备份的一种,它复制自上次完全备份以来已更改的所有数据。
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH DIFFERENTIAL
The transaction log records every modification made to the databases. The records are maintained in such a way that the system can be brought to a consistent state at any point of time with minimal or no data loss.
事务日志记录对数据库所做的每次修改。 记录的维护方式使系统可以在任何时间点保持一致状态,而数据丢失最少或没有。
For T-Log backup, the full backup is the base. It takes complete log file data to write to a backup file.
对于T-Log备份,完整备份是基础。 它需要完整的日志文件数据才能写入备份文件。
The transaction log backup depends on the database recovery model and it’s relevant for the databases that are using the full or bulk-logged recovery models.
事务日志备份取决于数据库恢复模型,并且与使用完整或批量记录的恢复模型的数据库有关。
To execute the BACKUP LOG statement to back up the transaction log, specify the database name and backup device
要执行BACKUP LOG语句来备份事务日志,请指定数据库名称和备份设备
BACKUP LOG PowerSQL TO DISK=N'f:\PowerSQL\PowerSQL_tlog.trc'
How can I continue the database backup processes despite they encounter an error?
尽管遇到错误,如何继续数据库备份过程?
It’s basically overriding the default behavior of database backup process using CONTINUE_AFTER_ERROR. On error, the backup process will stop the process.
它基本上是使用CONTINUE_AFTER_ERROR覆盖数据库备份过程的默认行为。 出错时,备份过程将停止该过程。
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH CHECKSUM, CONTINUE_AFTER_ERROR,STATS, FORMAT, COMPRESSION
In case of failure, it is required to start the recovery process, the first and foremost important step is intended to ensure take care of tail part of the transaction before starting the restoration process is called tail-log backup.
如果发生故障,则需要启动恢复过程,第一个也是最重要的一步是要确保在开始恢复过程之前要注意事务的尾部,这称为尾日志备份。
WITH CONTINUE_AFTER_ERROR keyword, the SQL Server will override the default behavior even though it’s generating an error to complete the backup process.
使用CONTINUE_AFTER_ERROR关键字,即使SQL Server生成错误以完成备份过程,它也会覆盖默认行为。
USE MASTER
GO
BACKUP LOG PowerSQL TO DISK=N'f:\PowerSQL\PowerSQL_tlog.trc' WITH CHECKSUM, CONTINUE_AFTER_ERROR,STATS
It’s a special type of backup. It is independent of the conventional backups and it will not have an impact on the overall backup process.
这是一种特殊的备份。 它独立于常规备份,不会对整个备份过程产生影响。
In simple words, it is used to create a full database or transaction log backup without breaking the log chain
简而言之,它用于创建完整的数据库或事务日志备份,而不会破坏日志链
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH COPY_ONLY,STATS, FORMAT, COMPRESSION
In some cases, it is required to create multiple copies of data into different files. You can create a maximum of three mirror copies of the data at a time.
在某些情况下,需要将数据的多个副本创建到不同的文件中。 您一次最多可以创建三个镜像副本。
BACKUP DATABASE PowerSQL
TO DISK = 'F:\PowerSQL\PowerSQL.BAK'
MIRROR TO DISK = 'F:\PowerSQL\PowerSQL_1.BAK'
MIRROR TO DISK = 'F:\PowerSQL\PowerSQL_2.BAK'
WITH FORMAT, COMPRESSION, STATs
GO
Partial database backup is one of the rarely used backup methods. All though it works with all recovery models, it is basically designed for simple database recovery model database. This provides flexibility for backing up only READ_WRITE_FILEGROUPS.
部分数据库备份是很少使用的备份方法之一。 尽管它适用于所有恢复模型,但它基本上是为简单数据库恢复模型数据库而设计的。 这提供了仅备份READ_WRITE_FILEGROUPS的灵活性。
BACKUP DATABASE PowerSQL READ_WRITE_FILEGROUPS TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH COPY_ONLY,STATS, FORMAT, COMPRESSION
This type of backup is mainly used where there is an issue with storage space.
这种备份类型主要用于存储空间有问题的地方。
In this type of database backup, the data will be split into parts and can be very useful during space constraints. Striped backup is a process of taking backup to different locations.
在这种类型的数据库中 备份时,数据将分为几部分,并且在空间受限的情况下非常有用。 条带化备份是将备份转移到不同位置的过程。
BACKUP DATABASE PowerSQL
TO DISK = 'F:\PowerSQL\PowerSQL.BAK',
DISK = 'F:\PowerSQL\PowerSQL_1.BAK',
DISK = 'F:\PowerSQL\PowerSQL_2.BAK'
WITH FORMAT, COMPRESSION, STATS=10
Every SQL Server database must have a minimum of a data file and a log file. We can also create more than one files and it can be grouped together in filegroups for easier file management and administration purpose.
每个SQL Server数据库必须至少具有一个数据文件和一个日志文件。 我们还可以创建多个文件,并且可以将其分组到文件组中,以简化文件管理和管理目的。
Using this method, the desired file\file group backup is possible
使用此方法,可以备份所需的文件\文件组
File Backup
文件备份
Syntax: BACKUP DATABASE [DBNAME] FILE = N’FILENAME’ TO DISK = N’Path’
语法: BACKUP DATABASE [DBNAME] FILE = N'FILENAME'TO DISK = N'Path'
BACKUP DATABASE PowerSQL FILE = N'PowerSQL_Index' TO DISK = N'F:\PowerSQL\PowerSQL_Index.BAK'
File Group Backup:
文件组备份:
Syntax: BACKUP DATABASE [DBNAME] Filegroup = N’Filegroup Name’ TO DISK = N’Path’
语法: BACKUP DATABASE [DBNAME]文件组= N'文件组名称'TO DISK = N'路径'
BACKUP DATABASE PowerSQL
Filegroup = N'PRIMARY',
Filegroup = N'SalesInMemFG' TO DISK = N'F:\PowerSQL\PowerSQL_PRIMARYFG.BAK'
WITH FORMAT, STATS
If you want the backup to expire, use WITH EXPIREDATE clause option in the backup database T-SQL. The following example shows How can I back up with an expiration date on Jun 28, 2018:
如果要使备份过期,请在备份数据库T-SQL中使用WITH EXPIREDATE子句选项。 以下示例显示了如何备份2018年6月28日的到期日期:
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL_PRIMARYFG.BAK' WITH EXPIREDATE = N'08/28/2018 00:00:00'
If you want to retain the backup for the only specific number of days then use the WITH RETAINDAYS clause with the database backup command.
如果要仅将备份保留特定的天数,请在数据库备份命令中使用WITH RETAINDAYS子句。
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL.BAK' WITH RETAINDAYS = 3 , FORMAT, STATS=10
USE MASTER
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'PowerSQL$2018'
CREATE CERTIFICATE BackupServerCert WITH SUBJECT = 'Backup certificate'
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL.BAK'
WITH FORMAT,
ENCRYPTION
(
ALGORITHM = AES_256,
SERVER CERTIFICATE = BackupServerCert
),
STATS = 10
GO
See Also,
也可以看看,
Understanding Database Backup Encryption in SQL Server
了解SQL Server中的数据库备份加密
FULL | BULK LOGGED | SIMPLE | |
Log level | All | All, but minimal log for few operations such as bulk operations(bcp, BULK INSERT) Select into, Create index, alter index, drop index, updatetext, and writetext | Minimal |
Log Truncation Process (LTP) Life | During backup process | During backup process | Every time Checkpoint background run against the database |
Can it use for Production Server? | Yes | Depends – Yes/No | Depends – Yes/No |
Point-in-time recovery | Yes | No | No |
Log backup support | Yes | Yes |
No |
Piecemeal Restore | Yes | Yes | Yes |
Log Shipping Support | Yes | Yes | No |
Database Mirroring Support | Yes | No | No |
Database Replication Support | Yes | Yes | Yes |
充分 | 批量记录 | 简单 | |
日志级别 | 所有 | 少量操作的全部日志,但是日志很少,例如批量操作(bcp,BULK INSERT) | 最小的 |
日志截断过程(LTP)寿命 | 在备份过程中 | 在备份过程中 | 每次对数据库运行Checkpoint后台 |
可以用于生产服务器吗? | 是 | 取决于–是/否 | 取决于–是/否 |
时间点恢复 | 是 | 没有 | 没有 |
日志备份支持 | 是 | 是 |
没有 |
零碎还原 | 是 | 是 | 是 |
日志传送支持 | 是 | 是 | 没有 |
数据库镜像支持 | 是 | 没有 | 没有 |
数据库复制支持 | 是 | 是 | 是 |
System databases are an essential component of SQL Server engine for the functioning of a server instance. The system databases are critical as it stores meta-data of the user-defined databases. It must be backed up after every significant update as we do it for user-defined databases. The system databases that you must always back up include msdb, master, model and configuration databases.
系统数据库是SQL Server引擎中服务器实例功能的基本组成部分。 系统数据库至关重要,因为它存储用户定义数据库的元数据。 必须像我们对用户定义的数据库所做的那样,在每次重大更新后都对其进行备份。 您必须始终备份的系统数据库包括msdb , master , model和配置数据库。
System database | Description | Backup? | Recovery model |
master | The database is used to record all of the system level information | Yes | Simple |
model | It’s a template for all databases. |
Yes | User configurable* |
msdb | It is used by SQL Server Agent for job management and it also stores a history of every backup and restore operations | Yes | Simple (default) |
Resource | A read-only database that contains copies of all system objects that ship with SQL Server | No | — |
tempdb | A workspace for SQL Server | No | Simple |
Configure Distribution | Configuration database for replication | Yes | Simple |
系统资料库 | 描述 | 备份? | 恢复模式 |
主 | 该数据库用于记录所有系统级别的信息 | 是 | 简单 |
模型 | 它是所有数据库的模板。 |
是 | 用户可配置* |
数据库 | SQL Server代理将其用于作业管理,并且还存储每个备份和还原操作的历史记录 | 是 | 简单(默认) |
资源资源 | 一个只读数据库,其中包含SQL Server附带的所有系统对象的副本 | 没有 | - |
临时数据库 | SQL Server的工作区 | 没有 | 简单 |
配置分配 | 复制配置数据库 | 是 | 简单 |
Database Backup and Restore process in SQL Server – series intro |
An overview of the process of SQL Server backup-and-restore |
Understanding the SQL Server Data Management Life Cycle |
Understanding SQL Server database recovery models |
Understanding SQL Server Backup Types |
Backup and Restore (or Recovery) strategies for SQL Server database |
Discussing Backup and Restore Automation using SQLCMD and SQL Server agent |
Understanding Database snapshots vs Database backups in SQL Server |
SqlPackage.exe – Automate SQL Server Database Restoration using bacpac with PowerShell or Batch techniques |
Smart database backup in SQL Server 2017 |
How to perform a Page Level Restore in SQL Server |
Backup Linux SQL Server databases using PowerShell and Windows task scheduler |
SQL Server Database backup and restore operations using the Cloud |
Tail-Log Backup and Restore in SQL Server |
SQL Server Database Backup and Restore reports |
Database Filegroup(s) and Piecemeal restores in SQL Server |
In-Memory Optimized database backup and restore in SQL Server |
Understanding Backup and Restore operations in SQL Server Docker Containers |
Backup and Restore operations with SQL Server 2017 on Docker containers using Azure Data Studio |
Interview questions on SQL Server database backups, restores and recovery – Part I |
Interview questions on SQL Server database backups, restores and recovery – Part II |
Interview questions on SQL Server database backups, restores and recovery – Part III |
Interview questions on SQL Server database backups, restores and recovery – Part IV |
SQL Server中的数据库备份和还原过程–系列简介 |
SQL Server备份和还原过程概述 |
了解SQL Server数据管理生命周期 |
了解SQL Server数据库恢复模型 |
了解SQL Server备份类型 |
SQL Server数据库的备份和还原(或恢复)策略 |
讨论使用SQLCMD和SQL Server代理进行备份和还原自动化 |
了解SQL Server中的数据库快照与数据库备份 |
SqlPackage.exe –使用bacpac和PowerShell或Batch技术自动执行SQL Server数据库还原 |
SQL Server 2017中的智能数据库备份 |
如何在SQL Server中执行页面级还原 |
使用PowerShell和Windows任务计划程序备份Linux SQL Server数据库 |
使用CloudSQL Server数据库备份和还原操作 |
SQL Server中的尾日志备份和还原 |
SQL Server数据库备份和还原报告 |
SQL Server中的数据库文件组和零碎还原 |
在SQL Server中进行内存优化的数据库备份和还原 |
了解SQL Server Docker容器中的备份和还原操作 |
使用Azure Data Studio在Docker容器上使用SQL Server 2017进行备份和还原操作 |
有关SQL Server数据库备份,还原和恢复的面试问题–第一部分 |
有关SQL Server数据库备份,还原和恢复的面试问题–第二部分 |
有关SQL Server数据库备份,还原和恢复的面试问题–第三部分 |
有关SQL Server数据库备份,还原和恢复的面试问题–第IV部分 |
翻译自: https://www.sqlshack.com/sql-interview-questions-on-database-backups-restores-and-recovery-part-i/
sql还原数据库备份数据库