/
server.py
63 lines (54 loc) · 1.5 KB
/
server.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
import threading
import sys
import lz4
import os
import tarfile
import datetime
import time
from apscheduler.scheduler import Scheduler
path = "/home/fortuna/test"
path2 = "/home/fortuna/directorio"
def compress(file):
data = open(file,"r+b")
if data.name.endswith('.tar'):
compressed_data = open(data.name + ".lz4","w")
compressed_data.write(lz4.compress(data.read()))
else:
print "The file type is not the expected."
def decompress(file):
data = open(file,"r+b")
if data.name.endswith('.lz4'):
decompressed_data = open(data.name[:-4],"w")
decompressed_data.write(lz4.decompress(data.read()))
else:
print "The file type is not the expected."
def restore():
dirs = os.listdir ( path )
fileList = list()
for file in dirs:
fileList.append(file)
fileList.sort()
print fileList
selectedFile = fileList[input("Select a file to restore (0 - " + str(len(fileList) - 1) + "): ")]
decompress(path + "/" + selectedFile)
def make_tar():
dateAndTime = time.strftime("%d-%m-%Y") + " [" + time.strftime("%X")+ "].tar"
tar = tarfile.open(str(dateAndTime), "w")
tar.add ( path2 )
tar.close()
return tar.name
def backup():
tarFile = make_tar()
compress(tarFile)
os.remove(tarFile)
try:
if sys.argv[1] == "restore":
restore()
if sys.argv[1] == "backup":
backup()
if sys.argv[1] == "help":
print "Avaible inputs: 'restore', 'backup'."
else:
print "The input argument is not valid."
except:
print "There is no argument, type 'help' to get more information."