forked from CoccoBlu/forme-website-landing-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_db.py
36 lines (27 loc) · 981 Bytes
/
backup_db.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
import time
import os
from datetime import datetime
from shutil import copyfile
BACKUP_FREQ = 30
MAX_BACKUPS = 5
BACKUPS_DIR = "backups"
BACKUP_PREFIX = "database"
DIR_SEP = "\\" if os.name == "nt" else "/"
if not os.path.exists(BACKUPS_DIR):
os.mkdir(BACKUPS_DIR)
print("Ctrl-C to kill.")
def extract_time(backup: str) -> datetime:
name, extension = backup.split(".")
assert extension == "db"
prefix, creation_time = name.split("_", 1)
assert prefix == BACKUP_PREFIX
return datetime.strptime(creation_time, "%Y-%m-%d_%H-%M-%S")
while True:
# Delete the old backups
backups = [(extract_time(backup), backup) for backup in os.listdir(BACKUPS_DIR)]
backups.sort()
for backup in backups[:1-MAX_BACKUPS]:
os.remove(BACKUPS_DIR+DIR_SEP+backup[1])
# Save the new backup
copyfile("database.db", BACKUPS_DIR+DIR_SEP+BACKUP_PREFIX+"_"+datetime.strftime(datetime.now(), "%Y-%m-%d_%H-%M-%S")+".db")
time.sleep(BACKUP_FREQ)