python oracle数据库备份脚本

简介:
+关注继续查看

学习python,将shell备份脚本改成了python脚本练手.python oracle备份脚本一例如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python  
# -*- coding: utf-8 -*- 
import time
import os
#配置oracle环境变量参数
os.environ["NLS_LANG"= "Simplified Chinese_china.ZHS16GBK"
os.environ['ORACLE_HOME'= '/u01/app/oracle/product/10.2.0/db_1'
os.environ['PATH'= "/u01/app/oracle/product/10.2.0/db_1/bin" + ":" + os.environ['PATH']
os.environ['ORACLE_SID'= "orcl"
os.environ['LANG'= "en_US"
#打印提示信息。
exp_node1 = "\n成功备份文件到目录...."
exp_node2 = "备份失败"
cls_node1 = "数据库备份数据清除成功,删除语句如下...."
cls_node2 = "数据库备份清除失败"
#配置导出的数据库信息
data_path = '/tmp/'
db_username = 'abc'
db_password = 'abc'
db_name = 'abc'
"""
print os.getenv('NLS_LANG')
print os.getenv('ORACLE_HOME')
print os.getenv('ORACLE_SID')
"""
def oracle_exp():
     exp_command = "exp %s/%s file=%s%s_%s-%s-%s_%s%s%s.dmp log=%s%s_%s-%s-%s_%s%s%s.log"  % (db_username,db_password,data_path,db_name,time.strftime('%Y'),time.strftime('%m'),time.strftime('%d'),time.strftime('%H'),time.strftime('%M'),time.strftime('%S'),data_path,db_name,time.strftime('%Y'),time.strftime('%m'),time.strftime('%d'),time.strftime('%H'),time.strftime('%M'),time.strftime('%S'))
     exp_note = "数据库备份执行语句...."
     print "\033[1;31;40m%s\033[0m" %  exp_note
     print "\033[1;31;40m%s\033[0m" %  exp_command
     if os.system(exp_command) == 0:
        print "\033[1;32;40m%s\033[0m" % exp_node1
     else:
        print "\033[1;31;40m%s\033[0m" % exp_node2
 
def backup_clear():
     cls_command1 = '/usr/bin/find %s -mtime +1 -name "*.dmp" -exec rm -rf {} \;' % data_path
     cls_command2 = '/usr/bin/find %s -mtime +1 -name "*.log" -exec rm -rf {} \;' % data_path
     if os.system(cls_command1) == 0 and os.system(cls_command2) == 0:
        print "\n\033[1;32;40m%s\033[0m" % cls_node1
     else:
        print "\n\033[1;31;40m%s\033[0m" % cls_node2
     print "%s" % cls_command1
     print "%s\n" % cls_command2
 
def main(): 
     oracle_exp() 
     backup_clear()
if __name__ == "__main__"
     main()

执行结果:

image

image




本文转自 koumm 51CTO博客,原文链接:http://blog.51cto.com/koumm/1438673,如需转载请自行联系原作者

相关文章
|
7天前
|
API 数据安全/隐私保护 Python
随手写了个博客多平台发布脚本:Python自动发布文章到Wordpress
作为一名技术博主,提高博客发布效率是我们始终追求的目标。在这篇文章中,我将分享一个基于Python的脚本,能够实现博客多平台发布,具体来说,是自动发布文章到WordPress。通过这个简单而高效的脚本,我们能够省去繁琐的手动发布步骤,提升工作效率。
随手写了个博客多平台发布脚本:Python自动发布文章到Wordpress
|
7天前
|
Linux Shell 索引
Python自动化脚本-运维人员宝典第一章 Python脚本概述
在学习本书前,你应该了解一些 Python 编程的基础知识,比如基础语法、变量类型、元组数据类型、列表字典、函数、字符串和方法。在python.org/downloads/上有3.7.2和2.7.15两个版本可供下载。本书中我们将使用3.7这一版本来作为代表示例和包的安装。
152 11
|
7天前
|
JSON Unix API
python脚本获取未来60天的天气预报
python脚本获取未来60天的天气预报
20 3
|
22天前
|
监控 机器人 开发工具
python监控脚本外发钉钉告警
python监控脚本外发钉钉告警
22 1
|
23天前
|
数据采集 监控 数据处理
Python自动化与脚本编写:提升效率与简化工作流程
Python自动化与脚本编写:提升效率与简化工作流程
|
1月前
|
Java Python
java调用python脚本并传递参数list
java调用python脚本并传递参数list
|
1月前
|
Java Python
Java调用Python脚本
Java调用Python脚本
|
1月前
|
Python
Python pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
Python pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
|
2月前
|
Linux 数据安全/隐私保护 C++
如何使用Cython对python脚本加密成pyd/so
如何使用Cython对python脚本加密成pyd/so
60 0
如何使用Cython对python脚本加密成pyd/so
|
2月前
|
Linux 索引 Python
【python脚本】ICer的脚本入门训练——find_key
【python脚本】ICer的脚本入门训练——find_key
推荐文章
更多
推荐镜像
更多