生产线的sqlserver数据库infoservice星期日做全盘备份,备份名为:infoservice_database.7,星期一做差异备份:infoservice_database.1,星期二:infoservice_database.2...
对应的本地模拟库infoserviceonline要定期恢复,恢复顺序与备份顺序相反. 如今天是星期一,要先还原
上个星期日的全盘备份,再还原今天的差异备份.
本程序支持在数据库服务器恢复,也支持在工作站恢复.
断开数据库的存储过程(创建于master库):
Create proc DisconnectDB(@dbname varchar(20),@reason nvarchar(200)=null,@delay varchar(8)='00:00:30')
declare @sql nvarchar(500),@cmd nvarchar(500)
declare @spid int
declare @hostname nvarchar(255),@pgname nvarchar(255)
declare @flag bit
set @reason=isnull(@reason,N'未知')
set @flag=0
set @sql='declare getspid cursor for select spid,hostname,program_name from master..sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
print 'ready'
fetch next from getspid into @spid,@hostname,@pgname
while @@fetch_status <>-1
begin
set @flag=1
set @cmd='net send '+ltrim(rtrim(@hostname))+' 请及时保存你的程序:'+ltrim(rtrim(@pgname))+'的相关数据,'+'数据库:'+@dbname+'将于'+@delay+'后断开连接.'+'原因:'+@reason
print @cmd
exec master.dbo.xp_cmdshell @cmd
fetch next from getspid into @spid,@hostname,@pgname
end
close getspid
deallocate getspid
if @flag=1
waitfor delay @delay
exec (@sql)
open getspid
fetch next from getspid into @spid,@hostname,@pgname
while @@fetch_status <>-1
begin
exec('kill '+@spid)
fetch next from getspid into @spid,@hostname,@pgname
end
close getspid
deallocate getspid
print 'disconnect db:'+@dbname+' completely!'
return 0
python脚本
import logging
import os,shutil,subprocess
import re
import datetime
from datetime import timedelta
from datetime import date
global_log='D:\\tmp\\RestoreInfoServiceDB.log'
global_smb_db_filepath='\\\\192.168.0.84\\f$\\ftproot\\infoservice.online\\'
global_localtmpdir='d:\\tmp\\'
global_notify_user=['192.168.0.129','192.168.0.124']
global_sqlserver_user='sa'
global_sqlserver_password='hpsystem'
global_sqlserver_source='192.168.0.86'
global_sqlserver_db='infoserviceonline'
global_disconnectdb_delay='00:00:30'
# filemode='r',只读,清除原有内容(默认方式) filemode='r+',读写,清除原有内容
# filemode='w',只写,创建一个新的文件覆盖旧的 filemode='w+',读写,创建一个新的文件覆盖旧的
# filemode='a',只写,追加 filemode='a+',读写,追加
# filemode='b',二进制模式
logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s] [%(levelname)s] %(message)s',filename=global_log,filemode='a')
logging.getLogger(global_log)
def NotifyUser(level,message):
logging.debug("进入消息通知函数:NotifyUser")
if level==1:
messagekind='提示:'
if level==2:
messagekind='警告:'
if level==3:
messagekind='错误:'
for user in global_notify_user:
hint=messagekind+message
cmd=['net','send',user,hint]
rst=subprocess.call(cmd)
def search_file(filename, search_path):
logging.debug("进入文件搜索函数:search_file")
dbfilelist=os.listdir(search_path)
for dbfile in dbfilelist:
p=re.compile(filename)
m=p.match(dbfile)
if m:
if os.path.exists(search_path+m.group()):
return m.group()
else:
continue
return None
def CopyFileFromSmbToLocal(host,local,format):
logging.debug("进入文件拷贝函数:CopyFileFromSmbToLocal")
i=date.today().weekday()
tmpdate=date.today()-datetime.timedelta(days=i+1)
while tmpdate<=date.today():
filename=tmpdate.strftime(format)
filefullname=search_file(filename,host)
if filefullname:
localfile=local+filefullname
if os.path.exists(localfile):
tmpdate=tmpdate+datetime.timedelta(days=1)
continue
else:
hostfile=host+filefullname
shutil.copy(hostfile,localfile)
else:
logging.info('数据库压缩文件:'+filefullname+'不存在')
NotifyUser(2,'数据库压缩文件:'+filefullname+'不存在')
return None
tmpdate=tmpdate+datetime.timedelta(days=1)
logging.info('拷贝文件成功')
return 1
def pickfile(filename):
logging.debug("进入文件提取函数:pickfile")
destfile='*infoservice_database.*'
cmd=['winrar','e',filename,destfile,'-r','e',global_localtmpdir]
rst=subprocess.call(cmd)
if rst==0:
return 1
else:
return 0
def Decompress(local,format):
logging.debug("进入解压函数:Decompress")
i=date.today().weekday()
tmpdate=date.today()-datetime.timedelta(days=i+1)
i=tmpdate.weekday()
while tmpdate<=date.today():
localdbfile='infoservice_database.'+str(i+1)
filename=tmpdate.strftime(format)
filefullname=search_file(filename,local)
if os.path.exists(local+localdbfile):
tmpdate=tmpdate+datetime.timedelta(days=1)
i=tmpdate.weekday()
continue
else:
source=local+filefullname
pickfile(source)
tmpdate=tmpdate+datetime.timedelta(days=1)
i=tmpdate.weekday()
logging.info('解压文件成功')
return 1
def RemoveOldFile(local):
logging.debug("进入删除旧文件函数:RemoveOldFile")
i=date.today().weekday()
if i==6 or i==0:
logging.info('del '+local+'infoservice_database.*')
logging.info('del '+local+'*.bz2')
os.system('del '+local+'infoservice_database.*')
os.system('del '+local+'*.bz2')
return 1
def ExecSQL(command):
logging.debug("进入SQL语句执行函数:ExecSQL")
logging.info("执行的SQL语句:"+command)
cmd=['isql','/U',global_sqlserver_user,'/P',global_sqlserver_password,'/S',global_sqlserver_source,'/d','master','/Q',command]
rst=subprocess.call(cmd)
if rst==0:
return 1
else:
return 0
def DisConnectDB():
logging.debug("进入断开数据库连接函数:DisConnectDB")
command='Exec DisconnectDB "'+global_sqlserver_db+'","数据库还原","'+global_disconnectdb_delay+'"'
print command
rst=ExecSQL(command)
return rst
def doRestoreDB(IsInHost):
logging.debug("进入还原主函数:doRestoreDB")
RemoveOldFile(global_localtmpdir)
CopyFileFromSmbToLocal(global_smb_db_filepath,global_localtmpdir,'backup-%Y%m%d\d{6}\.tar\.bz2')
Decompress(global_localtmpdir,'backup-%Y%m%d\d{6}\.tar\.bz2')
if not IsInHost:
hostLocation='\\\\'+global_sqlserver_source+'\\'
hostLocation=hostLocation+global_localtmpdir.replace(':','$')
os.system('del '+hostLocation+'infoservice_database.*')
os.system('copy '+global_localtmpdir+'infoservice_database.* '+hostLocation)
result=DisConnectDB()
if result==0:
NotifyUser(3,'还原库失败,原因:断开连接失败')
return -3
i=date.today().weekday()
tmpdate=date.today()-datetime.timedelta(days=i+1)
j=tmpdate.weekday()
while tmpdate<=date.today():
localdbfile='infoservice_database.'+str(j+1)
filefullname=global_localtmpdir+localdbfile
if os.path.exists(filefullname):
if j+1==7:
cmd='restore database '+global_sqlserver_db+' from Disk="'+filefullname+'" with file=1, NORECOVERY'
if tmpdate==date.today():
cmd='restore database '+global_sqlserver_db+' from Disk="'+filefullname+'" with RECOVERY'
else:
cmd='restore database '+global_sqlserver_db+' from Disk="'+filefullname+'" with file=1,NORECOVERY'
if tmpdate==date.today():
cmd='restore database '+global_sqlserver_db+' from Disk="'+filefullname+'" with file=1,RECOVERY'
rst=ExecSQL(cmd)
if rst==0:
NotifyUser(3,'还原库失败,原因:执行'+cmd+'失败')
return -1
else:
NotifyUser(3,'还原库失败,原因:文件'+filefullname+'不存在')
return -2
tmpdate=tmpdate+datetime.timedelta(days=1)
j=tmpdate.weekday()
NotifyUser(1,'还原库'+global_sqlserver_db+'成功')
logging.info('还原库'+global_sqlserver_db+'成功')
return 1
if __name__ == "__main__":
doRestoreDB(3<2)
logging.shutdown()
分享到:
- 2007-01-15 12:55
- 浏览 1539
- 评论(0)
- 论坛回复 / 浏览 (0 / 2019)
- 查看更多
相关推荐
LNH_MySQL 27-更多MySQL数据库增量恢复大讨论及大总结.mp4
通过已经备份的数据库文件对数据库进行恢复测试,并输出测试报告,本次测试对数据库的全量及增量备份进行恢复,并在恢复完成后验证数据是否正常,恢复时间是否与备份时间相一致。...
Java实现Mysql数据库完整、增量备份与还原
LNH_MySQL 23-企业场景数据库需要增量恢复的条件分析.mp4
NetVault SQL Server数据库异机增量恢复
Oracle数据库逻辑增量备份之exp/imp 1 一、实现需求 1 二、逻辑备份恢复工具exp/imp 2 1、逻辑备份原理 2 2、exp语法和参数 2 3、imp语法和参数 3 三、exp逻辑备份 4 1、exp表模式备份 4 2、exp用户模式备份 5 3、...
db2数据库恢复 增量备份和恢复 版本恢复
我们需要通过全备的数据文件,以及增量的binlog文件进行数据恢复 二、主要思想 利用全备的sql文件中记录的CHANGE MASTER语句,binlog文件及其位置点信息,找出binlog文件增量的部分 用mysqlbinlog命令将...
07-更多MySQL数据库增量恢复大讨论及大总结 08-MySQL增量恢复小结及核心恢复思想 09-课后自学能力作业-务必完成并分享讲解 10-内部学员技术大会分享课表说明 第九部-老男孩MySQL服务日志详细介绍及增量恢复命令实践...
Oracle RMAN增量备份恢复测试记录
超经典的Oracle rman增量备份恢复策略,DBA工作过程中,在对数据库进行备份的时候可以参考此文档
1.1 备份时间 凌晨2点(02:00:00) 1.2 备份操作 注: 增量备份模式为cumulative(累积模式) 1. 周日:0级增量备份 ... 保留策略为保存14天,也就是能恢复到14天内的任何时间点 并且实现本地备份+异地备份
本软件可以个人使用,也可以用于服务器进行实时或者定时数据增量备份,功能强大,非常使用,如果能结合server u,可以异地进行数据备份,针对于个人和小企业都很实用。保证实用
DB2数据库离线和在线全备、增量备份及恢复的具体操作步骤.
工程师通过技术手段读取将数据都恢复为可以读的数据,数据恢复不是靠一两种软件就可以完成,往往需要数个工程师靠经验不同的方式才能恢复数据,当然数据库恢复还包括各种操作系统:除普通的WINDOWS外,还有Unix、...
通过已经备份的数据库文件对数据库进行恢复测试,并输出测试报告,本次测试对数据库的全量及增量备份进行恢复,并在恢复完成后验证数据是否正常,恢复时间是否与备份时间相一致。...
假定数据库的备份任务作如下安排: ---- 星期一:完全备份(A) ---- 星期二:增量备份(B) ---- 星期三:增量备份(C) ---- 星期四:增量备份(D) ---- 星期五:累计备份(E) ---- 星期六:增量备份(F) ...
2.4 数据库增量备份 6 2.5 查看已备份的数据 7 2.6 现阶段数据库备份脚本展示 7 3.数据库恢复操作 7 3.1 恢复前准备 7 3.2 恢复SPFILE文件 8 3.3 恢复控制文件 8 3.4 恢复数据文件 9 3.5 以RESETLOGS方式打开数据库 ...
• 本案例适用于人为 SQL 语句造成的误操作或者没有主从...• 先恢复全量,然后把全备时刻点以后的增量日志,按顺序恢复成 SQL 文件,然后把文件中有问题的 SQL 语句删除(也可通过时间和位置点),再恢复到数据库。
DB2数据库离线和在线全备、增量备份及恢复的具体操作步骤: 1、离线全备份 2、在线备份 3、在线增量备份 4、恢复数据库 5、说明