Spaces:
Paused
Paused
Space Bot commited on
Commit ·
b097bf6
1
Parent(s): d24cb01
use timestamp to avoid loops
Browse files- backend/scripts/backup.py +79 -13
backend/scripts/backup.py
CHANGED
|
@@ -1,13 +1,54 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def backup_db():
|
| 7 |
passphrase = os.environ.get("BACKUP_PASSPHRASE")
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
space_id = os.environ.get("SPACE_ID")
|
| 10 |
-
|
| 11 |
if not passphrase:
|
| 12 |
raise ValueError("BACKUP_PASSPHRASE is not set.")
|
| 13 |
if not hf_token:
|
|
@@ -15,30 +56,55 @@ def backup_db():
|
|
| 15 |
if not space_id:
|
| 16 |
raise ValueError("SPACE_ID is not set (or define repo_id manually).")
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
print("Encrypting database with GPG...")
|
| 20 |
encrypt_cmd = [
|
| 21 |
"gpg", "--batch", "--yes", "--passphrase", passphrase,
|
| 22 |
"-c", "--cipher-algo", "AES256",
|
| 23 |
-
"-o", "db_backup/webui.db.gpg"
|
| 24 |
-
"data/webui.db"
|
| 25 |
]
|
| 26 |
subprocess.run(encrypt_cmd, check=True)
|
| 27 |
-
print("Database encrypted successfully to
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Upload using huggingface_hub
|
| 30 |
print("Uploading to Hugging Face Spaces via huggingface_hub...")
|
| 31 |
api = HfApi()
|
| 32 |
repo_id = space_id
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
repo_id=repo_id,
|
| 37 |
repo_type="space",
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
-
print("DB backup uploaded successfully!")
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
backup_db()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
+
import datetime
|
| 5 |
+
from huggingface_hub import HfApi, hf_hub_download, hf_hub_url
|
| 6 |
+
|
| 7 |
+
TIMESTAMP_FILE_PATH = "db_backup/last_backup_time.txt"
|
| 8 |
+
DB_GPG_PATH = "db_backup/webui.db.gpg"
|
| 9 |
+
DB_FILE_PATH = "data/webui.db"
|
| 10 |
+
|
| 11 |
+
def get_last_backup_time(repo_id, hf_token):
|
| 12 |
+
"""
|
| 13 |
+
Attempt to download and parse the last_backup_time.txt file
|
| 14 |
+
from the HF Space. If it doesn't exist, return None.
|
| 15 |
+
"""
|
| 16 |
+
api = HfApi()
|
| 17 |
+
try:
|
| 18 |
+
temp_file = hf_hub_download(
|
| 19 |
+
repo_id=repo_id,
|
| 20 |
+
repo_type="space",
|
| 21 |
+
filename=TIMESTAMP_FILE_PATH, # "db_backup/last_backup_time.txt"
|
| 22 |
+
token=hf_token
|
| 23 |
+
)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
# File might not exist or there's some other error
|
| 26 |
+
print(f"Could not download {TIMESTAMP_FILE_PATH}: {e}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# If we downloaded successfully, read the timestamp
|
| 30 |
+
try:
|
| 31 |
+
with open(temp_file, "r", encoding="utf-8") as f:
|
| 32 |
+
timestamp_str = f.read().strip()
|
| 33 |
+
last_backup_dt = datetime.datetime.fromisoformat(timestamp_str)
|
| 34 |
+
return last_backup_dt
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error parsing timestamp from {TIMESTAMP_FILE_PATH}: {e}")
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
def save_timestamp_locally():
|
| 40 |
+
"""
|
| 41 |
+
Save the current UTC time to db_backup/last_backup_time.txt (locally).
|
| 42 |
+
"""
|
| 43 |
+
now = datetime.datetime.utcnow()
|
| 44 |
+
os.makedirs("db_backup", exist_ok=True)
|
| 45 |
+
with open(TIMESTAMP_FILE_PATH, "w", encoding="utf-8") as f:
|
| 46 |
+
f.write(now.isoformat())
|
| 47 |
|
| 48 |
def backup_db():
|
| 49 |
passphrase = os.environ.get("BACKUP_PASSPHRASE")
|
| 50 |
hf_token = os.environ.get("HF_TOKEN")
|
| 51 |
space_id = os.environ.get("SPACE_ID")
|
|
|
|
| 52 |
if not passphrase:
|
| 53 |
raise ValueError("BACKUP_PASSPHRASE is not set.")
|
| 54 |
if not hf_token:
|
|
|
|
| 56 |
if not space_id:
|
| 57 |
raise ValueError("SPACE_ID is not set (or define repo_id manually).")
|
| 58 |
|
| 59 |
+
# 1) Check if last backup was too recent
|
| 60 |
+
threshold_hours = 12
|
| 61 |
+
last_backup_dt = get_last_backup_time(space_id, hf_token)
|
| 62 |
+
if last_backup_dt is not None:
|
| 63 |
+
now = datetime.datetime.utcnow()
|
| 64 |
+
elapsed = now - last_backup_dt
|
| 65 |
+
if elapsed.total_seconds() < threshold_hours * 3600:
|
| 66 |
+
print(f"Last backup was only {elapsed.total_seconds()/3600:.2f} hours ago.")
|
| 67 |
+
print(f"Skipping backup to avoid rebuild loop.")
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
+
# 2) Encrypt using GPG
|
| 71 |
print("Encrypting database with GPG...")
|
| 72 |
encrypt_cmd = [
|
| 73 |
"gpg", "--batch", "--yes", "--passphrase", passphrase,
|
| 74 |
"-c", "--cipher-algo", "AES256",
|
| 75 |
+
"-o", DB_GPG_PATH, # "db_backup/webui.db.gpg"
|
| 76 |
+
DB_FILE_PATH # "data/webui.db"
|
| 77 |
]
|
| 78 |
subprocess.run(encrypt_cmd, check=True)
|
| 79 |
+
print(f"Database encrypted successfully to {DB_GPG_PATH}")
|
| 80 |
+
|
| 81 |
+
# 3) Update the timestamp file locally
|
| 82 |
+
save_timestamp_locally()
|
| 83 |
|
| 84 |
+
# 4) Upload using huggingface_hub
|
| 85 |
print("Uploading to Hugging Face Spaces via huggingface_hub...")
|
| 86 |
api = HfApi()
|
| 87 |
repo_id = space_id
|
| 88 |
+
|
| 89 |
+
# uploads both "db_backup/webui.db.gpg" and "db_backup/last_backup_time.txt"
|
| 90 |
+
files_to_upload = [
|
| 91 |
+
(DB_GPG_PATH, DB_GPG_PATH),
|
| 92 |
+
(TIMESTAMP_FILE_PATH, TIMESTAMP_FILE_PATH)
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
api.create_commit(
|
| 96 |
repo_id=repo_id,
|
| 97 |
repo_type="space",
|
| 98 |
+
operations=[
|
| 99 |
+
# Each operation: (operation, local_path, remote_path)
|
| 100 |
+
# "add_or_update" is the operation
|
| 101 |
+
{"op": "add_or_update", "path_in_repo": remote, "path_or_fileobj": local}
|
| 102 |
+
for (local, remote) in files_to_upload
|
| 103 |
+
],
|
| 104 |
+
commit_message="Update encrypted database backup + timestamp",
|
| 105 |
+
token=hf_token
|
| 106 |
)
|
| 107 |
+
print("DB backup + timestamp uploaded successfully!")
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
backup_db()
|