postgresql_backup.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from .flightDB import flightDB
  2. import datetime
  3. from unitls.LogerinTxt import app_logger
  4. from unitls.StaticDataclass import get_dic
  5. def postgresql_bakup(bakcup:flightDB,main:flightDB,type):
  6. print(datetime.datetime.now(), "[主从同步]开始同步数据库")
  7. logintoken_dic=['id','username','token']
  8. loginsts_dic=['id','sts']
  9. logintable_dic=['id',"登录名","密码","权限","使用人"]
  10. today = datetime.datetime.now().strftime('%Y%m%d')
  11. yesterday=(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y%m%d')
  12. tomorrow=(datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%Y%m%d')
  13. table_colmuns={"peopleschedule%s"%today:get_dic('peopleScheduleLabel'),"peopleschedule%s"%yesterday:get_dic('peopleScheduleLabel'),"peopleschedule%s"%tomorrow:get_dic('peopleScheduleLabel'),
  14. 'risktable':get_dic('RiskLabel'),'workerinfo':get_dic('workerinfoLabel'),'pglist%s'%today:get_dic('pglistLabel'),'pglist%s'%yesterday:get_dic('pglistLabel'),
  15. 'logs':get_dic('logsLabel'),'taskauto':get_dic('taskLabel'),'calllist':get_dic('CalllistLabel'),
  16. 'flightsts%s'%today:get_dic('flightstsLabel'),'flightsts%s'%yesterday:get_dic('flightstsLabel'),
  17. 'logintoken':logintoken_dic,'loginsts':loginsts_dic,'logintable':logintable_dic,
  18. "TaskFlightinfo%s"%today:get_dic('TaskflightinfoLabel'),"TaskFlightinfo%s"%yesterday:get_dic('TaskflightinfoLabel'),"TaskFlightinfo%s"%tomorrow:get_dic('TaskflightinfoLabel'),
  19. "flightsearch%s"%today:get_dic('flightsearchLabel'),"flightsearch%s"%yesterday:get_dic('flightsearchLabel'),"flightsearch%s"%tomorrow:get_dic('flightsearchLabel'),
  20. "sortFlight%s"%today:get_dic('sortLabel'),"sortFlight%s"%yesterday:get_dic('sortLabel'),"sortFlight%s"%tomorrow:get_dic('sortLabel'),"display":get_dic('displayLabel'),
  21. }
  22. if type == 1:
  23. dbtables=["peopleschedule%s"%today,"peopleschedule%s"%yesterday,"peopleschedule%s"%tomorrow,'risktable',
  24. 'workerinfo','pglist%s'%today,'pglist%s'%yesterday,'taskauto','calllist',
  25. 'flightsts%s'%today,'flightsts%s'%yesterday,
  26. "TaskFlightinfo%s"%today,"TaskFlightinfo%s"%yesterday,"TaskFlightinfo%s"%tomorrow,
  27. "flightsearch%s"%today,"flightsearch%s"%yesterday,"flightsearch%s"%tomorrow,
  28. "sortFlight%s"%today,"sortFlight%s"%yesterday,"sortFlight%s"%tomorrow,"display"]#logs太大无法同步
  29. elif type == 2:
  30. dbtables=['logintable']
  31. elif type == 3:
  32. dbtables=['logintoken','loginsts']
  33. try:
  34. tablesok=""
  35. tablenum=0
  36. for dbtable in dbtables:
  37. try:
  38. if type == 1:
  39. Dic = {}
  40. primaryKey2 = 'text'
  41. for key in list(table_colmuns[dbtable])[1:]:
  42. Dic[key] = 'text'
  43. main.initTable(dbtable, table_colmuns[dbtable][0], primaryKey2, Dic)
  44. all_source_data=bakcup.getAlldata(dbtable)
  45. if len(all_source_data)!=0:
  46. main.lazydeleteTable(dbtable)
  47. if dbtable=="display":
  48. dispaly_res=main.getSingledata("A", 'display')
  49. if dispaly_res == None or len(dispaly_res) == 0:
  50. main.insertData('display', {'ID': 1, 'A': '{}', 'B': '{}'})
  51. main.insertData('display', {'ID': 2, 'A': '{}', 'B': '{}'})
  52. main.insertData('display', {'ID': 3, 'A': '{}', 'B': '{}'})
  53. for row in all_source_data:
  54. newdic = {"A": '"{}"'.format(row[1]),"B": '"{}"'.format(row[2])}
  55. main.lazyUpdateItem(dbtable, newdic, "ID = '%s'"%row[0])
  56. #online.FunctionCommit()
  57. else:
  58. for row in all_source_data:
  59. main.lazyInsertData2(dbtable,', '.join(table_colmuns[dbtable]),row)
  60. main.FunctionCommit()
  61. tablesok=tablesok+dbtable+"/"
  62. tablenum+=1
  63. except Exception as e:
  64. app_logger.log_error(e)
  65. continue
  66. print(datetime.datetime.now(), "[主从同步]完成数据库同步(%s[%s])"%(tablesok,tablenum))
  67. return "ok"
  68. except Exception as e:
  69. app_logger.log_error(e)
  70. return "fail"