database backup
1. Classification of database backup
1.1 Logical Backup and Physical Backup
Database backup is divided into logical backup and physical backup according to the backup status.
(1) Logical backup
Use SQL to extract data from the database and save it as a binary file for backup. The business database adopts this method and does not need to be in archive mode. For example, in Oracle, use EXPORT to export database objects (users, tables, stored procedures), and use IMPORT to import logical backup files into the database (Oracle exp/imp).
Advantages: You can back up only certain tables or certain data; you can perform cross-platform recovery between different versions; you can store and save space after compression.
Disadvantages: For large-scale databases, it takes a long time to execute statements; complete consistency cannot be guaranteed (for example, new data is inserted at the same time).
(2) Physical backup
Physical backup refers to directly copying all the content on the database storage medium, including the operating system level and the database engine level, so that the entire database can be completely restored. Generally requires an external storage device.
Pros: Fast and reliable; can provide consistent snapshots.
Disadvantages: It can only provide recovery to a "certain point in time", cannot selectively (by table or user) backup and restore, only full database backup and restore; cannot be used for cross-platform migration.
- Cold backup: occurs when the database has been shut down normally (offline state), and all data files are offline. During the backup process, the database cannot do other work, and the integrity and consistency of the data can be guaranteed.
- Hot backup: It is performed when the database is running (online), and will not affect normal business operations. It can be backed up at the table space or database file level, and it is transparent to users, and can maintain business continuity. The backup time is short, up to seconds Level recovery (recovery to a point in time). To put the log in the archive mode for hot backup, it is necessary to consider that the data being written or modified may cause data inconsistency, and take special measures to ensure data integrity.
(3) Archive mode and non-archive mode
- Archive mode : In archive mode, the database saves historical records to a set of special files called "archive logs". These logs contain details of previously committed transactions, making it possible to recover or restore lost or corrupted data. Due to the frequent writes to the log file, performance will be reduced.
- Non-archive mode : In non-archive mode, when a transaction is committed, its results are written directly to the main data file. Instead of additionally generating log files like the archive mode. Although this method can improve performance and reduce space overhead, there is also the risk that data cannot be fully recovered when data is lost or damaged.
1.2 Full Backup/Differential Backup/Incremental Backup
(1) Full backup (Full) : Back up all selected files, and does not rely on the file's archive attribute to determine which files to back up (each file is marked as backed up, eliminating the archive attribute).
(2) Differential backup (Differential) : Back up all files that have changed since the last full backup (do not mark them as backed up files after backup, and do not remove the archive attribute). (Backup takes a long time, takes up a lot of space, and restores quickly).
(3) Incremental backup (Incremental) : Back up all files that have changed since the last backup (no matter what kind of backup) (mark files after backup, eliminate archive attributes). (Short backup time, less space, slow recovery).
Combination example:
- Full + poor backup: Full backup is performed on Monday, and differential backup is performed from Tuesday to Friday. If the data is destroyed on Friday, you need to restore the full backup on Monday and the differential backup on Friday.
- Complete + additional backup: complete backup on Monday, and incremental backup of models from Tuesday to Friday. If the data is destroyed on Friday, you need to restore the full backup on Monday and the incremental backups from Tuesday to Friday.
2. Oracle logical backup
Oracle supports two types of logical backups: export/import utility and data pump.
2.1 EXP/IMP
(1) EXP export
- full mode
exp SYSTEM/MANAGER BUFFER=64000 file= C:\full.dmp full=Y
- user mode
exp detail12/detail12 owner=(detail12,detail11) file=/home/oracle/detail.dmp
exp detail12/detail12@RACDB_192.168.1.13 file=d:/detail.dmp
Among them, RACDB_192.168.1.13
is the net service name of Oracle Net Configuration Assistant.
- table schema
exp detail12/detail12 owner=detail12 tables=(T_JBXX) file=/home/oracle/t_jbxx.dmp
If T_JBXX is a partition table, use tables=(T_JBXX1:P1,T_JBXX2:P2
the format of ).
query field format:
query="""where dept_date<to_date('2014-02-01 00:00:10','yyyy-mm-dd hh24:mi:ss')"""
Note : do not import permissions and indexes can be joinedGRANTS=Y INDEXES=Y
keyword description
keywords | description (default) |
---|---|
USERID | username/password |
FULL | Export entire file (N) |
BUFFER | data buffer size |
OWNER | List of owner usernames |
FILE | Output file (EXPDAT.DMP) |
TABLES | list of table names |
COMPRESS | import into a zone (Y) |
RECORDLENGTH | The length of the IO record |
GRANTS | Export permission (Y) |
INCTYPE | incremental export type |
INDEXES | export index (Y) |
RECORD | Track Incremental Export (Y) |
DIRECT | direct path (N) |
TRIGGERS | Export Trigger (Y) |
LOG | log file for screen output |
STATISTICS | Analysis object (ESTIMATE) |
ROWS | Export Data Rows (Y) |
PARFILE | parameter file name |
CONSISTENT | Crosstab Consistency (N) |
CONSTRAINTS | Exported Constraints (Y) |
OBJECT_CONSISTENT | Transactions set to read-only only during object export (N) |
FEEDBACK | show progress every x lines(0) |
FILE SIZE | Maximum size of each dump file |
FLASHBACK_SCN | The SCN used to set the session snapshot back to the previous state |
FLASHBACK_TIME | The time used to get the SCN closest to the specified time |
QUERY | select clause to export a subset of the table |
RESUMABLE | Hang on encountering whitespace-related errors (N) |
RESUMABLE_NAME | A text string identifying a recoverable statement |
RESUMABLE_TIMEOUT | RESUMABLE wait time |
TTS_FULL_CHECK | Perform a full or partial dependency check on TTS |
TABLESPACES | list of tablespaces to export |
TRANSPORT_TABLESPACE | export transportable tablespace metadata (N) |
TEMPLATE | Call the template name exported by iAS mode |
(2) IMP import
- full mode
imp SYSTEM/MANAGER BUFFER=64000 file=C:\full.dmp full=Y
- user mode
imp detail12/detail12 fromuser=(detail12,detail11) touser=(detail12,detail11) file=/home/oracle/detail.dmp
- table schema
imp detail12/detail12 owner=detail12 tables=(T_JBXX) file=/home/oracle/t_jbxx.dmp
Notice
- IMP imports must be specified
Full=Y
, or provided withfromuser/touser
arguments, or provided withtables
arguments. - The character set needs to be set as required before importing
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
- 忽略创建表错误可加入
IGNORE=Y
,不导入权限和索引可加入GRANTS=Y INDEXES=Y
- 导入时发生
IMP-00032
错误,表示SQL语句超过缓冲区长度,可以加入选项:buffer=100000000
关键字说明
关键字 | 说明 (默认值) |
---|---|
USERID | 用户名/口令 |
FULL | 导入整个文件 (N) |
BUFFER | 数据缓冲区大小 |
FROMUSER | 所有者用户名列表 |
FILE | 输入文件 (EXPDAT.DMP) |
TOUSER | 用户名列表 |
SHOW | 只列出文件内容 (N) |
TABLES | 表名列表 |
IGNORE | 忽略创建错误 (N) |
RECORDLENGTH | IO 记录的长度 |
GRANTS | 导入权限 (Y) |
INCTYPE | 增量导入类型 |
INDEXES | 导入索引 (Y) |
COMMIT | 提交数组插入 (N) |
ROWS | 导入数据行 (Y) |
PARFILE | 参数文件名 |
LOG | 屏幕输出的日志文件 |
CONSTRAINTS | 导入限制 (Y) |
DESTROY | 覆盖表空间数据文件 (N) |
INDEXFILE | 将表/索引信息写入指定的文件 |
SKIP_UNUSABLE_INDEXES | 跳过不可用索引的维护 (N) |
FEEDBACK | 每 x 行显示进度 (0) |
TOID_NOVALIDATE | 跳过指定类型 ID 的验证 |
FILESIZE | 每个转储文件的最大大小 |
STATISTICS | 始终导入预计算的统计信息 |
RESUMABLE | 在遇到有关空间的错误时挂起 (N) |
RESUMABLE_NAME | 用来标识可恢复语句的文本字符串 |
RESUMABLE_TIMEOUT | RESUMABLE 的等待时间 |
COMPILE | 编译过程, 程序包和函数 (Y) |
STREAMS_CONFIGURATION | 导入流的一般元数据 (Y) |
STREAMS_INSTANTIATION | 导入流实例化元数据 (N) |
2.2 EXPDP/IMPDP
(1)数据泵介绍
Oracle Database 10g引入了最新的数据泵(Data Dump)技术,使DBA或开发人员可以将数据库元数据(对象定义)和数据快速移动到另一个oracle数据库中,与Export / Import相比,它提供更好的性能、安全性和灵活性。
- 并行处理: Oracle 数据泵支持并行处理(通过指定
PARALLEL
参数启用),这使得导入和导出任务更加快速和高效。 - 可定制: 数据泵允许用户选择要包含或排除的对象类型(如表、视图、约束等),以及要包含或排除的特定对象。
- 压缩: Oracle 数据泵支持压缩功能,可减少导出文件的大小,并提高传输速度。
- 安全性: 数据泵支持 SSL 加密协议来保护敏感信息,在安全方面非常有优势。
- 多平台兼容性: Oracle 数据泵支持多个平台上运行,例如 Windows 和 Linux 等操作系统。
(2)数据泵的使用
EXP和IMP是客户段工具程序,它们既可以在可以客户端使用,也可以在服务端使用。 EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用。
使用EXPDP工具时,其转储文件只能被存放在DIRECTORY对象对应的OS目录中,而不能直接指定转储文件所在的OS目录。因此,使用EXPDP工具时,必须首先建立DIRECTORY对象,并且需要为数据库用户授予使用DIRECTORY对象权限。
使用Data Pump,可将整个数据库或特定模式及其对象(如表格、索引、约束和其数据)导出/导入,还可指定选项,例如表空间、压缩级别、加密和远程导出/导入的网络链接。
Data Pump包括两个客户端实用程序:expdp(Export Data Pump)和impdp(Import Data Pump)。
e.g.
create directory imp_dir as 'D:\imp_dir';
GRANT READ, WIRTE ON DIRECTORY imp_dir TO detail;
expdp scott/tiger DUMPFILE=tmp_dump.dmp DIRECTORY= imp_dir
impdp scott/tiger DUMPFILE=tmp_dump.dmp DIRECTORY= imp_dir