forked from natsfr/ldogecoin_bchain_backuper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
89 lines (70 loc) · 2.04 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from zipfile import ZipFile
from datetime import datetime
from datetime import timedelta
from datetime import date
from pathlib import Path
import os
import threading, time, signal
import shutil
import configparser
#Import config file
current_directory = os.getcwd()
config = configparser.ConfigParser()
config.read('{}/backup.conf'.format(current_directory))
print("Read config")
class ProgramKilled(Exception):
pass
def backup():
myHome = Path(config["PATHS"]["myHome"])
now = datetime.now()
print(now)
print(myHome)
os.chdir(str(myHome))
strZipName = "LDOGE_Snapshot_{}.zip".format(str(datetime.now().isoformat()))
webFolder = Path(config["PATHS"]["webFolder"])
backFile = webFolder / 'tmp' / strZipName
print(backFile)
fileList = [ 'blk0001.dat' , 'peers.dat', 'database', 'txleveldb' ]
zipObj = ZipFile(backFile, 'w')
for file in fileList:
if(os.path.isdir(file)):
for root, dirs, files in os.walk(file):
for fcur in files:
fname = root + '/' + fcur
zipObj.write(fname)
elif(os.path.isfile(file)):
zipObj.write(file)
finalFolder = config["PATHS"]["savingFolder"]
shutil.move(str(backFile), str(finalFolder))
def signal_handler(signum, frame):
raise ProgramKilled
class Job(threading.Thread):
def __init__(self, interval, execute, *args, **kwargs):
threading.Thread.__init__(self)
self.daemon = False
self.stopped = threading.Event()
self.interval = interval
self.execute = execute
self.args = args
self.kwargs = kwargs
def stop(self):
self.stopped.set()
self.join()
def run(self):
while not self.stopped.wait(self.interval.total_seconds()):
print("Execute backup")
self.execute(*self.args, **self.kwargs)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
#job = Job(interval=timedelta(days=1), execute=backup)
# CHANGE DELAY HERE
job = Job(interval=timedelta(hours=12), execute=backup)
job.start()
while True:
try:
time.sleep(1)
except ProgramKilled:
print("LDOGE Backup killed")
job.stop()
break