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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值