Guest User

Untitled

a guest
Sep 8th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import schedule
  2. import sqlite3
  3. import subprocess
  4. import os
  5. import time
  6. import re
  7.  
  8. sdrive = "/mnt/source"
  9. ddrive = "rclone_enc:/"
  10.  
  11. servicefile = "XXXXXXXXX.iam.gserviceaccount.com.json"
  12. service = 1
  13.  
  14. con = sqlite3.connect('./rclone.db', timeout=20)
  15.  
  16. cursorObj = con.cursor()
  17. cursorObj.execute("CREATE TABLE IF NOT EXISTS serviceaccount(id integer PRIMARY KEY, current integer)")
  18. con.commit()
  19.  
  20. cursorObj = con.cursor()
  21. cursorObj.execute('SELECT * FROM serviceaccount WHERE id = 1')
  22. rows = cursorObj.fetchall()
  23. if len(rows) == 0:
  24. cursorObj.execute("INSERT INTO serviceaccount (current) VALUES (1);")
  25. service = 1
  26. else:
  27. service = int(rows[0][1])
  28.  
  29.  
  30.  
  31. def job():
  32. print("starting")
  33. global service, servicefile
  34. serviceaccount = "--drive-service-account-file=/home/gc2/keys/service" + str(service) + "@" + servicefile
  35. print( serviceaccount )
  36. print( "found enough files, syncing")
  37. process = subprocess.Popen(['rclone','sync', sdrive , ddrive ,'--drive-server-side-across-configs','--tpslimit=30','--transfers=4','--checkers=10', '--fast-list',
  38. '-c','--verbose=1','--stats=60s' , '--drive-stop-on-upload-limit','--max-backlog','10000','--exclude','**/.backups/**', serviceaccount],
  39. stdout=subprocess.PIPE,
  40. universal_newlines=True,
  41. shell=False,
  42. stderr=subprocess.STDOUT
  43. )
  44.  
  45. while True:
  46. output = process.stdout.readline()
  47. if output.strip() != "":
  48. print(output.strip())
  49. return_code = process.poll()
  50. if return_code is 7:
  51. service += 1
  52. if service is 101:
  53. #exit()
  54. service = 1
  55. con = sqlite3.connect('./rclone.db', timeout=20)
  56. cursorObj = con.cursor()
  57. cursorObj.execute('UPDATE serviceaccount SET current = ' + str(service) + ' WHERE id = 1;')
  58. con.commit()
  59. con.close()
  60. schedule.every(1).minutes.do(job)
  61. return schedule.CancelJob
  62.  
  63.  
  64. if return_code is not None:
  65. if return_code is 0:
  66. schedule.every(120).minutes.do(job)
  67. print("** job done")
  68. return schedule.CancelJob
  69.  
  70. print('RETURN CODE', return_code)
  71. # Process has finished, read rest of the output
  72. for output in process.stdout.readlines():
  73. print(output.strip())
  74. break
  75. schedule.every(120).minutes.do(job)
  76. return schedule.CancelJob
  77.  
  78.  
  79. schedule.every(1).seconds.do(job)
  80.  
  81. while True:
  82. schedule.run_pending()
  83. time.sleep(1)
  84.  
Advertisement
Add Comment
Please, Sign In to add comment