postgresql_backup.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "sortFlight2%s"%today:get_dic('sortLabel'),"sortFlight2%s"%yesterday:get_dic('sortLabel'),"sortFlight2%s"%tomorrow:get_dic('sortLabel'),
  22. }
  23. if type == 1:
  24. dbtables=["peopleschedule%s"%today,"peopleschedule%s"%yesterday,"peopleschedule%s"%tomorrow,'risktable',
  25. 'workerinfo','pglist%s'%today,'pglist%s'%yesterday,'taskauto','calllist',
  26. 'flightsts%s'%today,'flightsts%s'%yesterday,
  27. "TaskFlightinfo%s"%today,"TaskFlightinfo%s"%yesterday,"TaskFlightinfo%s"%tomorrow,
  28. "flightsearch%s"%today,"flightsearch%s"%yesterday,"flightsearch%s"%tomorrow,
  29. "sortFlight%s"%today,"sortFlight%s"%yesterday,"sortFlight%s"%tomorrow,
  30. "sortFlight2%s"%today,"sortFlight2%s"%yesterday,"sortFlight2%s"%tomorrow,
  31. "display"]#logs太大无法同步
  32. elif type == 2:
  33. dbtables=['logintable']
  34. elif type == 3:
  35. dbtables=['logintoken','loginsts']
  36. try:
  37. tablesok=""
  38. tablenum=0
  39. for dbtable in dbtables:
  40. try:
  41. if type == 1:
  42. Dic = {}
  43. primaryKey2 = 'text'
  44. for key in list(table_colmuns[dbtable])[1:]:
  45. Dic[key] = 'text'
  46. main.initTable(dbtable, table_colmuns[dbtable][0], primaryKey2, Dic)
  47. all_source_data=bakcup.getAlldata(dbtable)
  48. if len(all_source_data)!=0:
  49. main.lazydeleteTable(dbtable)
  50. if dbtable=="display":
  51. dispaly_res=main.getSingledata("A", 'display')
  52. if dispaly_res == None or len(dispaly_res) == 0:
  53. main.insertData('display', {'ID': 1, 'A': '{}', 'B': '{}'})
  54. main.insertData('display', {'ID': 2, 'A': '{}', 'B': '{}'})
  55. main.insertData('display', {'ID': 3, 'A': '{}', 'B': '{}'})
  56. for row in all_source_data:
  57. newdic = {"A": '"{}"'.format(row[1]),"B": '"{}"'.format(row[2])}
  58. main.lazyUpdateItem(dbtable, newdic, "ID = '%s'"%row[0])
  59. #online.FunctionCommit()
  60. else:
  61. for row in all_source_data:
  62. main.lazyInsertData2(dbtable,', '.join(table_colmuns[dbtable]),row)
  63. main.FunctionCommit()
  64. tablesok=tablesok+dbtable+"/"
  65. tablenum+=1
  66. except Exception as e:
  67. app_logger.log_error(e)
  68. continue
  69. print(datetime.datetime.now(), "[主从同步]完成数据库同步(%s[%s])"%(tablesok,tablenum))
  70. return "ok"
  71. except Exception as e:
  72. app_logger.log_error(e)
  73. return "fail"