oracle数据库自动定时备份脚本 ruby脚本 需要安装ruby

require 'net/http'
require 'Win32API'
class Getdatabasebackup    
  
  def deleteoldfile()
    @savefilecount=8
    #保留最后7份备份
    @orcle_path="D:/oracle/product/10.2.0/db_1/BIN/exp.exe"
    @user="demo/demo"
    @backup_path="e:/swiage/test/"
    #@backup_path="\\\\192.168.10.65/share/"
    #备份到网络路径下
    log=File.open("c:/log.txt","a")
    temp=0
    minfilename=""
    mincreate=0
    filecount=Dir.entries(@backup_path).length
    if(filecount>@savefilecount)
      Dir.foreach(@backup_path) do |f| 
       if(f.size>2)
        create=File.ctime(@backup_path+f).to_i
        if(mincreate==0)
          mincreate=create
          minfilename=f
        end 
        temp=create        
        if(mincreate>temp)
           mincreate=temp
           minfilename=f
        end
       end
      end 
      begin
        File.delete(@backup_path+minfilename)
        log<<Time.now.to_s+" delete file "+minfilename+"\n"
      rescue  
        log<<Time.now.to_s+" delete file "+minfilename+" error!\n"       
      end        
    end  
    result="error"
    while(result=="error")
       result=databasebackup(@orcle_path,@user,@backup_path) 
    end
    log<<Time.now.to_s+" backup database file "+result+"\n"
  end
  
  def databasebackup(toolspath,database,filepath)
     system = Win32API.new("msvcrt","system",['p'],'i')
     @filepath=filepath
     @toolspath=toolspath
     @database=database     
     filename="backup_"+Time.now.strftime("%Y_%m_%d_%H%M")+".dmp"
     stut =system.call(@toolspath+" "+@database+" GRANTS=Y file="+@filepath+ filename) 
     system=nil
     if(stut==0)
       return filename
     else
       return "error"
     end
  end
end

Getdatabasebackup.new.deleteoldfile()
    

 

Oracle_backup.rb

数据库备份程序,用于自动备份完整的数据库

 

 

脚本执行需要Ruby编译内核的支持即需要安装Ruby开发包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值