`
wuhuizhong
  • 浏览: 681216 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

Simple database (schema) backup using PL/SQL

 
阅读更多

Usually, database backups are the responsibility of DBAs, and not something that developers really care or think too much about. However, if you are a lone developer, or part of a small team that doesn't have the luxury of having a dedicated DBA, then you might have to deal with backups yourself.

There are several ways to make Oracle backups; what I will be concentrating on here is the "Data Pump Export" method. You may already be familiar with the command-line "expdp" command which allows you to create a dump (.dmp) file containing your database objects (schemas, tables, procedures, etc.).

To perform a regular backup, one could create a simple batch file to run "expdp" and schedule it using the Windows Task Scheduler or a Unix Cron job. While this certainly works, it means that you have one more "moving part" to think about (the OS scheduler), and you may have to hardcode database passwords in the batch file unless you use OS authentication.

However, the "expdp" command-line utility is actually just a "front-end" to a PL/SQL package called DBMS_DATAPUMP, as can be seen in this diagram:



This means that we can call the DBMS_DATAPUMP package directly from our own PL/SQL code to create dump files, and use DBMS_JOB to schedule this backup at regular intervals. We avoid the dependence on batch files, and we don't have to expose any passwords.

The API for DBMS_DATAPUMP is flexible and, as mentioned, supports everything that can be done from the command line (both exports and imports), but it can sometimes be a bit tricky to get the (filtering) syntax correct.

I've written a wrapper package called DATAPUMP_UTIL_PKG, which you can download as part of the Alexandria PL/SQL Utility Library. This package makes it really easy to perform the most common use case: Exporting a single schema to a file on disk. This can be accomplished with the following code:



-- export current schema to file, use default file name, and make the export compatible with XE 10g
-- include a custom message


begin
  debug_pkg.debug_on;
  datapump_util_pkg.export_schema_to_file ('DEVTEST_TEMP_DIR', p_version => '10.2', p_log_message => 'it is possible to include custom messages in the log');
end;
/


To schedule this as a regular job, use the following code:

declare
  l_job number;
  l_what varchar2(4000);
begin

  l_what := 'datapump_util_pkg.export_schema_to_file(''DEVTEST_TEMP_DIR'', p_version => ''10.2'');';

  -- run job at the end of each day

  dbms_job.submit (job => l_job, what => l_what, next_date => sysdate, interval => 'trunc(sysdate + 1) + 23/24');

  commit;

end;
/

For this to work, you also need to set up a directory on the database server:

-- run as SYS
create directory devtest_temp_dir as 'c:\some_folder\backup\';
grant read, write on directory devtest_temp_dir toyour_database_schema;


Obviously, since the files are written to the database server itself, you need some kind of process to move the files to another server or storage location, so the backups don't disappear along with the database if the database server itself is somehow lost or corrupted. Unlike the "hot" database files, these dump files can be safely copied by normal backup software.

Another option is to upload the backup file from the database to some online ("cloud") storage service, such as Amazon S3. I will talk about that in my next blog post...

PS. Don't forget to regularly test a restore (import) of your backup files. The only thing worse than not having a backup is having a backup that can't be restored... !

分享到:
评论

相关推荐

    Oracle PL/SQL常用47个工具包

    Oracle PL/SQL是一种强大的编程语言,它将SQL与过程编程语言的功能结合起来,为数据库管理和开发提供了丰富的工具。在Oracle环境中,PL/SQL是构建高效、可靠应用程序的关键组件。以下是对"Oracle PL/SQL常用47个工具...

    英文版Oracle PL/SQL Packages and Types Reference

    《Oracle PL/SQL Packages and Types Reference》是一本针对Oracle数据库编程的重要参考资料,主要涵盖了PL/SQL包和类型的详细信息。这本书对于那些具有英文阅读能力的开发者来说,是开发过程中的重要工具,能够帮助...

    PL/SQL 语法手册

    《PL/SQL 语法手册》全面涵盖了SQL与PL/SQL的相关语法,是数据库开发者和管理员的重要参考资料。手册分为两大部分,分别对SQL语法和PL/SQL语法进行了详细讲解。 第一部分,SQL语法部分,主要涉及以下关键语句和概念...

    如何通过PL/SQL访问到Web Services

    随着Web Services的广泛应用,Oracle也提供了相应的技术支持,让开发人员能够通过PL/SQL直接调用Web Services。这篇文章主要探讨如何在Oracle环境中,利用PL/SQL的UTL_DBWS包来访问Web Services。 在Oracle 9i版本...

    《Oracle9i: SQL与PL/SQL开发指南》习题

    《Oracle9i: SQL与PL/SQL开发指南》是一本专为学习Oracle数据库管理与开发的书籍,其中包含了丰富的习题。这些习题基于一个实际情境,即JustLee Books,这是一个在线图书销售商,通过互联网销售图书给全美客户。在...

    ORACLE PL-SQL超经典面试题

    Oracle PL/SQL是Oracle数据库系统中的重要组成部分,用于创建复杂的业务逻辑和数据库处理。以下是对题目中提到的一些关键知识点的详细解释: 1. **冷备份与热备份**:冷备份是在数据库关闭状态下进行的,简单且可靠...

    plsqldev12.0.4.1826x64主程序+ v12中文包+keygen

    The new PL/SQL Clipboard is a dockable tool that stores the history of all SQL and PL/SQL code you copy to the Windows clipboard, so that you can paste the clipboard item again in the future....

    SQL DataBase schema & data Compare

    本文将深入探讨“SQL Database Schema & Data Compare”这一主题,这对于数据库管理员、开发人员以及任何需要确保数据库一致性的人来说至关重要。 首先,我们要理解什么是数据库模式(Schema)。在SQL中,模式是指...

    toad 使用教程

    使用 PL/SQL Editor,可以编辑 PL/SQL 语句,将其格式化为标准格式,并执行 PL/SQL 语句。 PL/SQL Debugger PL/SQL Debugger 是 Toad 中的一个功能强大的调试工具,提供了 PL/SQL 语句的调试功能。使用 PL/SQL ...

    重建WMSYS用户的WMSYS.WM_CONCAT函数的3个文件

    - 解析PL/SQL绑定文件:使用如PL/SQL Developer等工具解析PL/SQL绑定文件,将其转换为可读的PL/SQL源代码。 - 创建或替换函数:根据解析出的源代码,在WMSYS用户下创建或替换WM_CONCAT函数。 - 授权:确保正确地...

    Oracle Web应用开发.pdf

    Oracle Web应用开发主要涉及到Oracle数据库与Web服务器的集成,尤其是Apache服务器与PL/SQL的结合。在Oracle 8i/9i版本中,开发人员可以选择使用Java或PL/SQL来展示和操作数据库中的数据。其中,PL/SQL Pages(PSP...

    Toad新手入门教程

    Toad是一款功能强大且广泛应用于Oracle开发的集成开发环境(IDE),本文旨在为新手提供一个综合的入门教程,涵盖Toad的安装、设置环境变量、 Schema Browser、SQL Editor、PL/SQL Editor、PL/SQL Debugger、SQL ...

    pl sql批量执行多个sql文件和存储过程

    ### PL/SQL批量执行多个SQL文件和存储过程 在日常的数据库管理与开发工作中,经常需要执行大量的SQL脚本或调用多个存储过程。对于Oracle数据库而言,PL/SQL是一种非常强大的工具,它不仅可以用于编写复杂的数据库...

    解决dubbo启动的时候报错,无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'

    解决启动dubbo项目的时候出现,无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd',其实在你本地把dubbo.jar文件解压,然后在META-INF下边就有个dubbo.xsd,就是他

    utPLSQL:一个 Oracle PL/SQL 单元测试框架

    **utPLSQL:Oracle PL/SQL 单元测试框架详解** utPLSQL 是一个专为 Oracle 数据库中的 PL/SQL 代码设计的开源单元测试框架。这个框架使得开发者能够更有效地进行测试驱动开发(TDD)和行为驱动开发(BDD),确保 PL...

    TOAD使用教程 TOAD使用教程TOAD使用教程

    1. 快速入门:Toad 使用快速入门目录,包括.Schema browser 的用法简介、SQL Editor 的使用介绍、Procedure Editor 的用法介绍、如何进行 PLSQL 的 debug、如何使用 SQLab Xpert 优化 SQL、如何使用 SQL Modeler 来...

    PL/SQL远程备份和恢复Oracle数据库

    PL/SQL远程备份和恢复Oracle数据库是一个复杂的过程,涉及到数据的导出、备份、传输和恢复。了解这个过程对于数据库管理员来说至关重要,因为它们可以确保数据的安全性和可恢复性。接下来,我们将详细介绍使用PL/SQL...

    oracle10g中HR用户所有表的创建sql文件

    下载human_resources.zip后释放到%ORACLE_HOME%/demo/schema/human_resources/目录下,在SYS用户下运行(在PL/SQL Developer工具下即用导入sql文件方式:tools菜单-->import tables菜单项...-->sql inserts选项卡-->...

    plsql导出导入oracle表结构方法

    ### PL/SQL Developer 导出/导入 Oracle 表结构详解 #### 一、PL/SQL Developer 简介 PL/SQL Developer 是一款强大的 Oracle 数据库应用开发工具,它不仅支持 PL/SQL 语言的编写与调试,还提供了丰富的功能来帮助...

Global site tag (gtag.js) - Google Analytics