postgresql_backup.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if type == 1:
  19. dbtables=["peopleschedule%s"%today,"peopleschedule%s"%yesterday,"peopleschedule%s"%tomorrow,'risktable',
  20. 'workerinfo','pglist%s'%today,'pglist%s'%yesterday,'taskauto','calllist',
  21. 'flightsts%s'%today,'flightsts%s'%yesterday]#logs太大无法同步
  22. elif type == 2:
  23. dbtables=['logintable']
  24. elif type == 3:
  25. dbtables=['logintoken','loginsts']
  26. try:
  27. tablesok=""
  28. tablenum=0
  29. for dbtable in dbtables:
  30. try:
  31. if type == 1:
  32. Dic = {}
  33. primaryKey2 = 'text'
  34. for key in list(table_colmuns[dbtable])[1:]:
  35. Dic[key] = 'text'
  36. main.initTable(dbtable, table_colmuns[dbtable][0], primaryKey2, Dic)
  37. all_source_data=bakcup.getAlldata(dbtable)
  38. if len(all_source_data)!=0:
  39. main.lazydeleteTable(dbtable)
  40. for row in all_source_data:
  41. main.lazyInsertData2(dbtable,', '.join(table_colmuns[dbtable]),row)
  42. main.FunctionCommit()
  43. tablesok=tablesok+dbtable+"/"
  44. tablenum+=1
  45. except Exception as e:
  46. app_logger.log_error(e)
  47. continue
  48. print(datetime.datetime.now(), "[主从同步]完成数据库同步(%s[%s])"%(tablesok,tablenum))
  49. return "ok"
  50. except Exception as e:
  51. app_logger.log_error(e)
  52. return "fail"