forked from ama31337/serverbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipDbFiles.py
More file actions
71 lines (59 loc) · 2.24 KB
/
zipDbFiles.py
File metadata and controls
71 lines (59 loc) · 2.24 KB
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
import subprocess
from subprocess import call, PIPE, run
import datetime, requests, config
from datetime import datetime as dt
# current directory
directory = "/home/tommygood/serverbot/"
# db directory
db_directory = directory + "db"
# backup directory, make sure this directory exist
backup_directory = directory + "db_backups/"
# record the monitor result in log
log_path = directory + "monitor.log"
# chat id
chat_id = config.tg
# bot
bot_token = config.BotAPIKey
def main() :
result = str(compressFile())
logResult(result)
sendTelegram(result) # send to telegram chat room
def sendTelegram(result) :
send_msg_url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={result}"
res = requests.get(send_msg_url) # this sends the message
# create new db directory
def createNewDb() :
result = run(["mkdir", db_directory], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(result)
result = run(["touch", db_directory + "/db.log"], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(result)
result = run(["touch", db_directory + "/dockerContainer.dat"], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(result)
with open(db_directory + "/dockerContainer.dat", 'w') as file:
file.write("[]")
file.close()
# compress the .wpress file
def compressFile() :
file_path = db_directory
tar_path = backup_directory + str(dt.now().date()) + ".tar.gz"
# compress file to tar file
result = run(["tar", "zcvf", tar_path, "-C", file_path, "."], stdout=PIPE, stderr=PIPE, universal_newlines=True)
result= str(result)
# compress failed
if not ("CompletedProcess" in result and "stderr=''" in result) :
print("Compress failed")
print(result)
# compress successfully, remove the original file
else :
result = run(["rm", "-r", file_path], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print("Compress successfully")
print(result)
createNewDb() # create new db directory
return result
def logResult(result) :
# record the odd directory
text_file = open(log_path, "a")
# write into log
text_file.write(str(datetime.datetime.now()) + " " + result + "\n")
text_file.close()
main()