Create backup.py
Browse files- ai-bot/core/backup.py +27 -0
ai-bot/core/backup.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from cryptography.fernet import Fernet
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
def create_backup(data: dict):
|
| 7 |
+
key = Fernet.generate_key()
|
| 8 |
+
cipher = Fernet(key)
|
| 9 |
+
|
| 10 |
+
backup_data = json.dumps(data).encode()
|
| 11 |
+
encrypted = cipher.encrypt(backup_data)
|
| 12 |
+
|
| 13 |
+
timestamp = str(datetime.datetime.now()).replace(" ", "_")
|
| 14 |
+
|
| 15 |
+
backup_file = f"backup_{timestamp}.enc"
|
| 16 |
+
key_file = f"key_{timestamp}.key"
|
| 17 |
+
|
| 18 |
+
with open(backup_file, "wb") as f:
|
| 19 |
+
f.write(encrypted)
|
| 20 |
+
|
| 21 |
+
with open(key_file, "wb") as f:
|
| 22 |
+
f.write(key)
|
| 23 |
+
|
| 24 |
+
return {
|
| 25 |
+
"backup_file": backup_file,
|
| 26 |
+
"key_file": key_file
|
| 27 |
+
}
|