-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup.py
130 lines (107 loc) · 4.06 KB
/
setup.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import platform
import re
import shutil
import subprocess
import time
import zipfile
from datetime import datetime
from threading import Thread
import discord
import ujson as json
from discord.ext import commands
import sqlbullshit
filepath = os.path.abspath(os.path.dirname(__file__))
day = 60 * 60 * 24
std = ""
botobj = None
def setbot(bot):
global botobj
botobj = bot
def in_wsl() -> bool:
if os.name == "nt":
return True
else:
return "microsoft-standard" in platform.uname().release
def updatedb(added: str):
sql = sqlbullshit.sql(f"{filepath}/data.db", "user")
collums = sql.collums()
if added not in collums:
sql.addcollum(added, "TEXT")
def compresslogs():
"""Find all logs in the /commandlogs directory more than 3 days old and use subprocess.run(["zpaq", "add", f"./{t}.zpaq", f"./{t}.zip"],
stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) on it"""
if in_wsl() is False:
while True:
dt = datetime.now()
t = dt.strftime("%d-%b-%y_%H-%M-%S")
# Get a list of all the logs in the /commandlogs directory
logs = os.listdir(f"{filepath}/commandlogs")
# For each log in the logs directory
for log in logs:
# If the log is more than 3 days old
if (
os.path.getmtime(f"{filepath}/commandlogs/{log}")
< time.time() - 3 * day
):
# If the log is already paqed, leave it
if log.endswith(".zpaq"):
continue
# ZPAQ it with subprocess
subprocess.run(
["zpaq", "add", f"./{log}.zpaq", f"./{log}"],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
)
# Move the file to the backups folder
shutil.move(
f"{filepath}/{t}.zpaq", fr"/{filepath}/commandlogs/{t}.zpaq"
)
time.sleep(day / 2)
def backup():
# Run the backup loop only if we are on linux
isbeta = in_wsl()
if isbeta is False:
while True:
# Read the database file as binary
with open(f"{filepath}/data.db", "rb") as db:
# Save the raw datetime
dt = datetime.now()
# Convert it into something we can read
t = dt.strftime("%d-%b-%y_%H-%M-%S")
# Convert the copied db to a zip file
with zipfile.ZipFile(fr"{filepath}/{t}.zip", mode="x") as archive:
archive.writestr(f"{t}.db", db.read())
# Attempt to compress the zip file with ZPAQ
try:
# ZPAQ it with subprocess
subprocess.run(
["zpaq", "add", f"./{t}.zpaq", f"./{t}.zip"],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
)
# Delete the zip file
subprocess.run(["rm", f"./{t}.zip"])
# Move the file to the backups folder
shutil.move(
f"{filepath}/{t}.zpaq", fr"/{filepath}/backups/{t}.zpaq"
)
# If it doesn't work, keep the zip file
except OSError:
shutil.move(f"{filepath}/{t}.zip",
fr"/{filepath}/backups/{t}.zip")
time.sleep(day)
print("Backup loop not run on the non-linux system")
def isin(regobject: re.Pattern, string: str) -> bool:
"""Check if a string is in a regex object"""
if len(regobject.findall(string)) > 0:
return True
return False
async def begin():
threads = [
Thread(target=backup, name="Database backup"),
Thread(target=compresslogs, name="Log compression"),
Thread(target=updatedb, name="Database collumn update", args=("inv",)),
]
for x in threads:
x.start()