Space Bot commited on
Commit
ae309aa
·
1 Parent(s): bcbce13

python backup script

Browse files
Dockerfile CHANGED
@@ -133,6 +133,7 @@ RUN if [ "$USE_OLLAMA" = "true" ]; then \
133
  # install python dependencies
134
  COPY --chown=$UID:$GID ./backend/requirements.txt ./requirements.txt
135
 
 
136
  RUN pip3 install uv && \
137
  if [ "$USE_CUDA" = "true" ]; then \
138
  # If you use CUDA the whisper and embedding model will be downloaded on first use
 
133
  # install python dependencies
134
  COPY --chown=$UID:$GID ./backend/requirements.txt ./requirements.txt
135
 
136
+ RUN pip3 install huggingface_hub
137
  RUN pip3 install uv && \
138
  if [ "$USE_CUDA" = "true" ]; then \
139
  # If you use CUDA the whisper and embedding model will be downloaded on first use
backend/requirements.txt CHANGED
@@ -106,3 +106,4 @@ googleapis-common-protos==1.63.2
106
  ldap3==2.9.1
107
 
108
  gnupg
 
 
106
  ldap3==2.9.1
107
 
108
  gnupg
109
+ huggingface_hub
backend/scripts/backup.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import subprocess
4
+ from huggingface_hub import HfApi
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:
14
+ raise ValueError("HF_TOKEN is not set.")
15
+ if not space_id:
16
+ raise ValueError("SPACE_ID is not set (or define repo_id manually).")
17
+
18
+ # Encrypt using GPG
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 db_backup/webui.db.gpg")
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
+ api.upload_file(
34
+ path_or_fileobj="db_backup/webui.db.gpg",
35
+ path_in_repo="db_backup/webui.db.gpg",
36
+ repo_id=repo_id,
37
+ repo_type="space",
38
+ token=hf_token,
39
+ commit_message="Update encrypted database backup"
40
+ )
41
+ print("DB backup uploaded successfully!")
42
+
43
+ if __name__ == "__main__":
44
+ backup_db()
backend/start.sh CHANGED
@@ -73,8 +73,7 @@ WEBUI_PID=$!
73
  echo "==============================================="
74
  echo "Daily backup job: encrypting and pushing the DB"
75
  echo "==============================================="
76
- # Calls your db_crypt.sh function
77
- bash "$SCRIPT_DIR/db_crypt.sh" encrypt_database
78
  # Sleep for 24 hours
79
  sleep 86400
80
  done
 
73
  echo "==============================================="
74
  echo "Daily backup job: encrypting and pushing the DB"
75
  echo "==============================================="
76
+ python "$SCRIPT_DIR/backup.py"
 
77
  # Sleep for 24 hours
78
  sleep 86400
79
  done