公司的交易系统bs架构,通过http方式访问备份数据库。开发公司不肯合作的态度,所以每一次必须人工的去登陆网站是手动备份。 每天安排6次备份,这种对于一个程序开发者来讲的话真是一种痛苦和侮辱。 今天空下来就用python开始编写自动备份方案: 1.http自动登录 2.wget断点下载 3.7zip压缩 4.网盘 everbox的同步到服务器 与http server的交互信息用wireshark抓包分析,还蛮灵光的 python代码:
1 # -*- coding:utf-8 -*- 2 #auto backup system database 3 #自动登录,http下载,压缩之后通过网盘同步到服务器 4 5 import httplib, urllib,sys,os,re,datetime,time,os.path,gzip 6 7 host="192.168.1.106" 8 sys_user='anyuser' 9 sys_pass='anypass' 10 11 html = '''<body><h1>Object Moved</h1>This object may be found <a HREF="shuju.asp?err= 12 ok!&dizhi=../data_backup/zhk0432011-1-26.7055475.mdb">here</a>.</body> 13 ''' 14 15 16 tasklet=[] 17 backupTimes= 6# in day 每天备份次数 18 19 20 21 def backup(outputfile): 22 params = urllib.urlencode({'login_name':sys_user, 'login_pass': sys_pass, 'submit.x': 9,'submit.y':9}) 23 conn = httplib.HTTPConnection(host) 24 headers = {"Content-type": "application/x-www-form-urlencoded", 25 "Accept": "text/plain"} 26 conn.request("POST","/asp/huiyuan/login_check_gl.asp",params,headers) 27 resp = conn.getresponse() 28 #print resp.status,resp.reason 29 #print resp.getheaders() 30 cookie = resp.getheader('set-cookie') 31 #print resp.read() 32 conn.close() 33 34 #print 'retry GET /' 35 conn = httplib.HTTPConnection(host) 36 headers = {"Content-type": "application/x-www-form-urlencoded", 37 "Accept": "text/plain",'Cookie':cookie} 38 39 conn.request("GET","/asp/admin/login_check001.asp",'',headers) 40 resp = conn.getresponse() 41 42 #sys.exit(0) 43 conn = httplib.HTTPConnection(host) 44 headers = {"Content-type": "application/x-www-form-urlencoded", 45 "Accept": "text/plain",'Cookie':cookie} 46 47 conn.request("GET","/asp/admin/backup.asp",'',headers) 48 resp = conn.getresponse() 49 #print resp.status,resp.reason 50 html= resp.read() 51 52 m = re.search(".*?/data_backup/(.*?\.mdb).*",html) 53 backupfile = '' 54 if len(m.groups()): 55 backupfile = m.groups()[0] 56 print backupfile 57 else: 58 print 'backup access failed!' 59 return False 60 # -o wget.log 61 downloadurl= "http://%s/asp/data_backup/%s -O %s "%(host,backupfile,outputfile) 62 #print 'try get %s '%downloadurl 63 cmd = "wget -c -t 0 %s"%downloadurl 64 print cmd 65 os.system(cmd) 66 return True 67 68 firsttime = datetime.datetime.now() 69 if not os.path.exists('./backup'): 70 os.mkdir('backup') 71 72 if not os.path.exists('./sync'): 73 os.mkdir('sync') 74 75 while True: 76 now = datetime.datetime.now() 77 #filename = "backup/%s_%s-%s_%s_%s_%s.bak"%(now.year,now.month,now.day,now.hour,now.minute,now.second) 78 sync_hour= int(now.hour/int(24/backupTimes)) * int(24/backupTimes) 79 filename = "%s_%s-%s_%s_%s_%s.bak"%(now.year,now.month,now.day,sync_hour,0,0) 80 try: 81 if not os.path.exists("backup/"+filename): 82 backup("backup/"+filename) 83 cmd = "7zip\\7z.exe a -t7z sync\\%s.7z backup\\%s"%(filename,filename) 84 print cmd 85 os.system(cmd) 86 except: 87 pass 88 time.sleep(10) # 89 90 91 92
|